On Sun, Apr 7, 2013 at 9:08 AM, Baruch Burstein <bmburst...@gmail.com>wrote:

> If I issue a select statement with a ORDER BY clause and a LIMIT clause,
> does SQLite do a full sort (assuming no index) and then return the first X
> rows, or just a partial sort to get the first X sorted results?
>

It uses a btree as a priority queue.  If you have LIMIT N, then it
initially starts putting results into the btree until the btree contains N
entries.  Then for each additional row, it first adds the value to the
btree, then removes the largest entry in the btree.  Hence the btree always
holds the smallest N entries seen so far. Once all is finished, it walks
the btree to output the results.

So with limit N, it never stores more than N results at one time.

-- 
D. Richard Hipp
d...@sqlite.org
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to