I have a simple query on an SQLite database (1.5GB on disk). It seems my loop is very slow
for id, data in conn.execute("select ID, val in data order by ID"):
# do stuff
despite an index on the ID column. But when I restart the script, the rows that had been read in previous runs, loop in an acceptable speed. Are they somehow cached?
Is there anything I can do to make the loop over the complete data fast?
EDIT:
Apparently the orderby/index is the problem. There is a 2000x speed different between looping over rows with and without the order by
.
Pragma table_info(data) show
ID: cid=0 type=int notnull=0 dflt_value=null pk=0
val: cid=1 type=int notnull=0 dflt_value=null pk=0
I did no special adjustments to anything.
VACUUM did not improve performance.
My index is:
type=index, tbl_name=data rootpage=3 sql=create index myindex on data (ID)
SOLVED: Creating a covering index (id, val) and then cycling is a huge lot faster. I had no success with other adjustments...