我运行了一些代码来编号列中的旧记录,以便在数据表子表单中对项目进行排序,但是在打开依赖于它的主表单时更新库存事务表中的列。它运行一个查询来确定客户余额到期,并且查询将永远运行2-3分钟或更长时间但是恢复到它运行正常的数据的旧副本,我所做的只是在一列中重新编号记录其中有1个代码重新编号记录:
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:
我今天继续玩这个,打破实际引用我更改的库存交易表的第二级子查询的查询运行正常,但是上升到subquery1的级别,引用subquery2和一个支付查询和它扼流圈但是,付款总额仅参考付款表。
编辑2:
我今天做了一些更多的测试Subquery2和支付总额查询的运行速度都超过了我用秒表的时间,但是当它们组合时它们大约需要1:45,子查询1中只有5列来自子查询2,1来自总和前4个中的3个是总和,最后一个是分组,然后来自付款总额的那个是分组
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;
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];
答案 0 :(得分:1)
nz()
等嵌入式功能会降低您的查询速度。它们也使您的SQL难以辨认。改进您的表格,因此不需要nz()
。例如,在设计视图中打开表,并为每个数字字段指定默认值零。 DISTINCTROW
关键字的情况下测试每个查询。查询应该运行得更快。我希望它什么都不做,因为我注意到每个查询在ID
子句中都有一个SELECT
记录。 (看看是否返回了相同数量的记录 - 是的,它什么也没做。)OrderID
对查询结果进行分组,该查询结果也按OrderID
进行分组。较高级GROUP BY
无效。这意味着你的一些SUM()
函数什么都不做......除了慢一点。Subquery 2
是一团糟(抱歉直言不讳)。你需要所有这些领域吗?如果是这样,至少可以通过缩短语法来怜悯我们:[Inventory Transactions].PriceChanged
可以表示为PriceChanged
。并且(至少在将来)避免使用表格和查询名称中的空格 - 您将save yourself trouble。[Line Total]
之类的计算在初步查询中可能会更好。在我看来,至少它会更清晰。 糟糕的设计是脆弱的:它因神秘的原因而破碎。良好的设计有助于查询功能,在这种情况下,您需要它来进行故障排除。修复查询通常需要
根据您的编辑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];