我一直都想與優化的MySQL查詢超時以下查詢:如何與內部查詢
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
此執行.02s:
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秒!
我認爲這是因爲選擇id
purcheased
...正在執行的products
的每一行。
我在mysql中運行查詢。
我該如何告訴mysql執行從purcheased
一次選擇id
並重新使用結果?
您的表名中的錯字:* purcheased *應該*購買* – RedFilter