Explain Codes LogoExplain Codes Logo

Mysql too many indexes?

sql
indexing-strategy
database-performance
mysql-optimization
Anton ShumikhinbyAnton Shumikhin·Aug 22, 2024
TLDR

Having too many indexes can slow down your INSERT, UPDATE, and DELETE operations. Each index requires maintenance which puts an additional burden on your operations. The EXPLAIN command helps you analyze query performance and evaluate the effectiveness of your indexes. Unneeded indexes can be discovered with this query:

SELECT * FROM sys.schema_unused_indexes;

This will display a list of unused indexes that should be considered for removal. Regular index audits help keep your database optimized and less cluttered.

Indexes: what, when, and why?

Indexes can significantly enhance data retrieval time, but they are not a universal cure for all performance issues. The trick lies in knowing when and what to index. You have to find the right balance between accelerating SELECT operations and not slowing down data manipulation (DML operations).

Criteria for adding indexes

The right strategy for adding indexes depends, among other things, on data selectivity, query patterns, and the specific use case requirements. A column which has many unique values, and frequently appears in your WHERE clause, JOIN operations, ORDER BY, or GROUP BY clauses, is typically a good candidate for indexing. But more isn't always better: remember, MySQL can only utilize one index per SELECT!

Selectivity and order of columns in the index

The order of columns in the index matters, too. A good rule of thumb: put the most selective columns first. This way, MySQL can even use just the prefix of an index, if the other columns are not referenced in a query.

Functioned referenced columns: the quicksand for indexes

Beware the pitfall of using functions on indexed columns – they can render your carefully crafted indexes useless!

The Price of indexing

Like with most things in life, indexes are not free. They speed up reads, but can have a significant impact on DML operations. Each additional index you add slows down INSERTs, UPDATEs, and DELETEs, and takes up extra table space. Here's how to tackle those side effects:

The impact of indexes on DML operations

For tables that experience frequent changes, try to limit the number of indexes. Just think of it like an over-protective parent slowing down a game of UPDATE tag.

The space constraints of indexing

Excessive indexing can also lead to space issues. We all know that space is the final frontier... especially when dealing with MySQL tables and index storage. Always make sure your SELECT speed boost is worth the occupied space.

Make the most of your indexes: Best practices

There is no "create index and forget" concept. Optimizing your indexing strategy requires regular attention, reflection, and course corrections. Here are some of the best practices:

Index Usage Monitoring

Use your introspection tools. Regularly run the pt-duplicate-key-checker and pt-index-usage commands to avoid redundancy and remove obsolete indexes. It's like spring cleaning for your database.

# When database cleanliness becomes all-consuming! pt-duplicate-key-checker

Beyond Indexing: Improved database model design

No amount of smart indexing can make up for a poorly designed database. A well-structured database design is ALWAYS the first step towards optimized performance.

Watch your balance: Not everything-or-nothing

Try to avoid either extreme: The "index everything approach" or the "shyness towards indexes".