我無法弄清楚爲什麼在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,子查詢並不比聯接更差。對於舊的關係型數據庫來說,這只是真的,他們無法弄清楚兩者是否相同。今天,有時一個子查詢是一個更好的方法去...這一切都取決於。 –