On 7 Apr 2013, at 2:08pm, 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?

The ORDER BY clause does not know about the LIMIT clause.  So even with a LIMIT 
1 it doesn't know that all it needs to do is pick the lowest value and not 
bother sorting the rest.

A limit clause just makes _step() return "I'm run out of result rows" sooner 
than normal.  It's like doing

_prepare()
_step()
_step()
_finalize()

stopping _step() after a few times, before you run out of results.  So the 
LIMIT clause has no influence on any ORDER BY clause.

Actually what you suggest is an interesting optimization.  But I think it would 
introduce too much extra code to be appropriate for SQLite.

Simon.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to