当我在有14K记录的表上运行这个查询时
select max(id) as 'id' from table_records where search_counter='0' group by Record_Date
在0.016 sec中执行,返回676条记录
id列是主键并且在其上定义了唯一索引 现在,当我使用上面的查询作为子查询时,例如select * from table_records where id in
(select max(id) as 'id' from table_records where search_counter='0' group by Record_Date)
永远执行。在我的本地开发机器上,需要139.511秒在服务器上执行,超时
我不知道这里到底出了什么问题。
如有任何帮助,不胜感激
代替in
运行join查询
select t1.id, t1.record_date from table_records t1
inner join (select max(id) as 'id' from table_records
where search_counter='0' group by Record_Date) t2
on t1.id = t2.id;