Question

Assuming I have a schema like this:

CREATE TABLE abc(
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  txt TEXT
);

CREATE INDEX "txtCS" ON "abc"("txt" COLLATE MY_CUSTOM_SORT);

when will sqlite use my index on txt ?

because I ran:

EXPLAIN QUERY PLAN SELECT * FROM abc ORDER BY txt COLLATE MY_CUSTOM_SORT DESC ...

and it tells me that it scans the table, twice, using the txtCS index (It doesn't search like I expected.)

MY_CUSTOM_SORT is my own sorting function that I hooked with sqliteCreateCollation. I just need that index for some queries that involve special ordering and I want them to be fast

Was it helpful?

Solution

In the EXPLAIN QUERY PLAN output, SEARCH means that the database tries to look up some particular record(s) with specific values, while SCAN means that the database goes through the entire table. This query returns all records, so the most efficient operation is a SCAN.

Either operation can be sped up with an index. (In a SCAN, the database just goes through all index entries in order.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top