![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
In a query such as: SELECT id FROM foo WHERE status = 'NORMAL' ORDER BY id LIMIT k (status is an enum, with an index) Does MySQL select all the rows which meet the WHERE condition, sort them all, then pick the top k? *Or is it smart enough to optimize the combined ORDER & LIMIT operations into a single partial sort which runs in O(n log k), as opposed to O(n log n)? |
#3
| |||
| |||
|
|
On Oct 22, 1:31*pm, Roy Smith <r... (AT) panix (DOT) com> wrote: In a query such as: SELECT id FROM foo WHERE status = 'NORMAL' ORDER BY id LIMIT k (status is an enum, with an index) Does MySQL select all the rows which meet the WHERE condition, sort them all, then pick the top k? If the index was status + id, then it could do it without needing to sort at all. |
#4
| |||
| |||
|
|
How would MySql, using that index, know what the greatest or smallest 10 id's are without sorting them? Is an index always already sorted? |
#5
| |||
| |||
|
|
Captain Paralytic: On Oct 22, 1:31 pm, Roy Smith <r... (AT) panix (DOT) com> wrote: In a query such as: SELECT id FROM foo WHERE status = 'NORMAL' ORDER BY id LIMIT k (status is an enum, with an index) Does MySQL select all the rows which meet the WHERE condition, sort them all, then pick the top k? If the index was status + id, then it could do it without needing to sort at all. How would MySql, using that index, know what the greatest or smallest 10 id's are without sorting them? Is an index always already sorted? [I'm not saying it isn't...I just don't know and would like to hear some more] its sort of sorted yes. Broadly that's what index MEANS. |
#6
| |||
| |||
|
|
Erick T. Barkhuis wrote: Captain Paralytic: On Oct 22, 1:31 pm, Roy Smith <r... (AT) panix (DOT) com> wrote: In a query such as: SELECT id FROM foo WHERE status = 'NORMAL' ORDER BY id LIMIT k (status is an enum, with an index) Does MySQL select all the rows which meet the WHERE condition, sort them all, then pick the top k? If the index was status + id, then it could do it without needing to sort at all. How would MySql, using that index, know what the greatest or smallest 10 id's are without sorting them? Is an index always already sorted? [I'm not saying it isn't...I just don't know and would like to hear some more] its sort of sorted yes. Broadly that's what index MEANS. |
#7
| |||
| |||
|
|
How would MySql, using that index, know what the greatest or smallest 10 id's are without sorting them? Is an index always already sorted? That is the whole purpose of an index. It speeds up searching tremendously, as you can do a binary search. (See http://www.topcoder.com/tc?module=St...2=binarySearch ) |
#8
| |||
| |||
|
|
Suppose we have these records (never mind the symantics): id date city ------------------------- 12 2009-08-12 London 14 2009-07-16 Athens 15 2010-01-10 Paris Now, if I have an index on 'city', what do I actually have? - just an array [0=>Athens, 1=>London, 2=>Paris] ? - an array [Athens=>???, London=>???, Paris=>???] ? - the full table ordered by 'city'? - something else? |
#9
| |||
| |||
|
|
On 2010-10-22 18:53, Erick T. Barkhuis wrote: [...] Suppose we have these records (never mind the symantics): id date city ------------------------- 12 2009-08-12 London 14 2009-07-16 Athens 15 2010-01-10 Paris Now, if I have an index on 'city', what do I actually have? - just an array [0=>Athens, 1=>London, 2=>Paris] ? - an array [Athens=>???, London=>???, Paris=>???] ? - the full table ordered by 'city'? - something else? Think of the index as a sorted tree where the leaf nodes point to a page in the data. You will typically find the row that match your predicate with log(n) i/o operations, and then fetch any columns not part of the index. For a large table this will save a lot of time, for a small table the optimizer would probably choose to ignore the index and scan the table instead. /Lennart [...] |
#10
| |||
| |||
|
|
How would MySql, using that index, know what the greatest or smallest 10 id's are without sorting them? Is an index always already sorted? |
![]() |
| Thread Tools | |
| Display Modes | |
| |