Home > Enterprise >  Do I need to create index for PK in SQLite tables?
Do I need to create index for PK in SQLite tables?

Time:11-28

If I have a single column integer PK on a SQLite table - do I need to create explicit index or is it take care of by the engine?

Same question for string/text single field PK

CodePudding user response:

From UNIQUE constraints:

In most cases, UNIQUE and PRIMARY KEY constraints are implemented by creating a unique index in the database. (The exceptions are INTEGER PRIMARY KEY and PRIMARY KEYs on WITHOUT ROWID tables.)

If a column is defined as INTEGER PRIMARY KEY it is actually an alias of the rowid of the column:

The data for rowid tables is stored as a B-Tree structure containing one entry for each table row, using the rowid value as the key. This means that retrieving or sorting records by rowid is fast. Searching for a record with a specific rowid, or for all records with rowids within a specified range is around twice as fast as a similar search made by specifying any other PRIMARY KEY or indexed value. The exception mentioned above is that if the declaration of a column with declared type "INTEGER" includes an "PRIMARY KEY DESC" clause, it does not become an alias for the rowid and is not classified as an integer primary key.

  • Related