標籤:sea 慢查詢 方案 聯合 如何 cti sel alert mysq
使用聯合索引需要注意的列順序
比如在使用
select * from user where x=1 and y=2;
的時候,應該需要建立的索引可能是 add key(x,y)
如何確定索引的順序
一般經驗而言
可以使用
select count(distinct x)/count(x) as x_selectivity,
count(distinct y)/count(y) as x_selectivity,
count(*),
from user;
************************row1***************************
x_selectivity: 0.0001
y_selectivity: 0.0312
count(*) : 16022
在x中的選擇性越高,所以可以放在第一列
alert table user add key(x,y);
另外可以在曆史的慢查詢中找到類似的進行最佳化
比如
select * from user where x=1 and y=2;
select sum(x=1),sum(y=2) from user\G;
******************row1**********************************
sum(x=1): 7992
sum(y=2): 30
y的選擇性會更高一些,可以放在第一列
然後看看y=2 對應的x列的選擇性
select sum(x=1) from user where y=2
******************row1**********************************
sum(x=1): 17
但是有可能mysql存在查詢不公平的情況,伺服器的整體效能可能更糟糕,,所以需要提取一下最差的查詢進行這項的查詢工作
這類工作被某些最佳化極客geek稱為sarg。這是“可搜尋參數(searchable argument)”的縮寫。
mysql b-tree 索引下聯合索引的已排序的測試方案