问题是此查询挂起或有无限记录,我不知道如何使用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条记录包含多条记录时,就会出现问题。
答案 0 :(得分:3)
[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;