SQL_CALC_FOUND_ROWS and no_found_rows

·

, ,

By default, the WordPress SQL query that selects posts from the database will use “SELECT SQL_CALC_FOUND_ROWS …”. This is mainly for pagination purposes, to speed up subsequent queries for the next set of results. Writing the query that way certainly helps speed up the site as a whole, but is useless in some circumstances.

Remedy: Look through your theme and plugin code for WP_Query objects. On queries that don’t need pagination, add a new query variable to the mix that looks like this:

[code language=”php”]
$a_faster_wp_query = new WP_Query( array(
‘foo’ => ‘bar’,
‘more_foo’ => ‘extra_bar’,
‘no_found_rows’ => true,
));[/code]

(The magic is on line 4 – it must be passed as an actual boolean, not a string)

Use this tidbit on every WP_Query that doesn’t need pagination to make WordPress omit the “SQL_CALC_FOUND_ROWS” from it’s query, thus speeding up the post selection query.

Leave a Reply

Discover more from Rich Collier

Subscribe now to keep reading and get access to the full archive.

Continue reading