我有如下表(包含超過一百萬行):優化MySQL的選擇查詢以獲得一個隨機行WHERE子句
**MyTable**
id - int(10)
externalId - smallint(5)
description - varchar(250)
status - smallint(6)
entrydate - int(10) (unix timestamp)
我希望能夠選擇一個隨機行(我只需要值在結果的ID)從該表具有以下where子句:
/* The number of external ids here can range from 1 to around 50 */
WHERE externalId in (1,2,3,4,x,x,x)
/* Status is fixed at 2 for all calls */
AND status = 2
/* Entry date is typically (now - 30 days) or may be excluded all together */
AND entrydate > 1279887765
顯而易見的解決方案是使用蘭特(),但是我發現,這種性能上有所欠缺,這是相當有據可查。那麼是否有人有其他解決方案?爲了給出一點背景:我打算在一個PHP驅動的網站上使用這個查詢,並且輸入參數取決於用戶,所以這些將從用戶變爲用戶。還值得一提的是,我根本無法改變表格結構。
在此先感謝。
問題在於ID的順序不一樣,可能會有很大的差距。 – MrEyes 2010-11-29 13:21:44