Wednesday, 27 November 2019

MySQL SHOW INDEXES

Summary: in this tutorial, you will learn how to query index information from a table by using the MySQL SHOW INDEXES command.

Introduction to MySQL SHOW INDEXES command

To query the index information of a table, you use the SHOW INDEXES statement as follows:
To get the index of a table, you specify the table name after the FROM keyword. The statement will return the index information associated with the table in the current database.
You can specify the database name if you are not connected to any database or you want to get the index information of a table in a different database:
The following query is similar to the one above:
Note that INDEX and KEYS are the synonyms of the INDEXESIN is the synonym of the FROM, therefore, you can use these synonyms in the SHOW INDEXES column instead. For example:
Or
The SHOW INDEXES returns the following information:

table

The name of the table

non_unique

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

key_name

The name of the index. The primary key index always has the name of PRIMARY.

seq_in_index

The column sequence number in the index. The first column sequence number starts from 1.

column_name

The column name

collation

Collation represents how the column is sorted in the index. A means ascending, B means descending, or NULL means not sorted.

cardinality

The cardinality returns an estimated number of unique values in the index.
Note that the higher the cardinality, the greater the chance that the query optimizer uses the index for lookups.

sub_part

The index prefix. It is null if the entire column is indexed. Otherwise, it shows the number of indexed characters in case the column is partially indexed.

packed

indicates how the key is packed; NUL if it is not.

null

YES if the column may contain NULL values and blank if it does not.

index_type

represents the index method used such as BTREEHASHRTREE, or FULLTEXT.

comment

The information about the index not described in its own column.

index_comment

shows the comment for the index specified when you create the index with the COMMENT attribute.

visible

Whether the index is visible or invisible to the query optimizer or not; YES if it is, NO if not.

expression

If the index uses an expression rather than column or column prefix value, the expression indicates the expression for the key part and also the column_name column is NULL.

Filter index information

To filter index information, you use a WHERE clause as follows:
You can use any information returned by the SHOW INDEXES statement to filter the index information. For example, the following statement returns only the invisible indexes of a table:

MySQL SHOW INDEXES examples

We will create a new table named contacts to demonstrate the SHOW INDEXES command:
The following command returns all index information from the contacts table:
The output is:
MySQL SHOW INDEXES - get all indexes
To get the invisible indexes of the contacts table, you add a WHERE clause as follows:
Here is the output:
MySQL SHOW INDEXES - get invisible indexes
In this tutorial, you have learned how to use the MySQL SHOW INDEXES statement to get the index information of a table.

0 comments:

Post a Comment