Fixed a slow render of a template

I was having a performance problems in my template.

It was taking 15 seconds to render with 280 queries. Which seemed slow.

Tried optimizing as best I could but had no luck.

After much searching it turned out that the solution was to remove the django-debug-toolbar!

Removed it and 15s turned to a 4s render. Yay!

So yeah - be aware of this little gotcha!

1 Like

Removing django-debug-toolbar’s overhead is probably hiding the problem, rather than fixing it. Yes it adds an overhead for query which slows things down, but you shouldn’t be making 280 queries in the first place - that’s a LOT. Those 4 seconds locally will translate to much more in production, since your database is unlikely to be on the same server and every query goes over the network.

I’d recommend you continue using data django-debug-toolbar locally and use its query traces to figure out where to add select_related() / prefetch_related() appropriately.

Also check out django-auto-prefetch which automatically adds prefetches to your queries and can eliminate the 1+n query problem :slight_smile:

1 Like

I went back and checked all my queries and I was already using select_related.

I have the render speeed at 2seconds now while still having the name number of queries.

The original page had 600 queries before I added the *_related() stuff.

thank you for django-auto-prefetch it looks like an easy way to keep efficientcy.

The reason I have so many queries on that page is that it’s a dashboard like page with a lot of info showing. I suspect caching might help here if the page get silly slow.