共计 1971 个字符,预计需要花费 5 分钟才能阅读完成。
# 若 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;
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-05/131115.htm