2

为什么在使用 UNION 查询而不是 UNION ALL 时会出现溢出错误?UNION 每一侧的查询单独运行良好,每个查询所作用的表是相同的,即具有完全相同结构的实时数据表和存档数据表。

查询作品:

SELECT Format(CVDate([DateID] & " " & [TimeID]),"yyyy/mm/dd hh:nn:ss") & "-" & [RecNum] AS DateTimeRecNumID, 
    [QC Records].WONum, 
    [QC Records].InspName1, 
    [QC Records].QtyInsp, 
    Nz([008],0)+Nz([011],0)+Nz([012],0)+Nz([013],0)+Nz([014],0)+Nz([015],0)+Nz([016],0)+Nz([017],0)+Nz([018],0)+Nz([019],0)+Nz([020],0)+Nz([022],0)+Nz([023],0)+Nz([029],0)+Nz([035],0)+Nz([036],0)+Nz([037],0)+Nz([040],0)+Nz([041],0)+Nz([044],0)+Nz([045],0)+Nz([046],0)+Nz([047],0)+Nz([048],0)+Nz([050],0)+Nz([052],0)+Nz([055],0)+Nz([057],0)+Nz([059],0)+Nz([060],0)+Nz([062],0)+Nz([064],0)+Nz([066],0)+Nz([070],0)+Nz([072],0)+Nz([075],0)+Nz([077],0)+Nz([080],0)+Nz([081],0)+Nz([082],0)+Nz([083],0)+Nz([084],0)+Nz([085],0)+Nz([086],0)+Nz([088],0)+Nz([095],0)+Nz([096],0)+Nz([097],0)+Nz([111],0)+Nz([113],0)+Nz([115],0)+Nz([116],0)+Nz([117],0)+Nz([118],0)+Nz([119],0)+Nz([120],0)+Nz([121],0)+Nz([122],0)+Nz([123],0)+Nz([124],0)+Nz([125],0)+Nz([126],0)+Nz([127],0)+Nz([128],0)+Nz([129],0)+Nz([130],0)+Nz([130],0) AS SumFaults,
    1-([SumFaults]/[QtyInsp]) AS PassRate,
    [QC Records].ItemNum
FROM [QC Records]
WHERE ((([QC Records].ProdSrc)="Production"))
UNION ALL
SELECT Format(CVDate([DateID] & " " & [TimeID]),"yyyy/mm/dd hh:nn:ss") & "-" & [RecNum] AS DateTimeRecNumID,
    [QC Records (Archive)].WONum,
    [QC Records (Archive)].InspName1,
    [QC Records (Archive)].QtyInsp,
    Nz([008],0)+Nz([011],0)+Nz([012],0)+Nz([013],0)+Nz([014],0)+Nz([015],0)+Nz([016],0)+Nz([017],0)+Nz([018],0)+Nz([019],0)+Nz([020],0)+Nz([022],0)+Nz([023],0)+Nz([029],0)+Nz([035],0)+Nz([036],0)+Nz([037],0)+Nz([040],0)+Nz([041],0)+Nz([044],0)+Nz([045],0)+Nz([046],0)+Nz([047],0)+Nz([048],0)+Nz([050],0)+Nz([052],0)+Nz([055],0)+Nz([057],0)+Nz([059],0)+Nz([060],0)+Nz([062],0)+Nz([064],0)+Nz([066],0)+Nz([070],0)+Nz([072],0)+Nz([075],0)+Nz([077],0)+Nz([080],0)+Nz([081],0)+Nz([082],0)+Nz([083],0)+Nz([084],0)+Nz([085],0)+Nz([086],0)+Nz([088],0)+Nz([095],0)+Nz([096],0)+Nz([097],0)+Nz([111],0)+Nz([113],0)+Nz([115],0)+Nz([116],0)+Nz([117],0)+Nz([118],0)+Nz([119],0)+Nz([120],0)+Nz([121],0)+Nz([122],0)+Nz([123],0)+Nz([124],0)+Nz([125],0)+Nz([126],0)+Nz([127],0)+Nz([128],0)+Nz([129],0)+Nz([130],0)+Nz([130],0) AS SumFaults,
    1-([SumFaults]/[QtyInsp]) AS PassRate,
    [QC Records (Archive)].ItemNum
FROM [QC Records (Archive)]
WHERE ((([QC Records (Archive)].ProdSrc)="Production"));

不起作用(区别是 UNION 之后的“ALL”:

SELECT Format(CVDate([DateID] & " " & [TimeID]),"yyyy/mm/dd hh:nn:ss") & "-" & [RecNum] AS DateTimeRecNumID, 
    [QC Records].WONum, 
    [QC Records].InspName1, 
    [QC Records].QtyInsp, 
    Nz([008],0)+Nz([011],0)+Nz([012],0)+Nz([013],0)+Nz([014],0)+Nz([015],0)+Nz([016],0)+Nz([017],0)+Nz([018],0)+Nz([019],0)+Nz([020],0)+Nz([022],0)+Nz([023],0)+Nz([029],0)+Nz([035],0)+Nz([036],0)+Nz([037],0)+Nz([040],0)+Nz([041],0)+Nz([044],0)+Nz([045],0)+Nz([046],0)+Nz([047],0)+Nz([048],0)+Nz([050],0)+Nz([052],0)+Nz([055],0)+Nz([057],0)+Nz([059],0)+Nz([060],0)+Nz([062],0)+Nz([064],0)+Nz([066],0)+Nz([070],0)+Nz([072],0)+Nz([075],0)+Nz([077],0)+Nz([080],0)+Nz([081],0)+Nz([082],0)+Nz([083],0)+Nz([084],0)+Nz([085],0)+Nz([086],0)+Nz([088],0)+Nz([095],0)+Nz([096],0)+Nz([097],0)+Nz([111],0)+Nz([113],0)+Nz([115],0)+Nz([116],0)+Nz([117],0)+Nz([118],0)+Nz([119],0)+Nz([120],0)+Nz([121],0)+Nz([122],0)+Nz([123],0)+Nz([124],0)+Nz([125],0)+Nz([126],0)+Nz([127],0)+Nz([128],0)+Nz([129],0)+Nz([130],0)+Nz([130],0) AS SumFaults,
    1-([SumFaults]/[QtyInsp]) AS PassRate,
    [QC Records].ItemNum
FROM [QC Records]
WHERE ((([QC Records].ProdSrc)="Production"))
UNION  
SELECT Format(CVDate([DateID] & " " & [TimeID]),"yyyy/mm/dd hh:nn:ss") & "-" & [RecNum] AS DateTimeRecNumID,
    [QC Records (Archive)].WONum,
    [QC Records (Archive)].InspName1,
    [QC Records (Archive)].QtyInsp,
    Nz([008],0)+Nz([011],0)+Nz([012],0)+Nz([013],0)+Nz([014],0)+Nz([015],0)+Nz([016],0)+Nz([017],0)+Nz([018],0)+Nz([019],0)+Nz([020],0)+Nz([022],0)+Nz([023],0)+Nz([029],0)+Nz([035],0)+Nz([036],0)+Nz([037],0)+Nz([040],0)+Nz([041],0)+Nz([044],0)+Nz([045],0)+Nz([046],0)+Nz([047],0)+Nz([048],0)+Nz([050],0)+Nz([052],0)+Nz([055],0)+Nz([057],0)+Nz([059],0)+Nz([060],0)+Nz([062],0)+Nz([064],0)+Nz([066],0)+Nz([070],0)+Nz([072],0)+Nz([075],0)+Nz([077],0)+Nz([080],0)+Nz([081],0)+Nz([082],0)+Nz([083],0)+Nz([084],0)+Nz([085],0)+Nz([086],0)+Nz([088],0)+Nz([095],0)+Nz([096],0)+Nz([097],0)+Nz([111],0)+Nz([113],0)+Nz([115],0)+Nz([116],0)+Nz([117],0)+Nz([118],0)+Nz([119],0)+Nz([120],0)+Nz([121],0)+Nz([122],0)+Nz([123],0)+Nz([124],0)+Nz([125],0)+Nz([126],0)+Nz([127],0)+Nz([128],0)+Nz([129],0)+Nz([130],0)+Nz([130],0) AS SumFaults,
    1-([SumFaults]/[QtyInsp]) AS PassRate,
    [QC Records (Archive)].ItemNum
FROM [QC Records (Archive)]
WHERE ((([QC Records (Archive)].ProdSrc)="Production"));

错误是“溢出”

我还尝试对 UNION ALL 查询执行 SELECT DISTINCT 查询,它也给了我“溢出”错误......??

4

2 回答 2

2

“为什么在使用 UNION 查询而不是 UNION ALL 时会出现溢出错误??”

您表示您知道UNION仅返回不同的行,并UNION ALL返回包括重复项在内的所有行。使用 just UNION,数据库引擎必须对初始工作集进行排序,以便识别哪些是重复行并将它们从最终结果集中消除。这种排序操作给 db 引擎带来了巨大的额外负担,这顺便解释了为什么UNION ALL总是比 快UNION,而且通常快得多。

在您的情况下,各个SELECT语句已经给数据库引擎带来了不寻常的负担。我以前从未见过使用Nz()67 次并将所有这些值相加得出一个计算字段的 Access 查询。并且数据库引擎必须为每个源表的每一行再次执行该工作。

Access 可以应付一个人的工作量SELECT。当你们UNION ALL两个时,它仍然可以应付。但是,当您对UNION.

如果您的下一个问题是“我能做什么?” ,我建议你先看看源表。查询的复杂性SELECT是表设计的结果。如果无法更改,至少考虑是否可以运行UPDATE语句将源表中的 Null 替换为 0;这可能会减轻足够UNION简单(即没有所有这些Nz调用)SELECT查询成功的负担。

作为最后的手段,考虑将每个结果集附加SELECT到临时表中:

INSERT INTO scratch_table
    (
        DateTimeRecNumID,
        WONum,
        InspName1,
        QtyInsp,
        SumFaults,
        PassRate,
        ItemNum
    )
SELECT ...

那么Access也许可以处理...

SELECT DISTINCT *
FROM scratch_table;
于 2013-08-28T15:12:23.030 回答
0

也许您的源字段包含一些#Num!由 0 个其他错误引起的除法。确保要与其进行联合的查询记录中没有错误,而不仅仅是顶级结果。您可能需要滚动或使用过滤器来检查这个假设。

于 2015-07-21T20:20:45.037 回答