Tag Archives: php pagination

MySQL Tricks

This is a small trick with MySQL that will make your paginate methods and queries faster.

When doing a SELECT use SQL_CALC_FOUND_ROWS on it so after the select is done you will already have the count of the rows even if you are doing a limit.

Example:

SELECT SQL_CALC_FOUND_ROWS id, name, created FROM my_table LIMIT 0,10;

This will return the 10 first results from the table, but if you do

SELECT FOUND_ROWS();

It will return the number of rows that it would have been if the prior select was done without the limit.

This small trick can speed up considerably the pagination with PHP and the speed of your queries since you now don’t have to do a SELECT count(id) FROM my_table to know how many records you have.


Follow

Get every new post delivered to your Inbox.

Join 293 other followers

%d bloggers like this: