我有一个相当低效的MySQL查询,直到最近一直工作正常,因为数据库的大小已经增长。我试图使用ORDER BY RAND()语法选择一个随机记录,但它非常慢(30+秒)并导致我的PHP脚本出现问题。有没有更有效的方法来编写此查询?
SELECT * FROM accountcampaign, accounts WHERE
accountcampaign.status='ACTIVE'
AND accountcampaign.dateLocked IS NULL
AND accountcampaign.campaignID='1'
AND ( (accountcampaign.lockedIPAddress IS NULL)
OR (accountcampaign.lockedIPAddress='')
)
AND ( (accountcampaign.dateLastEntry IS NULL)
OR (DATE(accountcampaign.dateLastEntry) < DATE(NOW()))
)
ORDER BY RAND() LIMIT 1
答案 0 :(得分:4)
您不想使用ORDER BY RAND() LIMIT 1
:它会为您表格中的每一行生成一个随机数。
您应该尝试使用2个查询: