Speed up WordPress WP_Query and query_posts functions
While working on a WordPress theme, I noticed a low MySQL query cache hit rate. There were a lot of
SQL_CALC_FOUND_ROWS queries that were not cached and were slowing down the database.
It appears WordPress uses
SQL_CALC_FOUND_ROWS in most queries in order to implement pagination. Even when you don’t need pagination at all.
SQL_CALC_FOUND_ROWS tells MySQL to do additional work to count the total matching rows and this can make a lot of difference if you have a big archive.
By inspecting WordPress 3.x source code I found an undocumented parameter that can be used with
query_posts functions. The parameter is named
no_found_rows and it accepts boolean values. It tells WordPress not to count the total rows. Here’s a usage example:
Hope this will help WordPress developers around the world lower their carbon footprint.
UPDATE 25 Oct 2016: I checked on WordPress 4.6.1 and now
WP_Query do not, so you should still set
no_found_rows=true when using them and don’t need pagination.