我有以下查询,它根据采购订单检查收货,以查看最初订购了哪些物品以及通过收货预订了多少物品。例如,我下达了 10 个香蕉奶昔的采购订单,然后我生成了一个收货单,说明我在所述采购订单上收到了 5 个这样的奶昔。
SELECT t.PONUM, t.ITMNUM, t.ordered,
SUM(t.received) as received,
t.ordered - ISNULL(SUM(t.received),0) as remaining,
SUM(t.orderedcartons) as orderedcartons,
SUM(t.cartonsreceived) as cartonsreceived,
SUM(t.remainingcartons) as remainingcartonsFROM(SELECT pod.PONUM,
pod.ITMNUM, pod.QTY as ordered, ISNULL(grd.QTYRECEIVED, 0) as received,
pod.DELIVERYSIZE as orderedcartons,
ISNULL(grd.DELIVERYSIZERECEIVED, 0) as cartonsreceived,
(pod.DELIVERYSIZE - ISNULL(grd.DELIVERYSIZERECEIVED, 0)) as remainingcartons
FROM TBLPODETAILS pod
LEFT OUTER JOIN TBLGRDETAILS grd
ON pod.PONUM = grd.PONUM and pod.ITMNUM = grd.ITMNUM) t
GROUP BY t.ITMNUM, t.PONUM, t.ordered
ORDER BY t.PONUM
它返回以下数据:
PONUM ITMNUM ordered received remaining orderedcartons cartonsreceived remainingcartons
1 1 5.0000 3.0000 2.0000 5.0000 3.0000 2.0000
接下来,我有一个 C# 循环来根据从上述查询返回的数据生成更新查询:
foreach (DataRow POUpdate in dt.Rows) {...
query += "UPDATE MYTABLE SET REMAININGITEMS=" + remainingQty.ToString()
+ ", REMAININGDELIVERYSIZE=" + remainingBoxes.ToString() + " WHERE ITMNUM="
+ itemNumber + " AND PONUM=" + poNumber + ";";
然后我对数据库执行每个更新查询。这在我的本地开发机器上运行良好。
但是,部署到生产服务器会在第一次查询中撤回超过 150,000 条记录。
所以循环这么多行会锁定 SQL 和我的应用程序。是前锋吗?是将所有数据加载到内存中的原始选择吗?两个都?我可以将此查询合并为一个查询并删除 C# 循环吗?如果是这样,实现这一目标的最有效方法是什么?