MySQL count(*),count(1),count(field)區別、性能差異及優化建議

2020-12-27 偏執的IT工匠

MySQL count函數是用來獲取表中滿足一定條件的記錄數,常見用法有三種,count(*),count(1),count(field),這三種有什麼區別?在性能上有何差異?本文將通過測試案例詳細介紹和分析。

原文地址:mytecdb.com/blogDetail.php?id=81

三者有何區別:

count(field)不包含欄位值為NULL的記錄。count(*)包含NULL記錄。select(*)與select(1) 在InnoDB中性能沒有任何區別,處理方式相同。官方文檔描述如下: InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way. There is no performance difference.

1. 性能對比

通過案例來測試一下count(*),count(1),count(field)的性能差異,MySQL版本為5.7.19,測試表是一張sysbench生成的表,表名sbtest1,總記錄數2411645,如下:

測試SQL語句:

select count(*) from sbtest1;select count(1) from sbtest1;select count(id) from sbtest1;select count(k) from sbtest1;select count(c) from sbtest1;select count(pad) from sbtest1;

針對count(*)、count(1)和count(id),加了強制走主鍵的測試,如下:select count(*) from sbtest1 force index(primary);select count(1) from sbtest1 force index(primary);select count(id) from sbtest1 force index(primary);

另外對不同的測試SQL,收集了profile,發現主要耗時都在Sending data這個階段,記錄Sending data值。

匯總測試結果:

結果分析:

從以上測試結果來看,count(*)和count(1)性能基本一樣,默認走二級索引(k_1),性能最好,這也驗證了count(*)和count(1)在InnoDB內部處理方式一樣。count(id) 雖然也走二級索引(k_1),但是性能明顯低於count(*)和count(1),可能MySQL內部在處理count(*)和count(1)時做了額外的優化。強制走主鍵索引時,性能反而沒有走更小的二級索引好,InnoDB存儲引擎是索引組織表,行數據在主鍵索引的葉子節點上,走主鍵索引掃描時,處理的數據量比二級索引更多,所以性能不及二級索引。count(c)和count(pad)沒有走索引,性能最差,但是明顯count(pad)比count(c)好,因為pad欄位類型為char(60),小於欄位c的char(120),儘管兩者性能墊底,但是欄位小的性能相對更好些。2. count(*)改進

在5.7.18版本之前,InnoDB處理select count(*) 是通過掃描聚簇索引,來獲取總記錄數。從5.7.18版本開始,InnoDB掃描一個最小的可用的二級索引來獲取總記錄數,或者由SQL hint來告訴優化器使用哪個索引。如果二級索引不存在,InnoDB將會掃描聚簇索引。執行select count(*)在大部分場景下性能都不會太好,尤其是表記錄數特別大的情況下,索引數據不在buffer pool裡面,需要頻繁的讀磁碟,性能將更差。

3. count(*)優化思路

一種優化方法,是使用一個統計表來存儲表的記錄總數,在執行DML操作時,同時更新該統計表。這種方法適用於更新較少,讀較多的場景,而對於高並發寫操作,性能有很大影響,因為需要並發更新熱點記錄。如果業務對count數量的精度沒有太大要求,可使用show table status中的行數作為近似值。

相關焦點

  • COUNT函數和COUNTA函數有什麼區別
    我們都知道count是計數,那count和COUNTA有什麼區別呢?今天小編就給大家介紹一下。介紹:COUNT :COUNT(value1,value2, ...);函數計算包含數字的單元格個數以及參數列表中數字的個數。value1 是必需參數。 要計算其中數字的個數的第一項、單元格引用或區域。
  • Down for the count? 敗得很慘
    Reader question:Please explain "down for the count", as in "Donald Trump is down for the count."My comments:Oh, a boxing term.
  • 分享一首英文歌曲《Count on Me》!
    on me like 1 2 3你可以向我數到:123I'll be there我就會出現在你身邊And I know when I need it I can count on you like 4 3 2而我清楚,當我需要時,我也可以向你倒數:432And you'll be there你就會在我面前出現
  • 聽歌學英語:Count on Me-Connie Talbot
    on me like 1 2 3你可以依靠我,就像1, 2, 數到3I』ll be there我就會出現一句歌詞:You can count on me like 1 2 3你可以依靠我,就像1, 2, 數到3Count on 依靠;指望例句:That was
  • 周二有禮
    如果你發現自己迷失在黑暗中,你看不到的I'll be the light to guide you我會指引你Find out what we're made of找到我們了When we are called to help our friends in need什麼叫我們去幫助需要幫助的朋友You can count
  • 彼此信賴:Count On Me-Connie Talbot
    on me like 1 2 3你可以依靠我,就像1, 2, 數到3I』ll be there我就會出現And I know when I need it I can count on you like 4 3 2而我知道我有需要時,我可以依靠你,就像倒數4,3,2And you』ll be there Cause
  • 朱小迪英語故事:Spot can count(小玻會數數)
    點擊下方綠標聽故事Spot can count
  • 聽歌學英語(第25期)Count on me
    了解我們有何真本事及What we are called to help our friends in need天職幫助患難中的朋友You can counton me like 1 2 3你可以依靠我,就像1, 2, 數到3I'll be there我就會出現And I know when I need it I can count
  • 單詞怎麼記系列(5)詞根 count
    本期講解常用詞根 -count-,結合詞綴,讓你快速秒記單詞。 詞根.count- : = to count, 表示「計算」,拉丁語computare的變體,源自表示「剪、砍、思考」的詞根put-。
  • 如何快速區分Excel表中的三個count函數?
    在Excel表格中錄入各類信息時,遇到「count」類函數是常有的事,count,counta,countblank,countif... 那這些count函數有何區別呢?今天Sara想和小夥伴分享下前面三個的異同。
  • Sing with Me:
    言歸正傳,count on me是你可以指望我、依靠我、可以信任我的意思。這首歌的歌詞也很美,它用了很多的比喻手法,表達的就是朋友間的一種信任,你遇到困難可以找我我遇到困難也可以找你。由於原唱對我來說不太友好,所以我另外找了一首翻唱。
  • 漫畫 耽美漫畫《10 count》
    寶井理人最火漫畫10 count正在連載中!     身為社長秘書的城谷忠臣雖然有優秀的工作能力,但也有嚴重的潔癖。他每天都戴著手套,酒精從不離身,極度不喜歡與他人接觸,總是和對方保持一定的距離。這樣的他在偶然間遇到心理諮詢師黑瀨陸。看似冷漠的黑瀨不知為何硬是要幫城谷治療潔癖。
  • 抖音捧臉殺神曲《Count on Me》好聽到爆!
    on me like 1 2 3你可以向我數到:123I'll be there我就會出現在你身邊And I know when I need it I can count on you like 4 3 2而我清楚,當我需要時,我也可以向你倒數:432And you'll be there你就會在我面前出現
  • 清新甜美的《Count on Me》,單曲循環停不下來
    on me like 1 2 3 你可以依靠我,就像1, 2, 數到3 I』ll be there 我就會出現 And I know when I need it I can count on you like 4 3 2
  • 聽歌學英語∣小康妮演繹火星哥的歌曲Count on Me
    on me like 1 2 3你可以向我數到:123I'll be there我就會出現在你身邊And I know when I need it I can count on you like 4 3 2而我清楚,當我需要時,我也可以向你倒數:432And you'll be there你就會在我面前出現Cause that's
  • 好聽到爆的抖音捧臉殺神曲《Count on Me》
    onme like 1 2 3你可以依靠我,就像1, 2, 數到3I』ll be there我就會出現And I know when Ineed it I can count on you like 4 3 2而我知道我有需要時,我可以依靠你,就像倒數4,3,2And you』ll be there Cause that
  • MySQL性能優化
    ,必須一步一步慢慢來,從各個方面進行優化,最終性能就會有大的提升。,可以提高速度]對mysql配置優化 [配置最大並發數my.ini, 調整緩存大小 ]mysql伺服器硬體升級定時的去清除不需要的數據,定時進行碎片整理(MyISAM)資料庫優化工作對於一個以數據為中心的應用,資料庫的好壞直接影響到程序的性能,因此資料庫性能至關重要。
  • MySQL中order by與limit不要一起用!
    摘自「LIMIT 查詢優化」如果你只需要結果集中的指定數量的行,那麼請在查詢中使用 LIMIT 子句,而不是抓取整個結果集並丟棄剩下那些你不要的數據。MySQL 有時會優化一個包含 LIMIT 子句並且沒有 HAVING 子句的查詢:①MySQL 通常更願意執行全表掃描,但是如果你用 LIMIT 只查詢幾行記錄的話,MySQL 在某些情況下可能會使用索引。
  • 抖音託臉的背景音樂是什麼 Count on me歌詞欣賞
    on me like 1 2 3  I'll be there  And I know when I need it I can count on you like 4 3 2  And you'll be there  Cause that's what friends are supposed to do oh yeah
  • 每日一句英語:Count me in. 算我一個。
    支持範例   1. If the cost of the trip is no more than 1000 yuan, you can count me in.   如果這次旅行的花費不超過一千元,就算我一個。   2. Please count me in this project.   請讓我參加這個項目。