Comments on: MySQL not being able to utilize a compound index? https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index Blog by Shlomi Noach Thu, 12 Mar 2015 01:47:15 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: MySql is not optimizing the query properly | XL-UAT https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-311743 Thu, 12 Mar 2015 01:47:15 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-311743 […] There’s Bug #35819, which I originally found in this article, which was in turn mentioned in the comments on this post. […]

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-74243 Wed, 07 Mar 2012 05:14:27 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-74243 @Rick,
The “(a<=2) AND (a<2 OR b<2)" is a cool transformation. I need to test this.

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-74242 Wed, 07 Mar 2012 05:12:50 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-74242 @Rick,
There is no table scan. Although there is this “OR”, the optimizer works it out well.

]]>
By: Rick James https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-74174 Tue, 06 Mar 2012 23:04:39 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-74174 A further note. (HINT to developers)

It should be easy to transform the parse tree for

WHERE (a, b) <* (x, y)

into

a <= x AND (a < x OR b <* y)

Notes:

* <* represents either < or <=

* other operators should have similar transformations

* (a,b,c) < … — also possible, but much messier (exercise for the reader)
* This is useful only if you have an index starting with `a`. (Only `a` will be used, so including `b` is optional.)

* The index will be scanned _through_ a=x, regardless of b and y.

]]>
By: Rick James https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-74164 Tue, 06 Mar 2012 22:43:10 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-74164 (a, b) < (…) is notorious for being poorly optimized.

The optimal way to do the query is not

(a<2) OR (a=2 AND b<2)

which involves OR at the top level, hence probably a table scan. Instead, use this equivalent:

(a<=2) AND (a<2 OR b<2)

(Caveat: When there is a huge number of rows with a=2, it will still scan all the a=2 values.)

]]>
By: Log Buffer #146: a Carnival of the Vanities for DBAs | Pythian Group Blog https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-1938 Fri, 15 May 2009 17:01:31 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-1938 […] code.openark.org, Shlomi Noach writes It seems like MySQL is unable to utilize a compound index when evaluating a plan for a query with a range condition. I’m looking for an explanation. I’ll […]

]]>
By: Xaprb https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-1861 Sat, 09 May 2009 20:51:51 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-1861 Shlomi, as others said — this is just an optimizer weakness at the moment. Peter posted on this a while ago, too: http://www.mysqlperformanceblog.com/2008/04/04/multi-column-in-clause-unexpected-mysql-issue/

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-1855 Sat, 09 May 2009 10:40:03 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-1855 @Harrison,

I agree. Thanks for the reference!

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-1848 Fri, 08 May 2009 19:00:44 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-1848 @Shantanu,

Thanks,
1. Haven’t tried this with 5.1
2. I do
3. Unlimited (killed query after realizing it was running for a few minutes)

]]>
By: Harrison Fisk https://shlomi-noach.github.io/blog/mysql/mysql-not-being-able-to-utilize-a-compound-index/comment-page-1#comment-1846 Fri, 08 May 2009 14:22:39 +0000 https://shlomi-noach.github.io/blog/?p=810#comment-1846 The query isn’t a subquery, however for some reason that is where the docs mentions the restriction about row operators. I suspect it was because the row operators were added at the same time as subqueries.

Really, the restriction should be put somewhere else in the documentation, such as the index section or even where row operators are mentioned in the docs.

]]>