MySQL 5.6 quickly locates unreasonable Indexes

Source: Internet
Author: User

MySQL 5.6 quickly locates unreasonable Indexes

# If CARDINALITY/TABLE_ROWS is less than 10% (experience value), it indicates a high data repetition rate. Generally, you need to consider whether it is necessary to create this index.

######################################## #################
# Mysql 5.6
Currently, the Cardinality STATISTICS in the 5.6 STATISTICS table is incorrect !!! VisibleMySQL bugs #78066.
However, the Cardinality statistics in the innodb_index_stats table are still correct.
######################################## #################
SELECT
T. TABLE_SCHEMA,
T. TABLE_NAME, INDEX_NAME,
S. CARDINALITY,
T. TABLE_ROWS,
S. CARDINALITY/t. TABLE_ROWS AS selecti.pdf
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 _ scheme', 'information _ scheme ')
Order by selecti.pdf;

From: http://mp.weixin.qq.com/s? _ Biz = MjM5MjIxNDA4NA ==& mid = 401131835 & idx = 1 & sn = 37c5fd9d3d8670fb1_a1e0565e50eeb & scene = 0 # wechat_redirect
######################################## #####################
# Mysql 5.7 table STATISTICS records the CARDINALITY values of each index
######################################## #####################

SELECT
T. TABLE_SCHEMA, t. TABLE_NAME, INDEX_NAME, CARDINALITY,
TABLE_ROWS, CARDINALITY/TABLE_ROWS AS selecti.pdf
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 _ scheme', 'information _ scheme ')
Order by selecti.pdf;

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.