分析mysql索引信息及空间占用

查询索引信息

show index from db.table;
select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,CARDINALITY from information_schema.STATISTICS iss
where iss.table_name=‘table‘;

查询表及索引占用空间

select * from information_schema.TABLES
where information_schema.TABLES.TABLE_SCHEMA=‘db‘ and information_schema.TABLES.TABLE_NAME=‘table‘


参考

https://dev.mysql.com/doc/refman/8.0/en/show-index.html

13.7.7.22 SHOW INDEX Statement

SHOW [EXTENDED] {INDEX | INDEXES | KEYS}
    {FROM | IN} tbl_name
    [{FROM | IN} db_name]
    [WHERE expr]

SHOW INDEX returns table index information. The format resembles that of the SQLStatistics call in ODBC. This statement requires some privilege for any column in the table.

mysql> SHOW INDEX FROM City\G
*************************** 1. row ***************************
        Table: city
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: ID
    Collation: A
  Cardinality: 4188
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
*************************** 2. row ***************************
        Table: city
   Non_unique: 1
     Key_name: CountryCode
 Seq_in_index: 1
  Column_name: CountryCode
    Collation: A
  Cardinality: 232
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL

An alternative to tbl_name FROM db_name syntax is db_name.tbl_name. These two statements are equivalent:

SHOW INDEX FROM mytable FROM mydb;
SHOW INDEX FROM mydb.mytable;

The optional EXTENDED keyword causes the output to include information about hidden indexes that MySQL uses internally and are not accessible by users.

The WHERE clause can be given to select rows using more general conditions, as discussed in Section 25.49, “Extensions to SHOW Statements”.

SHOW INDEX returns the following fields:

  • Table

    The name of the table.

  • Non_unique

    0 if the index cannot contain duplicates, 1 if it can.

  • Key_name

    The name of the index. If the index is the primary key, the name is always PRIMARY.

  • Seq_in_index

    The column sequence number in the index, starting with 1.

  • Column_name

    The column name. See also the description for the Expression column.

  • Collation

    How the column is sorted in the index. This can have values A (ascending), D (descending), or NULL (not sorted).

  • Cardinality

    An estimate of the number of unique values in the index. To update this number, run ANALYZE TABLE or (for MyISAM tables) myisamchk -a.

    Cardinality is counted based on statistics stored as integers, so the value is not necessarily exact even for small tables. The higher the cardinality, the greater the chance that MySQL uses the index when doing joins.

  • Sub_part

    The index prefix. That is, the number of indexed characters if the column is only partly indexed, NULL if the entire column is indexed.

    Note

    Prefix limits are measured in bytes. However, prefix lengths for index specifications in CREATE TABLEALTER TABLE, and CREATE INDEX statements are interpreted as number of characters for nonbinary string types (CHARVARCHARTEXT) and number of bytes for binary string types (BINARYVARBINARYBLOB). Take this into account when specifying a prefix length for a nonbinary string column that uses a multibyte character set.

    For additional information about index prefixes, see Section 8.3.5, “Column Indexes”, and Section 13.1.15, “CREATE INDEX Statement”.

  • Packed

    Indicates how the key is packed. NULL if it is not.

  • Null

    Contains YES if the column may contain NULL values and ‘‘ if not.

  • Index_type

    The index method used (BTREEFULLTEXTHASHRTREE).

  • Comment

    Information about the index not described in its own column, such as disabled if the index is disabled.

  • Index_comment

    Any comment provided for the index with a COMMENT attribute when the index was created.

  • Visible

    Whether the index is visible to the optimizer. See Section 8.3.12, “Invisible Indexes”.

  • Expression

    MySQL 8.0.13 and higher supports functional key parts (see Functional Key Parts), which affects both the Column_name and Expression columns:

    • For a nonfunctional key part, Column_name indicates the column indexed by the key part and Expression is NULL.
    • For a functional key part, Column_name column is NULL and Expression indicates the expression for the key part.

Information about table indexes is also available from the INFORMATION_SCHEMA STATISTICS table. See Section 25.32, “The INFORMATION_SCHEMA STATISTICS Table”. The extended information about hidden indexes is available only using SHOW EXTENDED INDEX; it cannot be obtained from the STATISTICS table.

You can list a table‘s indexes with the mysqlshow -k db_name tbl_name command.


https://dev.mysql.com/doc/refman/8.0/en/optimizer-statistics.html

25.32 The INFORMATION_SCHEMA STATISTICS Table

The STATISTICS table provides information about table indexes.

Columns in STATISTICS that represent table statistics hold cached values. The information_schema_stats_expiry system variable defines the period of time before cached table statistics expire. The default is 86400 seconds (24 hours). If there are no cached statistics or statistics have expired, statistics are retrieved from storage engines when querying table statistics columns. To update cached values at any time for a given table, use ANALYZE TABLE. To always retrieve the latest statistics directly from storage engines, set information_schema_stats_expiry=0. For more information, see Section 8.2.3, “Optimizing INFORMATION_SCHEMA Queries”.

Note

If the innodb_read_only system variable is enabled, ANALYZE TABLE may fail because it cannot update statistics tables in the data dictionary, which use InnoDB. For ANALYZE TABLE operations that update the key distribution, failure may occur even if the operation updates the table itself (for example, if it is a MyISAM table). To obtain the updated distribution statistics, set information_schema_stats_expiry=0.

The STATISTICS table has these columns:

  • TABLE_CATALOG

    The name of the catalog to which the table containing the index belongs. This value is always def.

  • TABLE_SCHEMA

    The name of the schema (database) to which the table containing the index belongs.

  • TABLE_NAME

    The name of the table containing the index.

  • NON_UNIQUE

    0 if the index cannot contain duplicates, 1 if it can.

  • INDEX_SCHEMA

    The name of the schema (database) to which the index belongs.

  • INDEX_NAME

    The name of the index. If the index is the primary key, the name is always PRIMARY.

  • SEQ_IN_INDEX

    The column sequence number in the index, starting with 1.

  • COLUMN_NAME

    The column name. See also the description for the EXPRESSION column.

  • COLLATION

    How the column is sorted in the index. This can have values A (ascending), D (descending), or NULL (not sorted).

  • CARDINALITY

    An estimate of the number of unique values in the index. To update this number, run ANALYZE TABLE or (for MyISAM tables) myisamchk -a.

    CARDINALITY is counted based on statistics stored as integers, so the value is not necessarily exact even for small tables. The higher the cardinality, the greater the chance that MySQL uses the index when doing joins.

  • SUB_PART

    The index prefix. That is, the number of indexed characters if the column is only partly indexed, NULL if the entire column is indexed.

    Note

    Prefix limits are measured in bytes. However, prefix lengths for index specifications in CREATE TABLEALTER TABLE, and CREATE INDEX statements are interpreted as number of characters for nonbinary string types (CHARVARCHARTEXT) and number of bytes for binary string types (BINARYVARBINARYBLOB). Take this into account when specifying a prefix length for a nonbinary string column that uses a multibyte character set.

    For additional information about index prefixes, see Section 8.3.5, “Column Indexes”, and Section 13.1.15, “CREATE INDEX Statement”.

  • PACKED

    Indicates how the key is packed. NULL if it is not.

  • NULLABLE

    Contains YES if the column may contain NULL values and ‘‘ if not.

  • INDEX_TYPE

    The index method used (BTREEFULLTEXTHASHRTREE).

  • COMMENT

    Information about the index not described in its own column, such as disabled if the index is disabled.

  • INDEX_COMMENT

    Any comment provided for the index with a COMMENT attribute when the index was created.

  • IS_VISIBLE

    Whether the index is visible to the optimizer. See Section 8.3.12, “Invisible Indexes”.

  • EXPRESSION

    MySQL 8.0.13 and higher supports functional key parts (see Functional Key Parts), which affects both the COLUMN_NAME and EXPRESSION columns:

    • For a nonfunctional key part, COLUMN_NAME indicates the column indexed by the key part and EXPRESSION is NULL.
    • For a functional key part, COLUMN_NAME column is NULL and EXPRESSION indicates the expression for the key part.

Notes

  • There is no standard INFORMATION_SCHEMA table for indexes. The MySQL column list is similar to what SQL Server 2000 returns for sp_statistics, except that QUALIFIER and OWNER are replaced with CATALOG and SCHEMA, respectively.

Information about table indexes is also available from the SHOW INDEX statement. See Section 13.7.7.22, “SHOW INDEX Statement”. The following statements are equivalent:

SELECT * FROM INFORMATION_SCHEMA.STATISTICS
  WHERE table_name = ‘tbl_name‘
  AND table_schema = ‘db_name‘

SHOW INDEX
  FROM tbl_name
  FROM db_name

原文地址:https://www.cnblogs.com/bensonwei/p/12665928.html

时间: 2024-10-28 22:19:14

分析mysql索引信息及空间占用的相关文章

深入浅出分析MySQL索引设计背后的数据结构

在我们公司的DB规范中,明确规定: 1.建表语句必须明确指定主键 2.无特殊情况,主键必须单调递增 对于这项规定,很多研发小伙伴不理解.本文就来深入简出地分析MySQL索引设计背后的数据结构和算法,从而可以帮你释疑如下问题: 1.为什么innodb表需要主键? 2.为什么建议innodb表主键是单调递增? 3.为什么不建议innodb表主键设置过长? B-tree(多路搜索树,并不是二叉的)是一种常见的数据结构.使用B-tree结构可以显著减少定位记录时所经历的中间过程,从而加快存取速度.B通常

干货篇:一篇文章让你——《深入解析MySQL索引原理 》

概述 最近一段时间重新深入研究了一遍MySQL的内容,今天主要分享分析MySQL索引原理,后续会输出一些关于MySQL方面的干货,希望各位小伙伴喜欢.一.什么是索引.为什么要建立索引? 关于索引的理解,个人更加喜欢将其比喻为字典里面的目录,根据字典来进行查询的速度远大于每一页逐个逐个字排查的速度. 索引主要用于快速找出在某个列中有特定值的行,倘若不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行,表越大,查询数据所花费的时间就越多.如果表中查询的列有一个索引,MySQL能够快

MYSQL索引分析和优化设计方案

一.什么是索引? 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时 MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的记录数量越多,这个操作的代价就越高.如果作为搜索条件的列上已 经创建了索引,MySQL无需扫描任何记录即可迅速得到目标记录所在的位置.如果表有1000个记录,通过索引查找记录至少要比顺序扫描记录快100倍. 假设我们创建了一个名为people的表: CREATE TABLE people (

Mysql 索引优化分析

MySQL索引优化分析 为什么你写的sql查询慢?为什么你建的索引常失效?通过本章内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字段的意义.助你了解索引,分析索引,使用索引,从而写出更高性能的sql语句.还在等啥子?撸起袖子就是干! 案例分析 我们先简单了解一下非关系型数据库和关系型数据库的区别. MongoDB是NoSQL中的一种.NoSQL的全称是Not only SQL,非关系型数据库.它的特点是性能高,扩张性强,模式灵

mySql索引优化分析

MySQL索引优化分析 为什么你写的sql查询慢?为什么你建的索引常失效?通过本章内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字段的意义.助你了解索引,分析索引,使用索引,从而写出更高性能的sql语句.还在等啥子?撸起袖子就是干! 案例分析 我们先简单了解一下非关系型数据库和关系型数据库的区别.MongoDB是NoSQL中的一种.NoSQL的全称是Not only SQL,非关系型数据库.它的特点是性能高,扩张性强,模式灵活

转:由浅入深探究mysql索引结构原理、性能分析与优化

摘要: 第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1. 简单介绍B-tree B+ tree树 2. MyisAM索引结构 3. Annode索引结构 4. MyisAM索引与InnoDB索引相比较 第三部分:MYSQL优化 1.表数据类型选择 2.sql语句优化 (1)     最左前缀原则 (1.1)  能正确的利用索引 (1.2)  不能正确的利用索引 (1.3)  如果一个查询where子句中确实不需要password列,那就用“补洞”. (1.4)  like

MySQL索引类型 & Mysql索引会失效的几种情况分析

MySQL索引类型介绍 (1)普通索引 这是最基本的索引,它没有任何限制.它有以下几种创建方式: CREATE INDEX indexName ON mytable(username(length)); 如果是CHAR,VARCHAR类型,length可以小于字段实际长度:如果是BLOB和TEXT类型,必须指定 length,下同. ALTER mytable ADD INDEX [indexName] ON (username(length)) CREATE TABLE mytable( ID

Mysql索引分析:适合建索引?不适合建索引?【转】

数据库建立索引常用的规则如下: 1.表的主键.外键必须有索引: 2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特别是大表的字段,应该建立索引: 5.索引应该建在选择性高的字段上: 6.索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引: 7.复合索引的建立需要进行仔细分析:尽量考虑用单字段索引代替: A.正确选择复合索引中的主列字段,一般是选择性较好的字段: B.复合索引的几个字段是否经常同时以A

【转】由浅入深探究mysql索引结构原理、性能分析与优化

摘要: 第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1.简单介绍B-tree B+ tree树 2.MyisAM索引结构 3.Annode索引结构 4.MyisAM索引与InnoDB索引相比较 第三部分:MYSQL优化 1.表数据类型选择 2.sql语句优化 (1)     最左前缀原则 (1.1)  能正确的利用索引 (1.2)  不能正确的利用索引 (1.3)  如果一个查询where子句中确实不需要password列,那就用“补洞”. (1.4)  like (2)