慢 SQL:Select Table2 中缺少 Table1 值的计数

Slow SQL: Select count of Table1 values missing from Table2

我正在尝试 return 计算表 1 中不存在于表 2 中的值的数量,我有一个看似正确的查询,但它 运行 非常慢:

SELECT count(container_no) 
FROM pier_data
WHERE NOT EXISTS(
    SELECT 1 
    FROM Iron_mountain_data 
    WHERE pier_data.container_no = iron_mountain_data.[Customer Box Nbr]
    );

有什么办法可以加快速度吗?

编辑:无论出于何种原因,我相信此 LEFT JOIN 业务在 MS Access 中为我提供了最方便的结果:

SELECT Count(container_no) AS boxes_missing_from_IM
FROM pier_data AS pd LEFT JOIN iron_mountain_data AS imd ON pd.container_no = 
imd.[Customer Box Nbr]
WHERE imd.[Customer Box Nbr] Is Null;

这可能会更好,试试吧:

SELECT COUNT(container_no) FROM pier_date
WHERE container_no NOT IN
(
     SELECT [Customer Box Nbr] FROM iron_mountain_data 
);

此外,正如所建议的那样,您可以使用带 where 子句的左连接,如下所示:

SELECT COUNT(container_no) FROM pier_date pd
LEFT JOIN iron_mountain_data imd ON pd.container_no = imd.[Customer Box Nbr]
WHERE imd.[Customer Box Nbr] IS NULL

关键字'DISTINCT'的使用可能也很有用

COUNT(DISTINCT container_no)

SELECT DISTINCT [Customer Box Nbr]

假设 [container_no] 和 [Customer box nbr] 捕获相同的信息

    SELECT  COUNT(container_no) 
    FROM pier_date pd
    WHERE   container_no NOT IN (
                                    SELECT DISTINCT [Customer Box Nbr] 
                                    FROM            iron_mountain_data
                                )

您的查询很好,尽管我会使用 COUNT(*) 和 table 别名:

SELECT COUNT(*) 
FROM pier_data as pd
WHERE NOT EXISTS (SELECT 1 
                  FROM Iron_mountain_data as imd
                  WHERE pd.container_no = imd.[Customer Box Nbr]
                 );

(这些更改对性能没有影响,除了非常、非常、非常小的检查 container_no 不是 COUNT()NULL。)

为了性能,您需要 Iron_mountain_data([Customer Box Nbr]):

上的索引
create index idx_iron_mount_data_customer_box_nbr on Iron_mountain_data([Customer Box Nbr];

这几乎适用于您编写查询的任何方式。