0

i want adding fast random select to this query :

SELECT * FROM postlink WHERE `source`='$mysource' AND NOT EXISTS (SELECT sign FROM `state` WHERE postlink.sign = state.sign AND `cite`='$mycite') LIMIT 2

i used this but that is very slow :

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

note : this topic not duplicated because i want to random select with very Condition and not a simple random select.

please help.

thank you.

SAHAR
  • 1
  • 1

1 Answers1

0

For this query:

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;

I would start with indexes: postlink(source, sign) and state(sign, cite). If that doesn't meet your needs, then I'd look at ways of improving the random selection.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • OK, but i use these postlink(source, sign) and state(sign, cite) to not view repeated row but if you think no creates any problem please continue and help to improving the random selection for my query. thank you. – SAHAR Feb 23 '16 at 14:24