sql 分组查询效率

来源:互联网 发布:mac 编译android源码 编辑:程序博客网 时间:2024/05/23 16:54

场景:分组查询每种类型 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秒

0 0
原创粉丝点击