我总是在以下查询中超时:
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
这需要 0.01 秒来执行,在这种特殊情况下返回 0 个结果:
select `id` from `purcheased` where `userId`=14 and `price`>100 and `reversed`=0
这在 0.02 秒内执行:
select * FROM `products`
where
`active`=1
and `group`= 6
order by `price` asc limit 0,500
完整的查询
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
执行 60 秒!
我认为这是因为 select id
from purcheased
... 正在为products
.
我正在mysql中运行查询。
如何告诉 mysqlid
从purcheased
一次执行选择而不是重新使用结果?