简体   繁体   中英

Getting row count for a table in MySQL?

Can anyone tell me which is he better appraoch in MySQL whin it comes to finding the row count of a table: is it better to do

SELECT COUNT(*) FROM TABLE_NAME

or to look up the row count in the table TABLE in the INFORMATION_SCHEMA ?

This would be a frequent operation for calculating pagination counts for pages?

I should add that tables will only have a few thousand rows at most.

Thanks

Martin O'Shea.

In MyISAM , this query:

SELECT  COUNT(*)
FROM    TABLE_NAME

is instant, since it's kept in the table metadata, so it's almost free to issue this query and it will always get the correct result.

In InnoDB , this query will count rows one-by-one which could take some time.

So if you don't need exact value of COUNT(*) , you may query INFORMATION_SCHEMA .

如果您发现COUNT(*)太慢,我还会考虑使用SQL_CALC_FOUND_ROWSSELECT FOUND_ROWS()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM