减少查询时间

时间:2016-12-28 11:32:46

标签: ms-access

我正在尝试运行以下查询,但由于数据量巨大需要花费太长时间:

SELECT pre.Count AS Precount, consol.Count AS commoncount, post.Count AS Postcount, Precount-commoncount AS deleted, postcount-commoncount AS added
FROM (SELECT COUNT([PRE_L]) AS [count] FROM PRE_LNADJL_2)  AS pre,
     (SELECT COUNT([PRE_L]) AS [count] FROM CD_LNADJL)  AS consol,
     (SELECT COUNT([Post_L]) AS [count] FROM POST_LNADJL_2)  AS post;

1 个答案:

答案 0 :(得分:0)

当我在一家审计公司工作时,我们曾经做过一个小技巧,我们的后端是SQL Server,我们正在处理一些有超过5B记录的表。我们要做的是"预运行"我们的报告。也就是说,我们会在一夜之间过滤我们的数据并将其写入新表。这样,当没有人需要数据时,所有更大,更耗时的计算都已完成。

我建议您运行On Timer事件集,以便在没有人在办公室时运行。在那种情况下,运行类似:

INSERT INTO pre SELECT COUNT([PRE_L]) AS [count] FROM PRE_LNADJL_2
INSERT INTO consol SELECT COUNT([PRE_L]) AS [count] FROM CD_LNADJL
INSERT INTO post SELECT COUNT([Post_L]) AS [count] FROM POST_LNADJL_2

(您也可以选择SELECT INTO' s,根据您是否希望每次运行时都必须删除/重新创建或清除数据持有者表,这两者都可以。)

然后,只要您的用户需要运行此报告或其他任何内容,SQL只需要:

SELECT pre.Count AS Precount, consol.Count AS commoncount, post.Count AS Postcount, 
       Precount-commoncount AS deleted, postcount-commoncount AS added
FROM (SELECT * FROM pre),
 (SELECT * FROM consol),
 (SELECT * FROM post);

应该在2秒内运行。