count 1 比 count 效率高嗎

2021-09-24 15:35:44 字數 469 閱讀 9597

一直聽同事說mysql的count(1) 比 count(*)要快,其實這兩個的執行效率是一樣的。

myisam引擎會把乙個表的總行數記錄下來,所以在執行count(*)的時候會直接返回數量,執行效率非常快。在mysql 5.5後預設引擎切換為innodb,innodb因為增加了版本控制的原因,同時有多個事務訪問資料並且有更新的時候,每個事務需要維護自己的可見性,那麼每個事務查詢到的行數也可能不相同,所以不能快取具體的行數,每次都需要count一下所有的行數。

innodb handles select count() and select count(1) operations in the same way. there is no performance difference.*

mysql官網給出的解釋是這兩者沒有區別的

count 1 和count 哪個高效?

當表的資料量大些時,對錶作分析之後,使用count 1 還要比使用count 用時多了!從執行計畫來看,count 1 和count 的效果是一樣的。但是在表做過分析之後,count 1 會比count 的用時少些 1w以內資料量 不過差不了多少。如果count 1 是聚索引,id,那肯定是coun...

count 和count 1 的區別

create table test1 id number,name varchar2 50 create time date 插入1000000條資料。begin for i in 1 10000000 loop insert into test1 values i,dba fashion測試 i,...

COUNT 1 和COUNT 的區別

兩者本質上沒有任何區別。參見mysql 參考手冊 innodbhandlesselect count andselect count 1 operations in the same way.there is no performance difference.一 count情況 1 count 1...