Forums

MySQL query slowness

Hello, I have deployed a Django app on this account for a customer few months ago on an hacker account.

Now the app has grown and very often customers detect slowness on a specific view that keep up to 20-25 seconds to be rendered, so I decided to investigate what is needed to better perform.

Initially I thought it was a limit of have just one web worker problem but static content are served regularly .

I have refreshed my local instance on MySql DB with the same data of the server on and then execute the same local copy of code and here the time are in order of 1-2 seconds to render the same slow page, mmmhh strange...

So I decided to deep investigate and make Django print out the executed SQL query on the console and manually run it with MySQL Workbench, here the surprise: same query on local MySql instance with identical data takes about 0.8 sec to be executed and on remote over SSH instance out of 3 tests the best result was 20 seconds.

I think this is the point of the slowness.

Instance is: fazzariworkout$sworkout

Now, what can we do ? The customer is also willing to invest money to solve the problem but at present I don't know what to recommend.

I appreciate your support!

Alessandro (Italy)

There are a couple of reasons why it might be be slower in production than locally. In order of how easy they are to fix:

  • More data in production than in dev -- this is obviously not the case for you, as you've run the test with identical data, but I mention it just in case someone else comes across this forum post who didn't do that check.
  • Table fragmentation -- when you delete or update a row in a table, MySQL can leave "holes" in the file representing the table, which makes it larger than it needs to be and potentially slower to query. There's an optimize table command you can use to "defragment" the tables and fix that.
  • Different indexes -- sometimes due to simple errors in migration strategies, it's possible that your live DB might lack some of the indexes that it's meant to have. The site will run, so you might not notice until things slow down. So it's worth double-checking to make sure that that hasn't happened in this case.
  • Simply having less resources. Your local machine is probably something like a four-core one with 16GiB RAM. A DB server with the same specs, including all hosting costs (electricity, bandwidth, cooling, physical security, and so on) would cost $300/month or so. So what you have as part of your account is a share of such a machine, so it will in general be slower. It is worth noting, though, that if this is the problem, upgrading to a private MySQL server might not be necessary -- you can do more with the same hardware if you have proper indexes on your tables, while performance problems caused by missing indexes might be masked on your dev server because you have sole access to quite a lot of power. So if you are resource-constrained, checking whether you have indexes on all of the columns that you're querying on is the first thing to do.
  • Latency. If you're doing lots of small queries, then the network latency caused by having to send the query from the server where your code is running to the MySQL server, and the results back, might have an impact -- which you would not see if you were running both of them on the same machine. This one, unfortunately, isn't something that can be fixed.

So, in summary, I'd suggest that you take a look at each of these possibilities in turn, apart from the first one, which you've already done.