您好,登錄后才能下訂單哦!
這篇文章主要講解了“MySQL5.6怎么快速定位不合理索引”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“MySQL5.6怎么快速定位不合理索引”吧!
# 若CARDINALITY / TABLE_ROWS小于10%(經驗值),那么表示數據重復率較高,通常需要考慮是否有必要創建該索引
#########################################################
# mysql 5.6
目前5.6的STATISTICS表中關于Cardinality的統計是錯誤的!!!具體可見MySQL bugs #78066。
但是,表innodb_index_stats中關于Cardinality值得統計依然是正確的
#########################################################
SELECT
t.TABLE_SCHEMA,
t.TABLE_NAME,INDEX_NAME,
s.CARDINALITY,
t.TABLE_ROWS,
s.CARDINALITY/t.TABLE_ROWS AS SELECTIVITY
FROM
information_schema.TABLES t,
(SELECT database_name,table_name,index_name,stat_value AS CARDINALITY
FROM mysql.innodb_index_stats
WHERE (database_name,table_name,index_name,stat_name) IN
(SELECT table_schema,table_name,index_name,CONCAT('n_diff_pfx0',MAX(seq_in_index))
FROM information_schema.STATISTICS
-- where table_name='xxxxx'
GROUP BY table_schema , table_name , index_name )) s
WHERE t.table_schema = s.database_name
AND t.table_name = s.table_name
AND t.table_rows != 0
AND t.table_schema NOT IN ( 'mysql','performance_schema','information_schema')
ORDER BY SELECTIVITY;
以下部分出自:http://mp.weixin.qq.com/s?__biz=MjM5MjIxNDA4NA==&mid=401131835&idx=1&sn=37c5fd9d3d8670fb379a1e0565e50eeb&scene=0#wechat_redirect
#############################################################
# mysql 5.7 表STATISTICS記錄了各個索引的CARDINALITY值
#############################################################
SELECT
t.TABLE_SCHEMA,t.TABLE_NAME,INDEX_NAME, CARDINALITY,
TABLE_ROWS, CARDINALITY/TABLE_ROWS AS SELECTIVITY
FROM
information_schema.TABLES t,
(
SELECT table_schema,table_name,index_name,cardinality
FROM information_schema.STATISTICS
WHERE (table_schema,table_name,index_name,seq_in_index) IN (
SELECT table_schema,table_name,index_name,MAX(seq_in_index)
FROM information_schema.STATISTICS
GROUP BY table_schema , table_name , index_name )
) s
WHERE
t.table_schema = s.table_schema
AND t.table_name = s.table_name AND t.table_rows != 0
AND t.table_schema NOT IN ( 'mysql','performance_schema','information_schema')
ORDER BY SELECTIVITY;
感謝各位的閱讀,以上就是“MySQL5.6怎么快速定位不合理索引”的內容了,經過本文的學習后,相信大家對MySQL5.6怎么快速定位不合理索引這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。