sqliteoptimizationindexingfull-table-scan

SQLite: Optimize table scans


In my table I have some columns that have no index on them, searching for a value in those columns can take very long because SQLite does a full table scan.

In my specific case the row I'm looking for (the values are unique) is almost always among the most recently inserted.

I suspect that SQLite starts from the oldest (first) row when doing the scan, is there any way to instruct SQLite to do the table-scan in reverse order?

UPDATE: I found this in the changelog:

The optimizer will now scan tables in the reverse if doing so will satisfy an ORDER BY ... DESC clause.

So maybe this means I can just do add an ORDER BY clause, to speed it up.


Solution

  • The solution was:

    ORDER BY rowid DESC LIMIT 1
    

    It made the lookups lightning fast!