时间:2021-07-01 10:21:17 帮助过:4人阅读
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‘
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 syntax is tbl_name FROM db_namedb_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.
Prefix limits are measured in bytes. However, prefix lengths for index specifications in CREATE TABLE, ALTER TABLE, and CREATE INDEX statements are interpreted as number of characters for nonbinary string types (CHAR, VARCHAR, TEXT) and number of bytes for binary string types (BINARY, VARBINARY, BLOB). 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 (BTREE, FULLTEXT, HASH, RTREE).
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.
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”.
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.
Prefix limits are measured in bytes. However, prefix lengths for index specifications in CREATE TABLE, ALTER TABLE, and CREATE INDEX statements are interpreted as number of characters for nonbinary string types (CHAR, VARCHAR, TEXT) and number of bytes for binary string types (BINARY, VARBINARY, BLOB). 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 (BTREE, FULLTEXT, HASH, RTREE).
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.
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
分析mysql索引信息及空间占用
标签:参考 value ocs add where lob cts spec eve