December 14th 2014, SQLite Efficient Searching
← November 12th 2014 Android JNI Torch Revisited | ● | December 15th 2014 QtTorch App →
Sometimes it is necessary to add an index to an existing table, because searching has turned out to be slow:
Supposed we have a table “names”, which has a primary key “id” and stores the additional columns “name” and “surname”:
CREATE TABLE names(id INTEGER PRIMARY KEY, name CHAR[100], surname CHAR[100])
Then searching the table by name will be awfully slow for large numbers of inserted items, because the primary key is the only index of the table, which speeds up searching by id but not by name. So we would like to add another composite search index (name, surname) to the table:
CREATE INDEX search_index ON names(name, surname)
The above index speeds up searching by (name, surname), but it also speeds up searching by any prefix of the search index like (name). So we do NOT have to explicitly add another search index like:
CREATE INDEX search_index ON names(name)
For the same reason, we do NOT add the following index
CREATE INDEX search_index ON names(surname, name)
to the table, because this one would speed up searching by (surname, name) but not by (name).
← November 12th 2014 Android JNI Torch Revisited | ● | December 15th 2014 QtTorch App →