I'm building a news theme, and it is generating far too many queries on the front page. This is due to multiple loops and multiple sidebars on the page. The theme options are all stored in one single array in the database (so I access them as $settings['themename_option'].
I'm wondering if there is a way that news theme authors reduce the number of database queries on their front pages. Each news box area on the front page is set to retrieve posts from a specific category. The user can set how many posts should appear in each news box.
So, I have 6 different news box areas on the front page. Each news box area has two loops: the primary loop, and the "More stories from this category" loop. The user doesn't have to have all newsbox areas and loops activated. But when he/she does, the query count goes up very quickly.
I'm using WP_Query object to handle each query.
Any insights on reducing query counts? Or is a high query count just a given with news themes?
Denzel Chia answers:
High query counts cannot be avoided by multiple queries on a homepage.
The only way is to tell the user of this theme to install a cache plugin,
such as wp-super-cache, so that it does not always goes to database queries,
instead, it queries once and shows cached pages for a defined period of time.
You can set wp-super-cache to clear cache when a new post or page is published,
so that the home page is updated instead of showing cache.
If you are not using cache plugin.
You can try writing you codes to use the WordPress Transient API.
Denzel Chia comments:
More info on Transient API on WordPress Codex http://codex.wordpress.org/Transients_API
John Cotton answers:
To some extent, it's a given, but you can do things.
To cut the number of queries down, you've got to make your own custom queries and then do the leg work in the PHP code to split out which content goes to which box.
So instead of the usual "while the_posts()" loop, you need to test for changes in category for instance or post type or whatever else is driving your layout.
If you need to vary the query, but you can keep the same output (ie columns), then doing a UNION between multiple SELECT statements can help since that can reduce the number of queries from many to 1 (albeit longer). Just add an extra column in to differentiate for your revised loop.
Do you have a rough example of how that might work? E.g. how the code might look? Would it involve actually writing SQL queries?
arman syam answers:
I welcome any feedback, but here are the main concerns I have at this point and would like some advice on:
1. The number of database queries (you can see the count in the footer). The front page in a typical configuration generates about 154, and the average for other pages is about 50 ~ 60. I'm aware that it's on the high side. I know it's hard to give advice without actually seeing the code, but I'm using multiple WPQuery objects to run the multiple loops. Plus, there are 7 sidebars and 3 wp_nav menus in use.
Citizen Press has a lot of options, but they are all stored in the database as an array and only make one entry. Doing that reduced my queries from over 200 to 144. Adding the wp_nav_menus raised the number of queries to 154.
2. Someone noted on Stack Overflow that I am using HTML to resize images instead of thumbnails, and that's causing performance to lag. I'm using WordPress's post thumbnail feature for all images on the front page. I have defined image sizes using set_thumbnail_size.
OK that was me, lol. I've been working on this issue for months. How did you find that? :D
Oleg Butuzov answers:
Only one way to optimize it to use db sql cache - you can use this plugin.
believe my experience, this is only one true sollution to optimize sql.
Yeah, I had installed that before and it cut the number of queries down by more than half, so it's good!
Victor Teixeira answers:
In one of the sites I'm building right now I have this on the homepage:
Page generated in: 0,405 seconds.
Total memory used: 30.8 MB
Total number of queries: 163
Total execution time: 0.06046 seconds
I don't think 163 queries are slowing the website. Actually it feels really fast.
PS: I have an NGINX reverse proxy in front of it. And on the Apache server I have my .htaccess properly configured for client side caching.
Yeah, my site still seems very fast, even with the high number of queries. I was just concerned because people always say a high number of queries is bad.