9๏ธโƒฃ ๐Ÿ“Œ MySQL Indexing
Estimated reading: 4 minutes 32 views

๐Ÿ“Œ MySQL Create / Drop / Show Indexes โ€“ Manage Indexes for Performance


๐Ÿงฒ Introduction โ€“ Why Manage Indexes in MySQL?

Indexes play a crucial role in improving the speed of SELECT queries, especially when dealing with large datasets. Creating the right indexes reduces lookup time, while dropping unused ones can improve write performance and reduce storage use.

Managing indexes effectively includes:

  • Creating indexes on frequently queried columns
  • Dropping unnecessary or duplicate indexes
  • Reviewing existing indexes using SHOW INDEXES

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to create, drop, and list indexes in MySQL
  • Syntax for unique, composite, and prefix indexes
  • When to use and not use indexes
  • Best practices for index management

๐Ÿ› ๏ธ CREATE INDEX โ€“ Add an Index to a Table

๐Ÿ”น Syntax

CREATE [UNIQUE] INDEX index_name ON table_name (column1 [, column2, ...]);
  • UNIQUE: Optional keyword to enforce uniqueness
  • index_name: Custom name for the index
  • column1, column2: Columns to include in the index

๐Ÿ”น Example 1: Basic Index

CREATE INDEX idx_email ON users(email);

Explanation:
Creates a regular (non-unique) index on the email column in the users table.


๐Ÿ”น Example 2: Unique Index

CREATE UNIQUE INDEX idx_username ON users(username);

Explanation:
Prevents duplicate values in the username column while improving lookup speed.


๐Ÿ”น Example 3: Composite Index (Multi-Column)

CREATE INDEX idx_city_age ON users(city, age);

Explanation:
Optimizes queries using both city and age, especially in combination.


๐Ÿ”น Example 4: Prefix Index (for VARCHAR/TEXT)

CREATE INDEX idx_title ON articles(title(100));

Explanation:
Indexes the first 100 characters of the title columnโ€”required when indexing long text columns.


๐Ÿ” SHOW INDEXES โ€“ View Existing Indexes

๐Ÿ”น Syntax

SHOW INDEXES FROM table_name;

๐Ÿ”น Example

SHOW INDEXES FROM users;

Explanation:
Displays all indexes on the users table, including:

  • Key_name: Name of the index
  • Column_name: Columns in the index
  • Non_unique: Whether duplicates are allowed (0 = unique, 1 = not unique)
  • Seq_in_index: Order of columns in composite indexes
  • Index_type: B-tree, FULLTEXT, etc.

โŒ DROP INDEX โ€“ Remove an Existing Index

๐Ÿ”น Syntax

DROP INDEX index_name ON table_name;

๐Ÿ”น Example

DROP INDEX idx_email ON users;

Explanation:
Removes the idx_email index from the users table, freeing storage and reducing update overhead.


๐Ÿง  Index Management Summary

TaskCommand Example
Create basic indexCREATE INDEX idx_col ON table(col);
Create unique indexCREATE UNIQUE INDEX idx_col ON table(col);
Create composite indexCREATE INDEX idx_multi ON table(col1, col2);
Drop indexDROP INDEX idx_col ON table;
Show all indexesSHOW INDEXES FROM table;

๐Ÿ“˜ Best Practices

โœ… Tip๐Ÿ’ก Why It Matters
Name your indexes meaningfullyEasier to identify and manage later
Use EXPLAIN to validate index usageEnsures your indexes are actually being used
Drop unused indexesSaves space and improves write speed
Use composite indexes for multi-column filtersReduces query execution time
Avoid duplicate or overlapping indexesWastes space and complicates query planner

๐Ÿš€ Real-World Use Cases

ScenarioAction Taken
Fast user login by emailCREATE INDEX idx_email ON users(email);
Remove outdated legacy indexDROP INDEX idx_old_field ON reports;
Audit current indexes in ordersSHOW INDEXES FROM orders;
Add search optimization to tagsCREATE INDEX idx_tags ON articles(tags);
Optimize multi-filter queryCREATE INDEX idx_city_age ON users(city, age);

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

MySQL index management helps ensure your queries are fast, your schema is clean, and your writes are efficient. Use CREATE, SHOW, and DROP INDEX wisely to balance performance vs. overhead.

๐Ÿ” Key Takeaways

  • Use CREATE INDEX to speed up searches, joins, and filters
  • Use SHOW INDEXES to audit table indexes
  • Use DROP INDEX to remove unnecessary or unused indexes
  • Choose UNIQUE when you want to enforce value uniqueness
  • Test with EXPLAIN to verify effectiveness

โš™๏ธ Real-World Relevance

Proper index management is crucial in e-commerce filters, search engines, user management, analytics, and any app with growing data volume.


โ“ FAQ โ€“ Create, Drop, and Show Index in MySQL


โ“ Can I create multiple indexes on one table?

โœ… Yes, on different columns or combinations of columns.


โ“ Can I drop the PRIMARY KEY with DROP INDEX?

โŒ No. Use:

ALTER TABLE table_name DROP PRIMARY KEY;

โ“ Can I index a TEXT or BLOB column?

โœ… Yes, but you must use a prefix length:

CREATE INDEX idx_summary ON articles(summary(255));

โ“ Does indexing always improve performance?

โš ๏ธ No. Over-indexing increases insert/update cost. Index only when needed.


โ“ Whatโ€™s the max number of indexes per table?

MySQL supports up to 64 indexes per table.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“ MySQL Create / Drop / Show Indexes

Or Copy Link

CONTENTS
Scroll to Top