What are the disadvantages of database indexes? — PlanetScale (2024)

If you've worked with databases for a while, you've probably learned that adding indexes can improve performance. This is especially true for large tables when you are querying with JOINs, GROUP BY, WHERE, or ORDER BY clauses.

An index basically works by storing a copy of part of the data in a different order, so that it can be accessed more quickly — kind of like adding a table of contents to a book.

For a more detailed explanation of how indexes work and how you can use them, check out this article: How do database indexes work? If you want to dive even further to indexes, we have 17 videos on indexing that cover everything from how indexes and B+Trees work to knowing where and when to add indexes.

Making good use of indexes can reduce query run time from seconds to milliseconds. The first time you get a performance boost like that, you might feel inclined to add indexes to every column of every table in your database just because you can. But this is not always a good idea, as there can be drawbacks to adding too many secondary indexes.

Note

You should always include a primary index on every table in your database. However, too many secondary indexes can begin to cause issues in some instances. This article covers issues that come with too many secondary indexes.

Downsides of database indexes

Let's go over some of the possible downsides of using too many database secondary indexes.

Additional storage

The first and perhaps most obvious drawback of adding indexes is that they take up additional storage space. The exact amount of space depends on the size of the table and the number of columns in the index, but it's usually a small percentage of the total size of the table. A basic index only needs to store the values of the indexed columns as well as a pointer to the row in the table. So for a column that contains integers, the index will only need to store the integer values. This space will increase if the column contains strings because the index will need to store the string values as well as the length of each string.

This is important to consider if you have large datasets, as adding multiple indexes to a table can quickly use a significant amount of additional storage space.

Slower writes

When you add an index, it has to be updated whenever a row is inserted, updated, or deleted. This means that writes will be slower. Before you add an index, you should consider whether you will be doing a lot of writes to the table and whether or not you can afford to slow down the writes.

As an example, in one application I worked on, doing a bulk insert of about a million records only took around 10-15 seconds without any indexes. Unfortunately, the performance of certain frequently used queries was quite slow, taking a few seconds to run and causing a bad user experience. Adding several indexes for such queries improved the performance significantly, but the bulk insert now takes closer to two minutes. That is a significant difference in write performance, but in this particular case, it was an acceptable trade-off, as the bulk insert is done infrequently and can be done during off-peak hours when the application is not used heavily.

If something like this bulk insert was triggered by users who had to sit and wait for it, then it might be a different story, and I may have weighted the impact of the slower writes differently.

Finding and removing unused indexes

To keep your database efficient, it's important to find and remove any unused indexes. In MySQL, you can use the following query to find indexes that are not being used (replace your_database_name with the name of your database):

SQL

SELECT table_name, index_name, non_unique, seq_in_index, column_name, collation, cardinality, sub_part, packed, index_type, comment, index_comment

FROM information_schema.STATISTICS

WHERE table_schema = 'your_database_name'

AND index_name != 'PRIMARY'

AND (cardinality IS NULL OR cardinality = 0)

ORDER BY table_name, index_name, seq_in_index;

This query checks the cardinality of each index, which is the number of unique values in the index. If this value is 0, then the index is not being used.

If you find an unused index, your_index_name, in a table called your_table_name, you could remove it with the following query:

SQL

ALTER TABLE your_table_name DROP INDEX your_index_name;

Auditing all indexes in a database

If you have some indexes that are in use, but after reading this article, you think some of the trade-offs may not be worth it, you can audit each of these individually to see if you want to keep or remove them.

To get a list of all indexes for all tables in your database, run:

SQL

SELECT * FROM information_schema.statistics;

Now that you've identified all of your indexes, you can use MySQL invisible indexes to determine which ones you may wish to drop.

Using invisible indexes to test dropping an index

One way to test the outcome of dropping an index before actually dropping it is to utilize MySQL's invisible indexes.

(If you prefer video, you can watch our video on invisible indexes.)

With invisible indexes, you can keep the index intact but essentially hide the index from MySQL so that queries do not use the index. This gives you a way to quickly test the impact of removing an index without completely destroying it.

Tip

You can use PlanetScale Insights to quickly see the performance (rows read, rows returned, total time, time per query, etc.) of any query in your database. This is a quick and easy way to test performance before and after making an index invisible.

To make an index invisible, run the following query:

SQL

ALTER TABLE your_table_name;

ALTER INDEX your_index_name INVISIBLE;

You can now run any applicable queries to see how performance is impacted. If you realize you still need this index, you can make it visible again with:

SQL

ALTER TABLE your_table_name;

ALTER INDEX your_index_name VISIBLE;

Note

With PlanetScale, we don't allow direct DDL on production branches, unless they have safe migrations disabled (not recommended). So, you’ll have to go through the deploy request process to test using invisible indexes. However, with our Revert feature, you can simply click the "Revert" button if you decide you want to undo an altered or dropped index and it will be reverted near instantaneously.

When doing this, ensure that you test the performance of any affected queries before and after removing the index to make sure that you are not inadvertently making things worse.

Conclusion

Adding indexes can be a great way to improve performance, but it's important to be aware that they do come with a cost. Every index takes up additsional storage, can slow down write operations, and can complicate the query optimizer's job, so they aren't always guaranteed to improve performance. Ultimately the decision to add indexes should be based on the specific needs of your application and the trade-offs you are willing to make. You should always measure the performance of your queries before and after adding indexes to see if they are actually improving performance, and if you don't seem to be seeing significant improvement for your desired use, then it may be better to leave the indexes out.

Want a performant MySQL database that branches, scales, and reverts?

Sign up for PlanetScale now
What are the disadvantages of database indexes? — PlanetScale (2024)

FAQs

What are the disadvantages of database indexes? — PlanetScale? ›

Slower writes

What is the advantage disadvantage index? ›

IRSAD is a general measure of advantage and disadvantage, summarising the economic and social conditions of people and households. It ranks areas from most disadvantaged to most advantaged. The score calculation includes factors such as income, education and employment.

What is one of the downsides of creating an index in PostgreSQL? ›

Disadvantages of Indexes in PostgreSQL
  • Increased storage requirement: The most obvious downside of utilizing indexes is the extra storage space they require. ...
  • Slower write operations: Every time a row is inserted, updated, or deleted, the index must be updated too.

What are the negative effects of creating unnecessary indexes? ›

However, each index you create has a negative performance impact on writes and requires some disk space. Creating unnecessary indexes leads to a bloated collection and slow writes. Consider each query your application performs and whether it justifies an index.

How can indexes negatively impact database efficiency? ›

While indexes are invaluable for improving query performance, there are downsides. Every index added to a table increases the storage space required by the database. Additionally, indexes can slow down write operations such as INSERT, UPDATE, and DELETE, as each operation requires updating the indexes.

What are index disadvantages? ›

The first and perhaps most obvious drawback of adding indexes is that they take up additional storage space. The exact amount of space depends on the size of the table and the number of columns in the index, but it's usually a small percentage of the total size of the table.

What are database indexing's advantages and disadvantages? ›

The advantage is better performance when using the index (mostly for reading, but also for updating other values when using the index). The disadvantage is slightly slower performance when adding or deleting records, or when updating the indexed columns.

When should indexes be avoided? ›

While indexes are designed to improve the efficiency of a database, there are occasions where they should be avoided, for instance:
  1. On small tables.
  2. Tables that receive a lot of big batch updates or inserts.
  3. Columns that have large numbers of null values.
  4. Columns that are frequently manipulated.
Feb 23, 2023

When should you not create an index? ›

When Should Indexes Be Avoided?
  1. Indexes should not be used on small tables.
  2. Indexes should not be used on columns that return a high percentage of data rows when used as a filter condition in a query's WHERE clause. ...
  3. Tables that have frequent, large batch update jobs run can be indexed.

When should indexes be avoided in PostgreSQL? ›

Indexes should not be used on small tables. Tables that have frequent, large batch update or insert operations. Indexes should not be used on columns that contain a high number of NULL values.

What are the disadvantages of index options? ›

Potential for Losses: Like any investment, there's a risk of losing the entire premium paid for index options. Market Volatility: Index options are highly sensitive to market volatility, which can amplify both potential gains and losses.

What is the problem with too many indexes on a table? ›

More indexes may mean that more different queries will run fast but inserts, updates, and deletes will be slower. So, depends on the workload mix. Slower inserts/updates/deletes as the indexes have to be updated as well as the table. Wasted disk/memory space as well.

What are the disadvantages of index selection? ›

However, its major disadvantage is that the weightings assigned to each characteristic are inherently quite hard to calculate precisely and so require some elements of trial and error before they become optimal to the breeder.

What are the cons of indexing in MySQL? ›

Disadvantages of MySQL indexes

In the case when a table is of large table size, the index file could reach the operating system's maximum file size. Secondly, the indexes slow down the speed of writing queries, such as INSERT, UPDATE and DELETE.

What are negative indexes and how are they used? ›

What are negative indices? Negative indices are powers (also called exponents) with a minus sign in front of them. E.g. We get negative indices by dividing two terms with the same base where the first term is raised to a power that is smaller than the power that the second term is raised to.

Do indexes make queries faster? ›

What is Indexing? Indexing makes columns faster to query by creating pointers to where data is stored within a database. Imagine you want to find a piece of information that is within a large database. To get this information out of the database the computer will look through every row until it finds it.

What are the advantages of an index? ›

Key Takeaways. An index gives a quick measure of the state of a market. Index funds are a low-cost way to invest, provide better returns than most fund managers, and help investors to achieve their goals more consistently.

What is index number advantages and disadvantages? ›

The index numbers show the approximate indications of the relative changes that occur. Moreover, the changes in variables that are compared over a prolonged time may fall short on reliability. The selection of representative commodities may be skewed. It is since these commodities are based on samples.

What are the advantages and disadvantages of index file? ›

The main advantage of indexed file access is its speed and efficiency for random and sequential access operations. But, its main disadvantage is that it requires additional storage space for the index, which can increase the cost and complexity of the system.

What are the advantages of index model? ›

It helps to examine the risk and returns of individual securities within a portfolio. Investors can use this model to make well-informed trading decisions regarding portfolio diversification and risk management. It is a valuable instrument for asset pricing, portfolio management, and risk analysis.

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6396

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.