gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:40:19 25 4
gpt4 key购买 nike

问题是这个查询挂起或有无限记录,我不知道如何使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com