我想在此查询中添加快速随机选择:
SELECT * FROM postlink WHERE `source`='$mysource' AND NOT EXISTS (SELECT sign FROM `state` WHERE postlink.sign = state.sign AND `cite`='$mycite') LIMIT 2
我用过这个,但这很慢:
SELECT * FROM postlink WHERE `source`='$mysource' AND NOT EXISTS (SELECT sign FROM `state` WHERE postlink.sign = state.sign AND `cite`='$mycite') ORDER BY RAND() LIMIT 2
注意:这个主题没有重复,因为我想随机选择非常条件,而不是简单的随机选择。
请帮忙。
谢谢。答案 0 :(得分:0)
对于此查询:
SELECT pl.*
FROM postlink pl
WHERE `source` = '$mysource' AND
NOT EXISTS (SELECT 1
FROM `state` s
WHERE pl.sign = s.sign AND s.`cite` = '$mycite'
)
ORDER BY RAND()
LIMIT 2;
我会从索引开始:postlink(source, sign)
和state(sign, cite)
。如果这不符合您的需求,那么我将研究改进随机选择的方法。