Query Keywords > SQL_CALC_FOUND_ROWS

Categories

Query Keywords

Definition

The SQL_CALC_FOUND_ROWS keyword is used when the result set of a query will be restricted by a LIMIT clause, instructing the MySQL server to store the number of rows that would have been returned had the result set not been restricted. The stored value is exposed through the FOUND_ROWS() function.

Sample

In this example, we first produce a list of users, restricted with the LIMIT clause, then we retrieve the full count of users with the FOUND_ROWS() function:

mysql> SELECT SQL_CALC_FOUND_ROWS emailaddress FROM users LIMIT 10,3;
+-----------------------------+
| emailaddress                |
+-----------------------------+
|  ben.@foo.com               |
|  charlie@bar.com            |
|  aaron@baz.com              |
+-----------------------------+
5 rows in set (0.00 sec)mysql>

SELECT FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
|       435136 |
+--------------+
1 row in set (0.00 sec)

Further Reference

SELECT Syntax - MySQL Reference Manual

Found_Rows() - MySQL Reference Manual

Google Cache of Article on Improving COUNT(*) Performance

Leave a Reply