planetary-ios icon indicating copy to clipboard operation
planetary-ios copied to clipboard

Move all DISTINCT + ORDER By queries in sql to using subqueries

Open rabble opened this issue 1 year ago • 0 comments

https://stackoverflow.com/questions/13533658/sqlite-subselect-much-faster-than-distinct-order-by % echo "SELECT DISTINCT acolumn FROM atable ORDER BY acolumn;" | time sqlite3 mydb sqlite3 mydb 8.87s user 15.06s system 99% cpu 23.980 total

% echo "SELECT acolumn FROM (SELECT DISTINCT acolumn FROM atable) ORDER BY acolumn;" | time sqlite3 options sqlite3 mydb 1.15s user 0.10s system 98% cpu 1.267 total

Sqlite3 doesn't do a very smart query optimizer when making queries that have DISTINCT and ORDER BY. If we move the DISTINCT in to a subquery it's MUCH MUCH faster. We've got 11 places in the code where we do this and would benefit.

rabble avatar Jul 08 '22 05:07 rabble