Afficher un message
Vieux 05/10/2007, 09h23   #3
Martijn Tonies
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SELECT MAX vs. SELECT / ORDER BYDESC / LIMIT

> > I'm curious - which query is generally faster?:
> >
> > SELECT MAX(order_total) AS order_max
> > FROM orders
> > WHERE customer = ...
> >
> > ...or...
> >
> > SELECT order_total AS order_max
> > FROM orders
> > WHERE customer = ...
> > ORDER BY order_total DESC
> > LIMIT 1

>
> The latter (especially where there is an index on order_total).
>
> There is no good reason I can see why the former could not be made as

good,
> but all the tests I have done have shown that the latter wins hands down.
>
> Incidentally, I was curious of this a whle ago, which was when I tested

it.
> You could have done the same. You learn a lot by trying this sort of
> exercise.


Hmm, funny. In another database system I use, I could easily say: no
difference.
Cause the index would most probably be used on CUSTOMER (assuming
there would be one, on this foreign key column) and indeed, optionally on
ORDER_TOTAL. The optimizer could use the latter index (if CUSTOMER
isn't unique, of course) for both ORDER BY DESC and MAX :-)

So, this is different in MySQL?

And, of course, the question to the original poster: is Customer in this
case
unique? If so, I'd still be saying: no difference.


--
Martijn Tonies
Database Workbench - development tool for MySQL, and more!
Upscene Productions
http://www.upscene.com
My thoughts:
http://blog.upscene.com/martijn/
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com


  Réponse avec citation
 
Page generated in 0,04969 seconds with 9 queries