使用 Fieldlist 优化数据库通信
Optimize database communication using Fieldlist
我正在阅读 Development 3 课程,在某个时候,这个讨论开始了——使用 fieldlist 优化与数据库的通信。
例如:
while select AmountCurCredit from ledgerJournalTrans
{
amountCurCredit += ledgerTrans.AmountCurCredit;
}
我明白这一点,我们只 selecting AmountCurCredit 字段而不是其他字段。因此,如果我们尝试对 AmountCurDebit 求和,结果将是 0。
好的,但是这个呢:
while select recId from inventTable
join qty from inventTrans
where inventTrans.itemId == inventTable.itemId
{
qty += inventTrans.qty;
}
itemId 不是 select 语句的一部分。它如何获得价值?是因为它被定义为 InventTable 和 InventTrans 表的索引吗?
我的意思是,我们不应该使用:
while select ItemId from inventTable
join qty, ItemId from inventTrans
where inventTrans.ItemId == inventTable.ItemId
{
qty += inventTrans.qty;
}
您不需要 select ItemId
因为您不在 while 正文中使用它。
您的查询转换为 SQL:
SELECT InventTable.RecId
, InventTrans.Qty
FROM InventTable
INNER JOIN InventTrans
ON InventTrans.ItemId = InventTable.ItemId
更优化的说法是:
while select qty from inventTrans
exists join recId
from inventTable
where inventTable.ItemId == inventTrans.ItemId
{
qty += inventTrans.qty;
}
转化为
SELECT T1.QTY
FROM INVENTTRANS T1
WHERE EXISTS
(
SELECT 'x'
FROM INVENTTABLE T2
WHERE T2.ITEMID=T1.ITEMID
)
更优化:
select sum(Qty)
from inventTrans
exists join recId
from inventTable
where inventTable.ItemId == inventTrans.ItemId
;
转化为
SELECT SUM(T1.QTY)
FROM INVENTTRANS T1
WHERE EXISTS
(
SELECT 'x'
FROM INVENTTABLE T2
WHERE T2.ITEMID=T1.ITEMID
)
我正在阅读 Development 3 课程,在某个时候,这个讨论开始了——使用 fieldlist 优化与数据库的通信。
例如:
while select AmountCurCredit from ledgerJournalTrans
{
amountCurCredit += ledgerTrans.AmountCurCredit;
}
我明白这一点,我们只 selecting AmountCurCredit 字段而不是其他字段。因此,如果我们尝试对 AmountCurDebit 求和,结果将是 0。
好的,但是这个呢:
while select recId from inventTable
join qty from inventTrans
where inventTrans.itemId == inventTable.itemId
{
qty += inventTrans.qty;
}
itemId 不是 select 语句的一部分。它如何获得价值?是因为它被定义为 InventTable 和 InventTrans 表的索引吗?
我的意思是,我们不应该使用:
while select ItemId from inventTable
join qty, ItemId from inventTrans
where inventTrans.ItemId == inventTable.ItemId
{
qty += inventTrans.qty;
}
您不需要 select ItemId
因为您不在 while 正文中使用它。
您的查询转换为 SQL:
SELECT InventTable.RecId
, InventTrans.Qty
FROM InventTable
INNER JOIN InventTrans
ON InventTrans.ItemId = InventTable.ItemId
更优化的说法是:
while select qty from inventTrans
exists join recId
from inventTable
where inventTable.ItemId == inventTrans.ItemId
{
qty += inventTrans.qty;
}
转化为
SELECT T1.QTY
FROM INVENTTRANS T1
WHERE EXISTS
(
SELECT 'x'
FROM INVENTTABLE T2
WHERE T2.ITEMID=T1.ITEMID
)
更优化:
select sum(Qty)
from inventTrans
exists join recId
from inventTable
where inventTable.ItemId == inventTrans.ItemId
;
转化为
SELECT SUM(T1.QTY)
FROM INVENTTRANS T1
WHERE EXISTS
(
SELECT 'x'
FROM INVENTTABLE T2
WHERE T2.ITEMID=T1.ITEMID
)