How to use MySQL Found_Rows() in PHP?

SQL_CALC_FOUND_ROWS is only useful if you’re using a LIMIT clause, but still want to know how many rows would’ve been found without the LIMIT.

Think of how this works:

SELECT SQL_CALC_FOUND_ROWS * FROM Users;

You’re forcing the database to retrieve/parse ALL the data in the table, and then you throw it away. Even if you aren’t going to retrieve any of the rows, the DB server will still start pulling actual data from the disk on the assumption that you will want that data.

In human terms, you bought the entire contents of the super grocery store, but threw away everything except the pack of gum from the stand by the cashier.

Whereas, doing:

SELECT count(*) FROM users;

lets the DB engine know that while you want to know how many rows there are, you couldn’t care less about the actual data. On most any intelligent DBMS, the engine can retrieve this count from the table’s metadata, or a simple run through the table’s primary key index, without ever touching the on-disk row data.

Leave a Comment