On “order by” optimization
1–10 of 16 posts
Re: On “order by” optimization
#2SELECT c,d FROM t WHERE b=X ORDER BY a DESC LIMIT 1
on MySQL and was surprised to find that MySQL was using index over a and in his opinion fully ignored the existence of the index over (b,c) to discover where b is equal to X? I admit I didn't understand his explanation why, especially not why it would maybe do the right thing with the LIMIT 2. I can't imagine that any serious SQL engine would ignore the possibility to use the index in such a case.
Re: On “order by” optimization
#3The author is really amateur. His inability to do what amounts to SQL I in college is quite telling, and somehow hes opinionated enough to write a huge blog article about it. I like the analysis and diagnostics of the query planning - but his query writing is trashgutter.
SELECT c,d FROM t WHERE a=(SELECT MAX(a) WHERE b=X from t)
That query is what I would have done from the getgo. It reads much better and is going to pretty much always be the optimized query plan. This is basic stuff...Re: On “order by” optimization
#4In short, if I understood, the author used SELECT c,d FROM t WHERE b=X ORDER BY a DESC LIMIT 1 on MySQL and was surprised to find that MySQL was using index over a and in his opinion fully ignored the existence of the index over (b,c) to discover where b is equal to X? I admit I didn't understand his explanation why, especially not why it would maybe do the right thing with the LIMIT 2. I can't imagine that any serio…
Re: On “order by” optimization
#5Subselects are almost always better when it comes to optimization. The author is really amateur. His inability to do what amounts to SQL I in college is quite telling, and somehow hes opinionated enough to write a huge blog article about it. I like the analysis and diagnostics of the query planning - but his query writing is trashgutter. SELECT c,d FROM t WHERE a=(SELECT MAX(a) WHERE b=X from t) That query is what I…
In the future, please edit that stuff out and stick to the substance when commenting here.
Re: On “order by” optimization
#6Re: On “order by” optimization
#7In short, if I understood, the author used SELECT c,d FROM t WHERE b=X ORDER BY a DESC LIMIT 1 on MySQL and was surprised to find that MySQL was using index over a and in his opinion fully ignored the existence of the index over (b,c) to discover where b is equal to X? I admit I didn't understand his explanation why, especially not why it would maybe do the right thing with the LIMIT 2. I can't imagine that any serio…
Its usually better to be explicit and use aggregates like MAX or MIN instead of a LIMIT of 1.
Re: On “order by” optimization
#8Subselects are almost always better when it comes to optimization. The author is really amateur. His inability to do what amounts to SQL I in college is quite telling, and somehow hes opinionated enough to write a huge blog article about it. I like the analysis and diagnostics of the query planning - but his query writing is trashgutter. SELECT c,d FROM t WHERE a=(SELECT MAX(a) WHERE b=X from t) That query is what I…
Re: On “order by” optimization
#9In short, if I understood, the author used SELECT c,d FROM t WHERE b=X ORDER BY a DESC LIMIT 1 on MySQL and was surprised to find that MySQL was using index over a and in his opinion fully ignored the existence of the index over (b,c) to discover where b is equal to X? I admit I didn't understand his explanation why, especially not why it would maybe do the right thing with the LIMIT 2. I can't imagine that any serio…
Its usually better to be explicit and use aggregates like MAX or MIN instead of a LIMIT of 1.
MySQL will return a random c and d, not necessarily the c and d from the row with maximum a...
Re: On “order by” optimization
#10I have not used MySql and cannot comment for that but with my experience with Oracle, i believe its histogram generation process is actually trying to solve the same problem.It has a binning strategy for histogram generation that tries to help with data patterns.