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.

About these ads

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 293 other followers

%d bloggers like this: