Reducing database queries in ExpressionEngine

ExpressionEngine can use a lot of database queries, especially when you have embedded templates, numerous facilties “turned on”, or just a lot of things happening on a page.

I decided to run a quick test to see how queries could be easily reduced by tweaking various settings in the back end. This wasn’t an exhaustive test by any means but it does demonstrate how to easily shave a few queries from your EE pages.

Test 1 - typical blog listings page

Start total 38 queries
Turn off Online user tracking saved 2 queries
Turn off Template hit tracking saved 1 query
Turn off Channel entry view tracking saved 0 queries
Enable Template caching for multiple entry template saved 24 queries
End result 11 queries

Test 2 - typical blog single entry page

Start total 37 queries
Turn off Online user tracking saved 4 queries
Turn off Template hit tracking saved 1 query
Turn off Channel entry view tracking saved 1 query
Enable Template caching for single entry template saved 18 queries
End result 13 queries

Analysis

Although turning off Tracking preferences only saves a few queries it’s well worth doing if you don’t rely on those numbers -  only turn them on when you absolutely need them.

Template caching is the biggie as demonstrated by the results, so whenever you can cache them it’s well worth doing. Of course if you display future entries (eg in an events calendar), caching won’t work for templates where they’re used.

There are many more ways to optimise EE’s performance which I hope to write about in the near future.