我有两个表,分别命名为:
- table_product
- table_user_ownned_auction
table_product
specific_product_id astatus ...
(primary_key,autoinc)
--------------------------------------
1 APAST ...
2 ALIVE ...
3 ALIVE ...
4 APAST ...
5 APAST ...
用户拥有的拍卖own_id specific_product_id details
----------------------------------------
1 1 XXXX
2 5 XXXX
我需要选择 atatus = APAST
,并且不在表2中。
也就是说,在上述结构中,表1有3个APAST状态(1、4、5)。但是在表2中,只存储了specific_product_id(1、5),因此我需要选择specific_product_id = 4。
我使用了这个查询
SELECT *
FROM table_product
WHERE astatus = 'APAST'
AND specific_product_id NOT IN (SELECT specific_product_id
FROM table_user_ownned_auction )
...需要这么长时间:
查询耗时115.1039秒
...才能执行。
解释计划
我该如何优化它或选择其他方法来获取我想要的内容?
NOT
执行反转,将uoa.specific_product_id = p.specific_product_id
更改为uoa.specific_product_id != p.specific_product_id
。 - OMG PoniesLEFT JOIN/IS NULL
和NOT IN
之间没有性能差异。因此,可以得出结论,您可以始终使用NOT IN
并且总是安全的。引用文章中比较两者的话:“实际上,算法是相同的,查询完成时间也相同。” - Adam