MySQL get the number of rows in an innodb table

前端 未结 4 983
星月不相逢
星月不相逢 2021-02-08 14:33

I have a table using innodb. I know the table has roughly 89 million rows. Using

SELECT COUNT(*) FROM table;

takes about five minutes to run. I

相关标签:
4条回答
  • 2021-02-08 15:04

    If you are OK with the estimated number and just don't want to mess with running SHOW TABLE STATUS from PHP, you can use the information_schema DB:

    SELECT TABLE_ROWS FROM information_schema.tables
    WHERE TABLE_SCHEMA = 'my_db_name' 
    AND TABLE_NAME = 'my_table_name';
    
    0 讨论(0)
  • 2021-02-08 15:14

    If you are ok with approximate number of records, you can use output of "explain".

    Simplified verion of the code is

    $result = mysql_query('explain SELECT count(*) from TABLE_NAME');
    $row = mysql_fetch_assoc($result);
    echo $row['rows'];
    
    0 讨论(0)
  • 2021-02-08 15:20

    If the table is read frequently and updated infrequently, you may want to consider creating a statistics table that is updated via triggers when making changes to the table.

    0 讨论(0)
  • 2021-02-08 15:29

    mysql_num_rows may be useful to you.

    0 讨论(0)
提交回复
热议问题