场景:分组查询每种类型 ID值最大 的记录;
mysql数据库30W数据量。
写法一:
select * from test1 t1 where t1.id in(
select MAX(t.id) as id from test1 t
GROUP BY t.col1
)
耗时超过30秒;
写法二:
select t2.* from (
select MAX(t.id) as id from test1 t
GROUP BY t.col1
)t1 LEFT JOIN test1 t2 on t1.id=t2.id
耗时不超过0.1秒