SQLite Forum

MAX performance
Login
Hi All,

I have a performance problem with a query involving MAX, where despite having a covering index, it gets progressively slower as more rows are inserted. I think I know why, but am just looking for another pair of eyes to confirm!

Schema:

```sql
CREATE TABLE events (
  id INTEGER PRIMARY KEY,
  stream TEXT NOT NULL CHECK(STREAM <> ''),
  data BLOB NOT NULL
);

CREATE INDEX events_stream_id ON events (stream, id DESC);
```

Query:

```sql
SELECT stream, MAX(id) AS id FROM events WHERE stream IN (?) GROUP BY stream;
```

Explain Query Plan:
```
SEARCH TABLE events USING COVERING INDEX events_stream_id (stream=?)
```

I think SQLite is scanning every row in the index instead of "simply" selecting the last. The query optimizer overview article: https://www.sqlite.org/optoverview.html#the_min_max_optimization talks about the "left-most" column. I suspect that the query optimizer is unable to utilise this optimization because my index has 2 columns.

My solution will be to create another table containing the max values, but just want to confirm I haven't missed anything.

Thanks in advance,

Ben