Forums

django site super slow but I cannot find out why

My django app is super slow since I migrated to pythonanywhere. I do have the faint idea that I might run into some limit of some but I don't know what it is. I put in a lot of timers and one of the loops that runs on the old cloud space (the cheapest Hetzner) in 0.8 sec now needs 15-20 secs.

So the first thing I did after reading a little bit here is that I migrated from sqlite to mysql. This did not change anything. There are some nested loops indeed, but depth is max 2-3 and I have like 40 entries in my database.

What is odd, is that I measured in my loops for every loop the time and it is in the area of 0.003secs per loop, which adds up to 0.14 secs. But then assigning the result to the context dict takes maybe 15 secs or so. This does not seem right and I wonder into which limitation I run. Testing on the old system and on my pc yields results always <1 sec for the whole page.

I also checked the template rendering by just showing an empty page which did not alter the time at all. Also there only is this one view.

CPU usage is 5% according to the dashboard and does not change during the day to a value > 10%.

You can generally expect it to run a little bit slower than on your local machine. You don't have a dedicated server on PythonAnywhere and therefore the resources are shared with other users. This does sound particularly slow though. Web app usage doesn’t count toward your CPU seconds, so you can ignore that stat while debugging your web app. Did you measure the execution speeds only on one occasion or have you tried it at different times of the day? Does it vary at all?

I measured it 0am, 6am and 12am ... so some h apart and the times were still incredible slow.

The fact that switching from sqlite -> mysql did not change the speed of the site is somehow making think that i somehow exceed some other limitation. Question is which one, and how to find out.

I have a web app with over a million rows of data and querying for a result can take 0.2 seconds to 0.3 seconds, i have mysql and not sqlite3. and have the columns being queried indexed

i dont know what these loops are that you are referring to.

@vool I'd suggest adding some logging to your app so you can narrow down where exactly it slows/hangs and then investigate that given bit.

I did end up splitting some backend actions that fill tables in the frontend in different functions and call them from the frontend at different times - I never found out what was the bottleneck unfortunately :(

Could you share the code snippet that is taking a particularly long time? If you don't want to share it publicly you can also email us at support@pythonanywhere.com