Open In App

SQL Show Indexes

Last Updated : 18 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In databases, indexes are set up to make finding and organizing data in tables faster. They help make searches better by giving faster access to rows in a table. An index is like a data structure linked to a table in the database. It helps the system quickly find and get rows based on columns that are part of it. Indexes play a critical role in optimizing data retrieval from databases.

They work as structured mediators, which increase the effectiveness of search processes by quickly locating specialized data in tables. An index is a separate data structure that accompanies the table, serving as some sort of roadmap or guide to finding rows by referencing values in columns indexed. This structured approach enhances the efficiency of search performance by rendering database queries more efficient and faster.

Key points about indexes

  1. Faster Data Retrieval: Indexes work like an index in a book, helping the database find certain rows fast without checking everything.
  2. Indexed Columns: Indexes are made on one or more columns in a table. Queries about these columns can get help from using indexes.
  3. Trade-offs: While indexes make it quicker to get data, they also need more space for storage and extra work. Updates, inserts, and deletes could take more time because indexes need to be updated too.
  4. Types of Indexes: Some types of indexes that databases can use include B-tree, Hash and Bitmap. Each one works best for particular uses or kinds of questions being asked.
  5. Primary and Secondary Indexes: A primary index can be used to find rows in a table easily, usually by using the primary key column. Secondary indexes are extra lists made on other columns than the main key to find things quicker using different rules.
  6. Query Optimization: Database systems use indexes to speed up how they run queries. They look at all the index choices and pick the best way to get data quickly.
  7. Index Selection: Picking the best columns to index is very important. Indexes should be made on columns often used in search parts, but not too much. This is because having lots of indexes can slow down performance.
  8. Maintenance: regular care is needed for indexes to make sure they work well. This means fixing or changing indexes, especially after major data adjustments.

SQL Show Indexes

In the world of relational databases, optimizing query performance is a crucial aspect of database management. One powerful tool in achieving this optimization is the use of indexes. Indexes play a vital role in speeding up data retrieval operations by providing a quick lookup mechanism.

Syntax:

The exact command varies depending on the database system you are using:

SHOW INDEXES FROM table_name;

SHOW INDEXES Statement Output

The `SHOW INDEXES` statement gives lots of details about the indexes in a certain table. Here’s a breakdown of the information returned by this statement:

  1. table: The name of the table that has info about indexes being shown.
  2. 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.
  3. key_name: It tells us the name of the index. The name for primary key indexes is written as ‘PRIMARY‘.
  4. 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.
  5. column_name: The title of the section that belongs to the index.
  6. 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.
  7. 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.
  8. sub_part: Shows how long the part of columns that are just partly indexed is. If NOTHING, the whole column is indexed.
  9. packed: This shows how the key is packed. If NULL, it means nothing is packed.
  10. null: Shows if the column that is being looked at can have space (‘YES’ means it can, blank means it cannot).
  11. index_type: Tells what kind of index is used, like BTREE, HASH,about or FULLTEXT.
  12. comment: This part talks about extra details about the index.
  13. index_comment: Displays the comment given for the number when it was made by using the COMMENT feature.
  14. 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

We will create a new table named orders to demonstrate the SHOW INDEXES command in MySQL:

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
);

The following command returns all index information from the contacts table in MySQL:

SHOW INDEXES FROM orders;

or

SHOW INDEX IN orders; 

or

SHOW KEYS FROM orders;

Output:

output

Output

output

Output

Explanation:

This output would show information about the indexes on the orders table, along with details such as index type, uniqueness, column sequence, and comments specified during the creation of the index. Mind that the actual numbers in the Cardinality column would depend on the data in your table.

Conclusion

In Summary, the `SHOW INDEXES` statement SQL provides a detailed picture of indexes on tables in any given database. It summarizes such important factors as index names, uniqueness, columns, cardinality, visibility, and types. The key things to consider are the use of cardinality as an important metric in query optimization, insights on composite indexes, and how you can make your indexes selectively visible or invisible. It is crucial for the effective management of the database and optimization of performance in querying operations to use this statement.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads