[sqlite] Partial sorting

2013-04-07 Thread Baruch Burstein
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? -- ˙uʍop-ǝpısdn sı ɹoʇıuoɯ ɹnoʎ 'sıɥʇ pɐǝɹ uɐɔ noʎ ɟı

Re: [sqlite] Partial sorting

2013-04-07 Thread Simon Slavin
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

Re: [sqlite] Partial sorting

2013-04-07 Thread Stephen Chrzanowski
I don't know if it'd be an interesting optimization. Who's to say what the order ends up as prior to the sort? Take for example if I have a list of dollars and cents being returned from a query. I want the 5 top highest cost items out of 6 possibilities, if I keep the top 5 unsorted, item 6

Re: [sqlite] Partial sorting

2013-04-07 Thread Simon Slavin
On 7 Apr 2013, at 3:51pm, Stephen Chrzanowski pontia...@gmail.com wrote: I don't know if it'd be an interesting optimization. Who's to say what the order ends up as prior to the sort? Take for example if I have a list of dollars and cents being returned from a query. I want the 5 top

Re: [sqlite] Partial sorting

2013-04-07 Thread Clemens Ladisch
Stephen Chrzanowski wrote: I don't know if it'd be an interesting optimization. Who's to say what the order ends up as prior to the sort? When doing a merge sort, it would actually be possible to abort the very last merge step when the first LIMIT entries have been merged. I might be wrong,

Re: [sqlite] Partial sorting

2013-04-07 Thread Richard Hipp
On Sun, Apr 7, 2013 at 9:08 AM, Baruch Burstein bmburst...@gmail.comwrote: 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

Re: [sqlite] Partial sorting

2013-04-07 Thread Simon Slavin
On 7 Apr 2013, at 6:56pm, Richard Hipp d...@sqlite.org wrote: 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

Re: [sqlite] Partial sorting

2013-04-07 Thread Baruch Burstein
I was thinking more of general (not just LIMIT 1) optimization: http://en.wikipedia.org/wiki/Partial_sorting On Sun, Apr 7, 2013 at 6:16 PM, Simon Slavin slav...@bigfraud.org wrote: On 7 Apr 2013, at 3:51pm, Stephen Chrzanowski pontia...@gmail.com wrote: I don't know if it'd be an