Change Displayed Text SizeGrow Displayed Text SizeShrink Displayed Text Size
 

Wednesday, April 28, 2010

Don't count() in mysql!

If you're new to mysql, you're going to try:
select count(*) from table_name;

Using that count() is resource intensive, it basically has to iterate over everything. If you have a large table that can take a while, and cause real problems.
There's a better way.

select table_rows from information_schema.tables where table_name='table_name';

That gets the count that mysql uses to keep track of that table internally. There are some caveats - it may not always be 100% up to date depending on your configuration, etc. But it sure beats hosing your server.

4/28/2010 04:23:00 PM ] [  0 comments  ]
[archives]
A good quick laugh