我一直在努力優化我的查詢,看起來像一個:MySQL的UNION VS IN子句
select toc.* from
(select sender, max(convid) as maxconvid
from MetaTable
where sender in ('arjunchow','aamir_alam')
group by sender) as tmp1
inner join MetaTable as toc on
toc.sender = tmp1.sender
and toc.convid = tmp1.maxconvid;
當MySQL服務器是在壓力下,這個查詢通常0.2秒內響應,但是當數IN子句中的「發件人」ID增加(> 50),查詢速度變慢(約5-6秒)。
考慮到我的查詢可能成爲50個查詢的聯合,建議使用多個聯合子句而不是IN子句。所以我的查詢將如下所示:
(SELECT
convId,
UNIX_TIMESTAMP(timeStamp) as timeStamp,
UNIX_TIMESTAMP(createTime) as createTime,
numMessages,
rootMsg,
sender,
ipormobile,
modIpOrMobile,
UNIX_TIMESTAMP(modTimeStamp) as modTimeStamp
from
MetaTable
where
sender='arjunchow'
ORDER BY convId DESC limit 1)
UNION ALL
(SELECT
convId,
UNIX_TIMESTAMP(timeStamp) as timeStamp,
UNIX_TIMESTAMP(createTime) as createTime,
numMessages,
rootMsg,
sender,
ipormobile,
modIpOrMobile,
UNIX_TIMESTAMP(modTimeStamp) as modTimeStamp
from
MetaTable
where
sender='aamir_alam'
ORDER BY convId DESC limit 1)
EXPLAIN輸出會很好。 – 2009-06-09 10:18:44