MySQL-count(*) 和 not in 的查詢優化


在這裏插入圖片描述


官方文檔

https://dev.mysql.com/doc/

在這裏插入圖片描述

如果英文不好的話,可以參考 searchdoc 翻譯的中文版本

http://www.searchdoc.cn/rdbms/mysql/dev.mysql.com/doc/refman/5.7/en/index.com.coder114.cn.html
在這裏插入圖片描述


優化的原因

MySQL-Btree索引和Hash索引初探 中 什麼情況下會使用到B樹索引 。

not int 和 <> 操作無法使用索引


not in 的優化

如果not in 的指標範圍非常大的話,這個效率很差。

舉個例子

select customer_id ,first_name ,last_name ,email 
from customer 
where customer_id 
not in (select customer_id from payment);

每個customer_id都要到payment中查詢一遍, 數據量大時很慢。

優化後 -----------> left join

select customer_id ,first_name ,last_name ,email 
from customer  a 
left join payment b 
on a.customer_id = b.customer_id 
where b.customer_id  is null



這樣的話,可以避免對payment表的多次查詢。


使用匯總表優化count(*)查詢

select count(*) from product_comment where product_id = 999;

如果這個表 有上億條,或者併發訪問很高的情況,這個SQL的執行效果也不是很理想

優化思路:就是使用匯總表

彙總表就是提前統計出來數據,記錄到表中以備後續的查詢使用。

Step1: 建立彙總表

字段看自己的需求,基本的有下面兩列

create table product_comment_cnt(product_id int , cnt int);

然後 每天定時的彙總,更新改表,對於當天新增的未統計到的數據,可以單獨查詢,然後累加

新的SQL如下

select sum(cnt) from (
  # 彙總表中查詢到的由定時任務更新的數據 
   select cnt from product_comment_cnt where product_id = 999
 
    union all 
  
   #  新增的數據 
    select count(*) from product_comment where product_id = 999 
     and timestr > date(now())
) a 

提供思路,實際情況自行調整。


發佈了820 篇原創文章 · 獲贊 2035 · 訪問量 416萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章