Slow SQL_CALC_FOUND_ROWS Query

The use of SQL_CALC_FOUND_ROWS is not really a problem, although it incurs an overhead.

What happens is, WordPress uses SQL_CALC_FOUND_ROWS in order to determine the total posts that would have been returned, if no LIMIT clause was provided. This is necessary in order to calculate and provide you with correct pagination links.

Disabling it unconditionally is guaranteed to break things all over the place.

If you can identify specific queries that suffer from its use, and you can do without pagination, then you could hook to pre_get_posts and conditionally set the no_found_rows parameter to true.
This, however, is more a hack than a solution.

The proper solution is to use a database query caching mechanism, either on the side of the database, or on the WordPress side using a plugin such as Advanced Post Cache (developed for and used in WordPress.com)

Leave a Comment