Is there any such thing as "too many indexes" when it comes to speed in SQLite3?

StackOverflow https://stackoverflow.com/questions/23655254

  •  22-07-2023
  •  | 
  •  

سؤال

I wanted to improve the performance on my SQLite3 database. I went with the most extreme course of action first (just to see what would happen) and added an index to every column of every table in the database.

The database size more than doubled, and to my surprise, performance dropped drastically. Where I had previously gotten 4000 selects per second I now get ~50 selects per second.

This question is not specifically about my case. My question is; is it possible that adding indexes will decrease SELECT performance in SQLite3? I'm asking because I want to know if my problem is that I added too many indexes, or if I've made a mistake somewhere that is causing the slowdown.


To be more specific about my case: the database increased from 140 MB to 280 MB and I have an SSD.

هل كانت مفيدة؟

المحلول

There a mechanisms by which additional indexes could cause a slowdown:

  1. Most optimization decisions are designed for the worst case – when you're accessing data that is too large to fit into any cache and has to be loaded from disk.

    If the data itself fits into the caches, but all the various indexes used by your queries are so large that the entire working set becomes too large, you will get more swapping.

  2. SELECT queries will ignore any indexes that are not actually used. However, INSERT/UPDATE/DELETE statements must update all indexes of the changed table, so every additional index will slow down such changes.

Use EXPLAIN QUERY PLAN to check which indexes are actually used by a query.

Read Query Planning and The SQLite Query Planner to understand how indexes can be used.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top