is SELECT COUNT(*) expensive?

后端 未结 7 1726
小蘑菇
小蘑菇 2021-01-13 00:16

Do you think it\'s a good idea to count entries from a really big table (like 50K rows) on each page load?

SELECT COUNT(*) FROM table

Right

7条回答
  •  -上瘾入骨i
    2021-01-13 00:54

    COUNT(*) is optimized to return very quickly if the SELECT retrieves from one table, no other columns are retrieved, and there is no WHERE clause. For example:

    mysql> SELECT COUNT(*) FROM student;
    

    This optimization applies only to MyISAM tables only, because an exact row count is stored for this storage engine and can be accessed very quickly.

    Source

    As you said you use MyISAM and your query is for the whole table, it doesn't matter if its 1 or 100000 rows.

提交回复
热议问题