performancesqliteprimary-keyindexing

Primary key effect on performance


In my SQLite database I store backup jobs. Each run it increases by ~25MB for adding ~32,000 entries to a "map table" used to link another table.

It has a primary key (autoincrement int) that I don't use. SQLite will reserve 1, 2, 4, or 8 byte for an INT column (depending on its value). This table has 3 additional columns, also of INT type. I've added indexes to the columns that I use in WHERE clauses.

In the presence of indexes in the situation described, do primary keys have any benefit in terms of performance? Performance is important but not to the extent that 10ms saved on a 32,000 entry job means an additional 10MB of data.


Solution

  • A primary key index is used to look up a row for a given primary key. It is also used to ensure that the primary key values are unique.

    If you search your data using other columns, the primary key index will not be used, and as such will yield no performance benefit. Its mere existence should not have a negative performance impact either, though.

    An unnecessary index wastes disk space, and makes INSERT and UPDATE statements execute slower. It should have no negative impact on query performance.