我有一个包含多列主键(城市/州/日期)和更多数据列的表。我正在寻找每个城市/州的最新数据。我如何干净/有效地做到这一点?现在我可以通过执行第一个查询来获取我要获取的所有行的列表,然后执行第二个带有大量 WHERE 子句的查询来做到这一点:
SELECT state, city, max(date) from data GROUP BY city, state;
+-------+---------------------+------------+
| state | city | MAX(date) |
+-------+---------------------+------------+
| CA | San Francisco | 2013-09-01 |
| CA | Los Angeles | 2013-08-01 |
| NY | New York | 2013-10-01 |
| ... | ... (many rows) ... | ... |
+-------+---------------------+------------+
SELECT * FROM data WHERE
(state = "CA" AND city = "San Francisco" AND date='2013-09-01') OR
(state = "CA" AND city = "Los Angeles" AND date='2013-08-01') OR
(state = "NY" AND city = "New York" AND date='2013-10-01') OR
...
这真的很丑陋且效率低下,如果第一个查询返回很多行,我的第二个查询可能太长了。显然,如果我有一个单列主键,我可以使用带有 IN() 的子选择,但这在这里是不可能的。有什么建议么?
更新:我用一个子选择尝试了比尔的建议,但它没有使用任何键并且需要永远。如果我将子选择限制为仅返回 5 行,它将在 0.64 秒内返回。如果我让它返回所有 73 个城市/州的组合,则需要很长时间(查询仍在运行)。
EXPLAIN SELECT * FROM data WHERE (city, state, date) IN (SELECT state, city, MAX(date) FROM data GROUP BY city, state)
+----+--------------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| 1 | PRIMARY | data | ALL | NULL | NULL | NULL | NULL | 13342 | Using where |
| 2 | DEPENDENT SUBQUERY | data | index | NULL | PRIMARY | 57 | NULL | 8058 | Using index |
+----+--------------------+-------+-------+---------------+---------+---------+------+-------+-------------+