sql - 使用 NOT IN 子查询的 SELECT INTO 查询需要很长时间/挂起

标签 sql ms-access indexing ms-access-2010 ms-access-2013

问题是这个查询挂起或有无限记录,我不知道如何使用 MS ACCESS 修复:

预期的用户输入:

User input Start Date: 1/15/2015
User input End Date: 11/15/2015
User input Upper Data Threshold in kB: 50

源表:

[Master] Table in Access:
Invc Date  Mobile Nbr     PktDtVol   
---------  ----------     --------   
1/15/15   647-409-8206    48kB
2/15/15   647-409-8206    33kB
3/15/15   647-409-8206    8000kB
4/15/15   647-409-8206    20kB
5/15/15   647-409-8206    10kB
6/15/15   647-409-8206    0kB
7/15/15   718-500-2311    3kB
8/15/15   718-500-2311    45kB
9/15/15   718-500-2311    25kB
10/15/15  514-300-3311    33kB
11/15/15  514-300-3311    20kB

[Temp_Table] 中的输出:

Invc Date  Mobile Nbr     PktDtVol    Difference in Days 
---------  ----------     --------   -------------------
7/15/15    718-500-2311    3kB             304
8/15/15    718-500-2311    45kB            304
9/15/15    718-500-2311    25kB            304
10/15/15   514-300-3311    33kB            304
11/15/15   514-300-3311    20kB            304

接受的 SQL 解决方案生成以上输出:

PARAMETERS [Start Date] DateTime, [End Date] DateTime, [Upper Bound Usage in KB] IEEEDouble; 
SELECT m.[Invc Date], m.PktDtVol, m.[Mobile Nbr], DateDiff("d",[Start Date],[End Date]) AS [Difference in days] 
INTO Temp_Table FROM Master AS m 
WHERE (m.[Invc Date]>=[Start Date] And m.[Invc Date])<=[End Date] AND m.[Mobile Nbr] NOT IN 
(SELECT q.[Mobile Nbr] FROM Master AS q WHERE (q.PktDtVol>=[Upper Bound Usage in KB]));

从这里开始,我尝试通过另一个 SQL 语句修改表来创建索引来优化查询,但不起作用:

 CREATE INDEX Index2 ON Master([Ttl Charges])

查询工作正常,源表中的 10 条记录没有挂起,多条记录具有预期的输出。但是当源表中有 56,000 条记录且有多个记录时,问题就出现了。

最佳答案

[Ttl Charges] 上的索引不会对您有任何帮助,但 [PktDtVol] 上的索引会。我刚刚对 10,000 行进行了测试,[PktDtVol] 上缺少索引绝对是性能瓶颈:

Indexes:
  none
Time:
  170 seconds (just under 3 minutes)

Indexes:
  [Invc Date]
  [Mobile Number]
Time:
  (same as before)

Indexes:
  [Invc Date]
  [Mobile Number]
  [PktDtVol]
Time:
  36 seconds

为了进一步提高性能,您可以重新制定查询以使用 LEFT JOIN 而不是带有子查询的 NOT IN 子句,正如您在评论中提到的:

PARAMETERS [Start Date] DateTime, [End Date] DateTime, [Upper Bound Usage in KB] IEEEDouble; 
SELECT 
    [Master].[Invc Date], 
    [Master].PktDtVol, 
    [Master].[Mobile Nbr], 
    DateDiff("d",[Start Date],[End Date]) AS [Difference in days] 
INTO Temp_Table 
FROM 
    [Master] 
    LEFT OUTER JOIN 
    (
        SELECT DISTINCT q.[Mobile Nbr] FROM Master AS q 
        WHERE (q.PktDtVol>=[Upper Bound Usage in KB])
    ) s 
        ON [Master].[Mobile Nbr] = s.[Mobile Nbr] 
WHERE 
    [Master].[Invc Date] >= [Start Date]
        AND [Master].[Invc Date] <= [End Date]
        AND s.[Mobile Nbr] IS NULL;

关于sql - 使用 NOT IN 子查询的 SELECT INTO 查询需要很长时间/挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877852/

相关文章:

sql - 使用计数我只得到一行

mysql - 使用一条语句从同一个表中的不同行中选择数据

c# - 为任意参数可以为空的两个表选择记录

java - 如何检查(甚至设置)mdb(ms access)文件中的排序规则?

swift - ClosedRange<Int>.Swift 中 Int 的索引?

php - 用重复值更新行

r - 从 R (x64) 连接到 MS Access 数据库

database - 将多个 (csv) 文件导入 Access 数据库的任何方式

excel - 根据下拉列表选择使用其他表中的数据填充空表

postgresql - 成本降低了,但时间几乎相同,为什么?