mardi 3 mai 2016

Select random record from mysql with multiple filters

I know this was discussed many times but my research did not help me with my problem.

I have a table (innodb) with about 3k records. I need to pick 1 row random with some filters, which i do it like this:

select id, title, topic_id 
from posts 
where id not in 
(select post_id from records where user_id='$my_id' and checked='1') 
and topic_id='$topic_id' and status='1' 
order by RAND() limit 1

This gives me the result i wanted. The problem is this takes too much time even with 3k records. It will get slower when records are increased.

I have to find a solution for this. Any suggestions?




Aucun commentaire:

Enregistrer un commentaire