1

我运行了一些代码来对列中的旧记录进行编号,以便在数据表子表单中对项目进行排序,但是在打开依赖它的主表单时更新了库存交易表中的列之后。它运行一个查询来确定到期的客户余额,并且该查询需要永远运行 2-3 分钟或更长时间,但是恢复到它运行良好的数据的旧副本,我所做的只是重新编号一列中的记录最其中有 1s 是重新编号记录的代码:

Dim rst As DAO.Recordset
Dim i As Long
Dim OID As Long
Dim stringSQL as String
i = 1
stringSQL = "SELECT * FROM [Inventory Transactions] " _
          & "WHERE [OrderID] >  0 Order By [OrderID],[TransactionID]"
Set rst = CurrentDb.OpenRecordset(stringSQL, dbOpenDynaset)
With rst
OID = rst!OrderID
    Do Until .EOF
        If OID = rst!OrderID Then
        .Edit
            !OrderLineNumber = i
        .Update
        .MoveNext
        i = i + 1
        Else
        OID = rst!OrderID
        i = 1
        End If
    Loop
    .Close
End With
Set rst = Nothing

编辑1:

我今天继续玩这个,并且打破第二级子查询的查询实际上引用了我更改的库存交易表运行良好,但上升到子查询1的级别,它引用了子查询2和支付总和查询,但它阻塞了总和付款仅参考付款表。

编辑2:

我今天做了更多测试 Subquery2 和付款总和查询的运行速度都比我用秒表计时的速度要快,但是当它们组合起来大约需要 1:45 时,subquery1 中只有 5 列来自 subquery2 的 4 列和来自付款总和 3 的 1前 4 个是 sum,最后一个是 group by,然后来自付款总和的一个是 group by

子查询 2:

SELECT 
  CLng((nz([UnitsSold])*nz([UnitPrice]))*(1-nz([Discount]))*100)/100 AS [Line Total], 
  CLng([Line Total]*(1+nz([SalesTaxRate]))*100)/100 AS [Line Total With Tax], 
  [Line Total With Tax]-[Line Total] AS [Line Tax], 
  [Inventory Transactions].* 
  FROM [Inventory Transactions] 
  WHERE ((([Inventory Transactions].OrderID) Is Not Null));

付款总和查询:

SELECT DISTINCTROW Payments.OrderID, 
  Sum(Payments.PaymentAmount) AS [Total Payments]
FROM Payments 
GROUP BY Payments.OrderID;

子查询 1:

SELECT 
  [Balance Due By Customers Subquery2].OrderID, 
  Sum([Balance Due By Customers Subquery2].[Line Total]) AS [SumOfLine Total], 
  Sum([Balance Due By Customers Subquery2].[Line Tax]) AS [SumOfLine Tax], 
  Sum([Balance Due By Customers Subquery2].[Line Total With Tax]) AS [SumOfLine Total With Tax], 
  [Sum Of Payments Query].[Total Payments]
FROM [Balance Due By Customers Subquery2] 
  LEFT JOIN [Sum Of Payments Query] 
  ON [Balance Due By Customers Subquery2].OrderID = [Sum Of Payments Query].OrderID
GROUP BY [Balance Due By Customers Subquery2].OrderID, 
  [Sum Of Payments Query].[Total Payments];
4

1 回答 1

1
  1. 嵌入式功能,例如nz()减慢查询速度。它们还会使您的 SQL 难以辨认。改进您的表格,因此nz()不需要。例如,在设计视图中打开您的表格,并为每个数字字段指定一个默认值零。
  2. DISTINCTROW在没有关键字的情况下测试每个查询。查询应该运行得更快。我希望它什么都不做,因为我注意到每个查询在子句中都有一条ID记录。SELECT(看看是否返回了相同数量的记录——是的,它什么也没做。)
  3. 您似乎正在对也分组OrderID的查询结果进行分组OrderID。高层GROUP BY什么都不做。这意味着您的某些SUM()功能什么都不做……除了速度很慢。
  4. Subquery 2是一团糟(抱歉直言不讳)。您需要所有这些字段吗?如果是这样,至少可以通过缩短语法来怜悯我们: [Inventory Transactions].PriceChanged可以表示为PriceChanged. 并且(至少在将来)避免在表和查询名称中使用空格——您将省去自己的麻烦
  5. 像这样的计算[Line Total]可能在初步查询中效果更好。在我看来,至少它会是更简洁的编程。
  6. 在每个句子的末尾使用句号。这将有助于我们理解您的帖子。

糟糕的设计是脆弱的:它会因为神秘的原因而损坏。良好的设计有助于查询功能,在这种情况下,您需要它来进行故障排除。修复查询通常需要

  1. 减少和简化,直到问题消失
  2. 建立所需的结果。(更多信息在这里。)

根据您的Edit 2,您需要在Subquery 2. 从结合了其他两个的最简单的查询开始。 离开聚合函数(SUM(), GROUP BY)——不知何故,它们把事情搞砸了。

在以更改数据结果的方式进行简化之前,您可以通过仅改进语法来简化这些查询。例如,这里是Subquery 1嵌套查询的别名——更容易阅读:

SELECT 
  BalanceDue.OrderID, 
  Sum(BalanceDue.[Line Total]) AS [SumOfLine Total], 
  Sum(BalanceDue.[Line Tax]) AS [SumOfLine Tax], 
  Sum(BalanceDue.[Line Total With Tax]) AS [SumOfLine Total With Tax], 
  SumPmts.[Total Payments]
FROM [Balance Due By Customers Subquery2] AS BalanceDue
  LEFT JOIN [Sum Of Payments Query] AS SumPmts
  ON BalanceDue.OrderID = SumPmts.OrderID
GROUP BY BalanceDue.OrderID, 
  SumPmts.[Total Payments];
于 2013-08-02T03:19:45.320 回答