On 5/11/18, Deon Brewis <d...@mylio.com> wrote:
> e.g. If you do:
> SELECT c1 from t1 ORDER BY c2 LIMIT 5;
> vs. just running the query without the "LIMIT" clause and taking the top 5
> rows programmatically?

Yes, if there is an ORDER BY that cannot be satisfied by an index and
the total number of rows in the output is large relative to the LIMIT.
This is especially in 3.24.0 and later.

Without the LIMIT, all the terms of the original query must be
computed then sorted.  This can require a lot of storage.  With the
LIMIT, only the top N results must be stored.  And beginning with
3.24.0, if it is clear that a particular row will never make the LIMIT
cutoff, then columns that are not part of the ORDER BY clause are
never computed in the first place.  This latter optimization can be a
big win if the non-ORDER BY terms involve expensive functions and/or
correlated subqueries.
D. Richard Hipp
sqlite-users mailing list

Reply via email to