MySQL Forums
Forum List  »  Optimizer & Parser

Re: Statistics used by optimizer.
Posted by: Rick James
Date: September 01, 2009 09:59PM

A possible answer...
In drilling down the BTree to find const1 (or const2), it gets some feel for what percentage of the BTree nodes INDEX(x) are to the left of the path it is traversing. If that is < 30%, it will use the index; if > 30%, it won't bother with the index. Instead it will use a table scan.

OTOH, if x is a "clustered key" (ie, PRIMARY KEY for InnoDB), the index and the data are one and the same. So it will scan the data/PK from beginning up to const1 (or const2).

OTOOH, your example is rather simplistic -- note that the only columns mentioned are (x). So, it will do the entire query in the index. Think of any index as being a BTree-ordered table. And BTrees are easily walked through (forward or backward). If the query can be resolved by scan of the index, that's always (well, almost always) faster than any other approach.
SELECT x, y FROM t1 WHERE x<const1
would invalidate this optimization, unless you had
INDEX(x,y)
or even (in this case)
INDEX(y,x)
or even
INDEX(z,y,x)
This last one _may_ be less inefficient than scanning the table -- the entire index would have to be scanned, and
(1) the index could be bigger than the data
(2) a BTree is probably less efficient to scan thru than the data.

Options: ReplyQuote


Subject
Views
Written By
Posted
6364
August 28, 2009 07:32AM
2645
August 29, 2009 01:11PM
2499
September 01, 2009 06:30AM
2450
September 01, 2009 07:44AM
2478
September 01, 2009 02:51PM
Re: Statistics used by optimizer.
2278
September 01, 2009 09:59PM
2409
September 04, 2009 11:00AM
2406
September 04, 2009 07:37PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.