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.


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


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.


About mcloide

Making things simpler, just check: View all posts by mcloide

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: