我正在使用NOT IN在一个大约有10万行的表上运行下面的查询,性能非常糟糕。还有另一种方法可以达到同样的效果吗?
SELECT c.Id, c.Name, c.address, c.town,
c.county, c.postcode, FROM contractor AS c
inner join Order w
on w.Id = c.Id WHERE (c.valid = 1) and c.Id not in
(select w.Id from Order w) ORDER BY c.Name ASC
由于
答案 0 :(得分:2)
您似乎有一个冗余的内部联接Order
我相信以下查询会产生相同的结果,并可能提高性能。
SELECT c.Id
, c.Name
, c.address
, c.town
, c.county
, c.postcode
FROM contractor AS c
left outer join Order w on w.Id = c.Id
WHERE (c.valid = 1)
and w.Id IS NULL
ORDER BY
c.Name ASC
答案 1 :(得分:0)
我个人无法看到您的代码的进一步改进,除了您可能想要为您的字段添加索引。一般规则是为您在WHERE
子句中使用的列添加索引。