group by 后取的数据默认好像是第一条。现在我想取分组后,最后写入的一条数据。请教下这个用group by 能做到吗?不能的话,有什么其他的方案实现,前提要确保性能(至少在几十万的数据量下,不要太慢)?有经验的朋友来分享下哈。先表示感谢!
如下的方案,性能偏低:
select content,ac,id,aId from c_log where id in (select max(id) from c_log where ac in(1,3,5) group by aId )
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这个性能要比group by 好的多。而且满足要求。
具体性能分析,可以参考
http://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group
建议在已有的sql中将in改用inner join做关联,同时给group by出来的临时表添加索引。