On Jul 13, 2008, at 11:37 AM, Igor Tandetnik wrote:

>>
>> Does this change if I have an ORDER BY clause that does
>> not match any index?
>
> No (though it would take longer to get the first row, as SQLite will
> have to retrieve and sort all the records first).

If a query has both a LIMIT and an ORDER BY and the ORDER BY is such  
that a sort is required (in other words there is no index that can be  
used to satisfy the ORDER BY) then SQLite implements the sort using a  
priority queue.  As each (unsorted) row comes out of the original  
query, that row is placed into the queue in order (using a O(logN)  
insertion).  But if the LIMIT is K then only the first K elements of  
the queue are retained.  So the sorting process is still O(NlogN) but  
because elements beyond the Kth element are discarded, less temporary  
memory and/or disk space is used to do the sort.

So there is potentially some memory savings from specifying a LIMIT  
with an ORDER BY but the computation time is roughly the same with or  
without the LIMIT.  The query still must compute the entire result set  
before it outputs the first row.


D. Richard Hipp
[EMAIL PROTECTED]



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

Reply via email to