PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > SELECT MAX vs. SELECT / ORDER BYDESC / LIMIT
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
SELECT MAX vs. SELECT / ORDER BYDESC / LIMIT

Réponse
 
LinkBack Outils de la discussion
Vieux 04/10/2007, 17h00   #1
yawnmoth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 04/10/2007, 18h09   #2
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SELECT MAX vs. SELECT / ORDER BYDESC / LIMIT

yawnmoth wrote:
> 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.


  Réponse avec citation
Vieux 05/10/2007, 08h23   #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
Vieux 05/10/2007, 09h26   #4
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SELECT MAX vs. SELECT / ORDER BYDESC / LIMIT

On 5 Oct, 08:23, "Martijn Tonies" <m.ton...@upscene.removethis.com>
wrote:
> > > 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 Productionshttp://www.upscene.com
> My thoughts:http://blog.upscene.com/martijn/
> Database development questions? Check the forum!http://www.databasedevelopmentforum.com- Hide quoted text -
>
> - Show quoted text -


As I commented, I would have expected to be able to say "no
difference", but my tests prove otherwise.

Another thing I am curious about and am about to test, is whether an
index can make a difference when doing a SELECT DISTINCT on a column
in a large table. If I was programming thebasics in VSAM, I know how I
could use such an index to optimise this task, but will MySQL do
similar?

  Réponse avec citation
Vieux 06/10/2007, 10h49   #5
Séverin Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SELECT MAX vs. SELECT / ORDER BYDESC / LIMIT

according to me
> SELECT MAX(order_total) AS order_max
> FROM orders
> WHERE customer = ...

is faster

with a limitation that is:
> SELECT MAX(order_total) AS order_max,****ID_ORDER****
> FROM orders
> WHERE customer = ...

will give u a random ****ID_ORDER****
(not the one with the max order)

and
> SELECT order_total AS order_max, ****ID_ORDER****
> FROM orders
> WHERE customer = ...
> ORDER BY order_total DESC
> LIMIT 1

will give u the right ****ID_ORDER****

That is the amazing way that the MAX() function works(fast).


yawnmoth wrote:
> 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
>

  Réponse avec citation
Vieux 06/10/2007, 22h32   #6
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SELECT MAX vs. SELECT / ORDER BYDESC / LIMIT

Séverin Richard wrote:
> yawnmoth wrote:
>> 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

> according to me
>> SELECT MAX(order_total) AS order_max
>> FROM orders
>> WHERE customer = ...

> is faster
>
> with a limitation that is:
>> SELECT MAX(order_total) AS order_max,****ID_ORDER****
>> FROM orders
>> WHERE customer = ...

> will give u a random ****ID_ORDER****
> (not the one with the max order)
>
> and
>> SELECT order_total AS order_max, ****ID_ORDER****
>> FROM orders
>> WHERE customer = ...
>> ORDER BY order_total DESC
>> LIMIT 1

> will give u the right ****ID_ORDER****
>
> That is the amazing way that the MAX() function works(fast).
>
>

Please do not top post. (Top posting fixed)

I have tested it and found that the speed advantage is the opposite way
around from what you suggest. Have you tried it?


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 04h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17155 seconds with 14 queries