SQLite Forum

Requesting optimization for index expression case
Login
```
sqlite> create table x(x text);
sqlite> insert into x select value from wholenumber where value between 1 and 100000000;
sqlite> create index i on x(length(x));
sqlite> .eqp on
sqlite> .timer on
sqlite> select sum(length(x)) from x;
QUERY PLAN
`--SCAN x (~1048576 rows)
┌────────────────┐
│ sum(length(x)) │
├────────────────┤
│ 788888898      │
└────────────────┘
Run Time: real 9.542 user 9.531250 sys 0.000000
sqlite> select sum(length(x)) from x indexed by i;
QUERY PLAN
`--SCAN x USING INDEX i (~1048576 rows)
┌────────────────┐
│ sum(length(x)) │
├────────────────┤
│ 788888898      │
└────────────────┘
Run Time: real 12.403 user 12.375000 sys 0.000000
sqlite> create index j on x(length(x),x);
Run Time: real 30.600 user 61.453125 sys 9.109375
sqlite> select sum(length(x)) from x indexed by j;
QUERY PLAN
`--SCAN x USING COVERING INDEX j (~1048576 rows)
┌────────────────┐
│ sum(length(x)) │
├────────────────┤
│ 788888898      │
└────────────────┘
Run Time: real 9.536 user 9.531250 sys 0.000000
sqlite>
```

The fastest solution is to do a table scan and compute the result.