views:

71

answers:

3

This is my table:

ID   KEY    VALUE
1    alpha  100
2    alpha  500
3    alpha  22
4    beta   60
5    beta   10

I'm trying to retrieve a list of all KEY-s with their latest values (where ID is in its maximum):

ID   KEY     VALUE
3    alpha   22
5    beta    10

In MySQL I'm using this query, which is not effective:

SELECT temp.* FROM
  (SELECT * FROM t ORDER BY id DESC) AS temp
GROUP BY key

Is it possible to avoid a sub-query in this case?

+2  A: 

Use an INNER JOIN to join with your max ID's.

SELECT  t.*
FROM    t
        INNER JOIN (
          SELECT  ID = MAX(ID)
          FROM    t
          GROUP BY
                  key
        ) tm ON tm.ID = t.ID                  

Assuming the ID column is indexed, this is likely as fast as its going to get.

Lieven
@Lieven MySQL will create **many** temporary tables, while in my example there will be only one. Are you sure that your SQL is faster??
Vincenzo
I don't have a MySQL running but it shouldn't be hard to try both versions and compare both plans to determine the most optimal solution. Using SQL Server with only 5 ID's, both solutions perform equally fast. Using 13 ID's, the LEFT JOIN solution takes 55%, the INNER JOIN solution 45%. You can expect that difference to become even more apparent when your table grows. *Note: the ID column is the primary key and there's an index on the Value column*
Lieven
...that should read *"there's an index on the Key column"*
Lieven
@Lieven Works perfect, thanks!
Vincenzo
@Lieven - good math. :) +1
Lee
+2  A: 

here is the mysql documentation page that discusses this topic.

it presents three distinct options.

the only one that doesn't involve a sub query is:

SELECT t1.id, t1.k, t1.value
FROM t t1
LEFT JOIN t t2 ON t1.k = t2.k AND t1.id < t2.id
WHERE t2.k IS NULL;
Lee
@Lee That's exactly what I was looking for, thanks!
Vincenzo
@Lee Oops, this query is 10x times slower than my original one (there are 2mln rows in my table)...
Vincenzo
:-) the original question was "how to avoid a sub query"... not "how to make the most efficient query". I believe that the choice of which query to use for optimal performance comes down to how many rows you have in your table, and how many unique values of `key`. Glad you got it sorted out.
Lee
@Lee Yes, it was my mistake, I didn't specify the question properly. Thanks for your help, I up-voted your answer
Vincenzo
@Lee, as did I. +1
Lieven
A: 

There's page in the manual explaining how to do this

symcbean