SQLite count performance -
i'm experiencing (in opinion) weird performance issues when trying find total number of records in table. (boiled down) table looks this:
create table "records" ( pk integer primary key autoincrement, other_table_fk integer, int_value integer, foreign key(other_table_fk) references other_table(pk) on delete cascade } create index int_value_idx on records(int_value);
now when try query count of records takes long time:
$ time sqlite3 db.sqlite "select count(rowid) records" 100000 real 0m2.962s user 0m0.162s sys 0m2.618s
when indexed field used query way faster (but count same):
$ time sqlite3 db.sqlite "select count(rowid) records records.int_value > 0" 100000 real 0m0.083s user 0m0.015s sys 0m0.057s
my problem is, table schema dependant on users configuration, cannot have indexed field in table, still want have performance of indexed count
.
i know primary keys have index on own, in case not seem help:
$ time sqlite3 db.sqlite "select count(rowid) records records.pk > 0" 100000 real 0m2.965s user 0m0.138s sys 0m2.636s
weirdly, if create additional index on primary key, not either:
$ time sqlite3 db.sqlite "create index records_pk_idx on records(pk)" real 0m3.288s user 0m0.313s sys 0m2.812s $ time sqlite3 db.sqlite "select count(rowid) records records.pk > 0" 100000 real 0m2.974s user 0m0.154s sys 0m2.624s
i read suggestions circumvent issue, either using max(_rowid_)
(not option, regular deletes) , using triggers store number of records (seems "dirty" me store redundant information).
are there other ways speed seemingly trivial queries?
for record:
$ sqlite3 -version 3.6.20
i'm stuck version.
edit:
as suggested cl. explain query plan
info:
$ sqlite3 db.sqlite "explain query plan select count(rowid) records" 0|0|table records $ sqlite3 db.sqlite "explain query plan select count(rowid) records records.pk < 0" 0|0|table records using primary key $ sqlite3 db.sqlite "explain query plan select count(rowid) records records. int_value > 0" 0|0|table records index int_value_idx
this not seem add additional information.