SELECT DISTINCT was scanning two million rows
Every time you open the Puls dashboard it asks the API which sites there are. Simple query:
SELECT DISTINCT site FROM pageviews;
There’s an index on site, so that should be fast. Right?
Not really. SQLite uses the index, but it still walks through every single entry in it just to give you one row per
site. EXPLAIN QUERY PLAN says SCAN pageviews USING COVERING INDEX. Scan is the word to look out for.
So the cost grows with the history. With three sites and two million pageviews, you read two million index entries to get three names back. And it gets a bit slower every day you use it.
Jumping through the index
What I wanted was to jump from one site to the next in the index, instead of reading everything in between. Postgres people call this a loose index scan, or skip scan. SQLite doesn’t do it for you, but you can write it with a recursive CTE:
WITH RECURSIVE tracked(site) AS (
SELECT MIN(site) FROM pageviews
UNION ALL
SELECT (SELECT MIN(site) FROM pageviews WHERE site > tracked.site)
FROM tracked WHERE tracked.site IS NOT NULL
)
SELECT site FROM tracked WHERE site IS NOT NULL ORDER BY site;
Start with the smallest site. Then ask for the smallest site that is bigger than that one. And again. Each of those
MIN() lookups is a single seek in the index, not a scan.
When there are no more sites the subquery returns NULL and the recursion stops. That’s also why the empty table case
matters. MIN() on an empty table gives NULL, so you have to filter it out at the end or you get one NULL row back.
The numbers
Measured on a table with two million rows and three sites:
SELECT DISTINCT 92.35 ms
loose index scan 0.01 ms
Now the cost follows the number of sites, not the number of rows.
92 ms doesn’t sound like much. But it’s on every dashboard load and it only goes one direction. That’s the kind of thing you don’t notice until the day you do.
Don’t do it everywhere
The stats:build command still uses a plain DISTINCT. It runs once after an upgrade, if at all, not on every page
load. The recursive version is harder to read, so I’d only use it where the table is big and the query runs often.
I wrote the tests against the old query first and ran the same tests against the new one. Same results, including the empty table. That gave me a lot more confidence than just looking at the SQL and nodding.