tags:

views:

487

answers:

3

MySQL 4.0 doesn't have information_schema and 'show table status from db' only gives approximate row count for innodb tables.

So whats the quickest way to get the count of innodb tables, of course other than count(*), which could be slower with big tables.

+1  A: 

Updated

When using InnoDB the only accurate count of rows in your entire table is COUNT(*). Since your upgrade from 4.0 to 5.0 will only occur once, you'll just have to deal with the speed.

hobodave
Thanks hobodave. As I just responded to your comment above, i would like to do a sanity check before and after migration and so i can't go for a separate table approach, in this case.
Thiru
A: 

Well, using * is definitely not optimal but how about just 1 column. I usually use the id column to count # of rows.

Knix
in MySQL, a specific column is not needed as one can just do: select count(1) from my_table
Cody Caughlan
Thanks Knix and Caughlan. Since my innodb tables are huge, the count(1) also takes a lot of time. I wonder if dropping all the indices on innodb tables would expedite the count(1).
Thiru
Then I think it would be best just to keep a counter in a different table whenever a row gets inserted or deleted. Is this an option for you?
Knix
It would've been an option if i'm designing the DB now but the tables are there for years now and i need this count() for a sanity check for my 4.0 to 5.0 mysql upgrade.
Thiru
A: 

This applies to all versions of MySQL. As other commenters have pointed out - there is no fast SELECT COUNT(*) in InnoDB. Part of the reason for this is that InnoDB is multi-versional, and it will depend on the context of your transaction how many rows there are supposed to be in a table.

There are some workarounds:

1) If you never delete, SELECT MAX(id) should return the right number of rows.

2) Instead of deleting rows, you can archive them to a 'deleted rows table' (a lot of people seem to want to keep everything these days). Assuming that the delete is a much smaller subset of still current, you may be able to subtract count(*) from deleted_rows from SELECT max(id) from not_deleted.

3) Use triggers. This sucks for performance.

There's quite a technical discussion on this problem here: http://mysqlha.blogspot.com/2009/08/fast-count-for-innodb.html

Morgan Tocker