Indexes improve database performance by creating fast access paths for table data. They help retrieve information quickly, especially when working with large datasets. The SHOW INDEXES statement allows users to view and manage the existing indexes of a table.
- Speeds up data retrieval by avoiding full table scans.
- Improves query execution by using optimized search paths.
- Helps analyze existing indexes for better optimization.
- Affects write operations because indexes need maintenance.
Syntax:
SHOW INDEXES FROM table_name;- This command displays all the indexes created for a particular table.
- It providing details such as index name, column names, and more.
Output of SHOW INDEXES
The SHOW INDEXES command provides valuable information about the indexes in a table. Below are the key columns in the output and their meanings:
- table: The name of the table that has information about indexes being shown.
- non_unique: It tells whether the index allows similar values. A value of 1 means repetition is allowed, but with a zero the index makes sure it don't repeat.
- key_name: It tells us the name of the index. The name for primary key indexes is written as 'PRIMARY'.
- seq_in_index: It shows the sequence number of a column in an index. For complex indexes (indexes made up of more than one column), this part shows where the column is in the index.
- column_name: The title of the section that belongs to the index.
- collation: Sets out the way that column is sorted inside the index. 'A' means ascending, 'B' means descending and NULL shows that the column is not sorted.
- cardinality: An estimate of how many unique values are there in the index. Usually, a higher cardinality means better use of indexes for search queries.
- sub_part: Shows how long the part of columns that are just partly indexed is. If NOTHING, the whole column is indexed.
- index_type: Tells what kind of index is used, like BTREE, HASH, about or FULLTEXT.
- visible: Shows if the list is seen by the query optimizer (say 'Yes' if it can be, and not say anything for no).
Example Of SQL Show Indexes
Let’s create a table and add indexes to it, and then use the SHOW INDEXES statement to view index details in SQL. Additionally, we will demonstrate how different types of indexes such as primary, unique, and composite indexes impact the table's performance and indexing strategy.
Step 1: Create the orders Table
CREATE TABLE orders (
order_id INT AUTO_INCREMENT,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
total_amount DECIMAL(10, 2),
PRIMARY KEY(order_id),
INDEX idx_customer_id (customer_id), -- Regular index on customer_id
UNIQUE INDEX unique_order_date (order_date), -- Unique index on order_date
INDEX invisible_index (total_amount) INVISIBLE, -- Invisible index on total_amount
INDEX composite_index (customer_id, order_date) COMMENT 'By customer and order date' -- Composite index on multiple columns
);
Explanation:
This creates the orders table with several indexes: a primary index on order_id, a regular index on customer_id, a unique index on order_date, an invisible index on total_amount, and a composite index on customer_id and order_date.
Step 2: Show Indexes on the orders Table
SHOW INDEXES FROM orders;Output:


- The output shows all the indexes associated with the orders table, along with details like index name, column names, uniqueness, cardinality, and index type.
- It helps you understand how each index is structured and whether it's used for specific queries.