This question already has answers here:
MySQL select 10 random rows from 600K rows fast
                                
                                    (25个答案)
                                
                        
                                3年前关闭。
            
                    
我想向此查询添加快速随机选择:

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


注意:本主题未重复,因为我想在非常条件的情况下随机选择而不是简单的随机选择。

请帮忙。

谢谢。

最佳答案

对于此查询:

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)开始。如果那不能满足您的需求,那么我将研究改善随机选择的方法。

10-02 14:34