我无法弄清楚为什么在Query2中的'order by'子句导致它超过一分钟,而第一个子句立即返回结果。有没有更好的办法 '由序' 要做到这一点Mysql Order By By Group By造成查询速度很慢
快速:
select c.id, max(date(a.sent)) as sent,
if(c.id in (select id from bin where (num=1 or num=2)),1,0) as done
from test c, test2 a
where c.id=a.id
group by c.id
limit 1;
慢
select c.id, max(date(a.sent)) as sent,
if(c.id in (select id from bin where (num=1 or num=2)),1,0) as done
from test c, test2 a
where c.id=a.id
group by c.id
order by done, sent
limit 1;
对于大多数现代的RDBMS,子查询并不比联接更差。对于旧的关系型数据库来说,这只是真的,他们无法弄清楚两者是否相同。今天,有时一个子查询是一个更好的方法去...这一切都取决于。 –