I know that I have to avoid to use “IN” clause, but I don’t know how to do in this case. The scenario is this: from PHP I get a list of values and then I construct a query like this:
SELECT id FROM
( SELECT * FROM `tips` r
LEFT JOIN (SELECT tip FROM `tips_showed` WHERE user='8') AS a ON r.id=a.tip
WHERE a.tip IS NULL ) AS t
WHERE t.id IN
('3','4','5','2','6','7','8','9','10','18',
'11','12','13','14','15','16','17','20') LIMIT 1
This makes my site very slow (because it is executed each time a user visits it). How can make it faster?
I believe you can simplify the query to:
Using
NOT EXISTSinstead of theLEFT JOINmay also buy you some additional performace. Try each and compare.