更新时间:2023-11-30 21:45:22
Access'数据库引擎无法为Not In
使用索引,因此它一定很慢.有了CustomerId的索引,此查询应该会更快,因为数据库引擎可以使用该索引.
Access' database engine can't use an index for Not In
, so it's bound to be slow. With an index on CustomerId, this query should be much faster because the db engine can use the index.
SELECT DISTINCT blue.CustomerId
FROM
ProductSales AS blue
LEFT JOIN
(
SELECT CustomerId
FROM ProductSales
WHERE Product = 'RED'
) AS red
ON blue.CustomerId = red.CustomerId
WHERE
blue.Product = 'BLUE'
AND red.CustomerId Is Null;
您可能还可以尝试使用Not Exists
方法,但是不能保证在那里使用索引.另外,请参阅以下David Fenton的评论,其中更详细地讨论了性能影响.
You could probably also try a Not Exists
approach, but index use there is not guaranteed. Also, please see the comment below from David Fenton which discusses performance impact in more detail.