I’m always getting a timed out on the following query:
select * FROM `products`
where
`active`=1
and `group`=6
and `id` not in (select `id` from `purcheased` where `userId`=14 and `price`>100
and `reversed`=0)
order by `price` asc limit 0,500
This takes 0.01s to execute, and in this particular case returns 0 results:
select `id` from `purcheased` where `userId`=14 and `price`>100 and `reversed`=0
This executes in .02s:
select * FROM `products`
where
`active`=1
and `group`= 6
order by `price` asc limit 0,500
The full query
select * FROM `products`
where
`active` = 1
and `group` = 6
and `id` not in (
select `id` from `purcheased`
where
`userId`=14
and `price` > 100
and `reversed`=0
)
order by `price` asc limit 0,500
executes it 60 seconds!
I think this is happening because select id from purcheased … is being executed for each row of the products.
I’m running the queries in mysql.
How do I tell mysql to execute the select id from purcheased once and than to re-use the result?
A
LEFT OUTER JOINis probably your best bet here: