Using Set Processing Examples 使用集合处理示例
Each of the following topics contains an example of set processing.
以下每个主题都包含一个集处理示例。
Payroll 工资单
In this example, suppose the payroll department needs to give a 1000 USD salary increase to everyone whose department made more than 50,000 USD profit. The following pseudocode enables you to compare the row-by-row and set-based approaches.
在这个例子中,假设工资部门需要给部门利润超过50,000美元的每个人增加1000美元的工资。下面的伪代码使您能够比较逐行和基于集合的方法。
• Row-by-Row:
• 一行一行
declare A cursor for select dept_id from department where profit > 50000;
open A;
fetch A into p_dept_id
while sql_status == OK
update personnel set salary = (salary+1000) where dept_id = p_dept_id;
fetch A into p_dept_id;
end while; close A; free A;
• Set-Based:
• 基于集合的
update personnel set salary = (salary + 1000)
where exists
(select 'X' from department where profit > 50000 and personnel.dept_id = department.dept_id)
Note: The set-based example employs a correlated subquery, which is important in set-based processing.
注意:基于集合的示例使用了一个相关的子查询,这在基于集合的处理中很重要。
Temporary Tables
One technique for improving database performance is to use temporary tables to hold the results of common subqueries. Effective dating and setID indirection are common types of subqueries that you can replace with joins to temporary tables. With the joins in place, you can access the temporary table instead of doing the subquery multiple times. Not only do most databases prefer joins to subqueries, but if you combine multiple subqueries into a single join as well, the performance benefits can be significant.
提高数据库性能的一种技术是使用临时表来保存公共子查询的结果。有效日期和setID间接寻址是常见的子查询类型,您可以使用到临时表的联接来替换它们。连接到位后,您可以访问临时表,而不是多次执行子查询。不仅大多数数据库更喜欢连接而不是子查询,而且如果将多个子查询合并到一个连接中,性能方面的好处也会非常显著。
In this setID indirection example, you see a join from a transaction table (keyed by BUSINESS_UNIT and ACCOUNTING_DT) to a setup table (keyed by SETID and EFFDT).
在这个setID间接寻址示例中,您将看到从事务表(以BUSINESS_UNIT和ACCOUNTING_DT为键)到设置表(以SETID和EFFDT为键)的连接。
If using a single SQL statement, you need to bring in PS_SET_CNTRL_REC to map the business unit to a corresponding setID. Typically, you do this in a subquery. You also need to bring in the setup table a second time in a subquery to get the effective date (MAX(EFFDT) <= ACCOUNTING_DT). If you have a series of similar statements, performance may be negatively affected.
如果使用单个SQL语句,则需要引入PS_SET_CNTRL_REC来将业务单元映射到相应的setID。通常,在子查询中执行此操作。您还需要在子查询中第二次引入安装表以获得生效日期(MAX(EFFDT)<=Accountingdt)。如果你有一系列类似的语句,性能可能会受到负面影响。
The alternative is to use a temporary table that is the equivalent of the setup table. The temporary table is keyed by BUSINESS_UNIT and ACCOUNTING_DT instead of SETID and EFFDT. You populate it initially by joining in your batch of transactions (presumably also a temporary table) once, as described previously, to get all the business units and accounting dates for this batch. From then on, your transaction and setup temporary tables have common keys, which allow a straight join with no subqueries.
另一种方法是使用一个临时表,它相当于设置表。临时表由Business_Unit和Account_DT键组成,而不是SETID和EFFDT。如前所述,首先通过加入您的事务批(假定也是一个临时表)一次来填充它,以获得此批的所有业务单元和会计日期。从那时起,您的事务和设置临时表就有了公共键,这允许不使用子查询的直接联接。
For the example, the original setup table (PS_ITEM_ENTRY_TBL) is keyed by SETID, ENTRY_TYPE and EFFDT.
例如,原始设置表(PS_ITEM_ENTRY_TBL)由SETID、ENTRY_TYPE和EFFDT作为键。
The denormalized temporary table version (PS_ITEM_ENTRY_TAO) is keyed by
PROCESS_INSTANCE, BUSINESS_UNIT, ENTRY_TYPE and ACCOUNTING_DT, and carries the
original keys (SETID and EFFDT) as simple attributes for joining to other related setup tables, as in PS_ITEM_LINES_TBL for this example.
非规范化的临时表版本(PS_ITEM_ENTRY_TAO)由PROCESS_INSTANCE、BUSINESS_UNIT、ENTRY_TYPE和ACCOUNTING_DT,并携带原始键(SETID和EFFDT)作为连接到其他相关设置表的简单属性,如本例中的PS_ITEM_LINES_TBL。
If the program references the setup table in only one Insert/Select or Select statement, you would not see increased performance by denormalizing the temporary table. But if several SQL statements are typically executed in a single run, all of which join in the same setup table with similar setID and effective date considerations, then the performance cost of populating the temporary table initially provides long-term advantages.
如果程序只在一个Insert/Select或Select语句中引用设置表,那么将不会看到通过反规范化临时表来提高性能。但是,如果几条SQL语句通常在一次运行中执行,所有这些语句都加入到具有类似setID和有效日期考虑的同一个设置表中,那么填充临时表的性能成本最初提供了长期优势。
• Original setup table version:
原始设置表版本:
INSERT INTO PS_PG_PENDDST_TAO (...)
SELECT . . . . . ( (I.ENTRY_AMT_BASE - I.VAT_AMT_BASE) * L.DST_LINE_MULTIPLR * L.DST_LINE_PERC⇒ ENT / 100 ), ( (I.ENTRY_AMT - I.VAT_AMT) * L.DST_LINE_MULTIPLR * L.DST_LINE_⇒
PERCENT / 100 ), . . . . .
FROM PS_PENDING_ITEM I, PS_PG_REQUEST_TAO R, PS_ITEM_LINES_TBL L,
PS_ITEM_ENTRY_TBL E, PS_SET_CNTRL_REC S, PS_BUS_UNIT_TBL_AR B
. . . . .WHERE AND L.ENTRY_REASON = I.ENTRY_REASON AND L.SETID = E.SETID AND ⇒
L.ENTRY_TYPE = E.ENTRY_TYPE AND L.EFFDT = E.EFFDT. . . . . AND E.EFF_STATUS = 'A'
AND S.RECNAME = 'ITEM_ENTRY_TBL'
AND S.SETID = E.SETID
AND S.SETCNTRLVALUE = I.BUSINESS_UNIT
AND E.ENTRY_TYPE = I.ENTRY_TYPE
AND E.EFFDT = (SELECT MAX(EFFDT) FROM PS_ITEM_ENTRY_TBL Z
WHERE Z.SETID = E.SETID
AND Z.ENTRY_TYPE = E.ENTRY_TYPE
AND Z.EFF_STATUS = 'A'
AND Z.EFFDT <= I.ACCOUNTING_DT )
AND B.BUSINESS_UNIT = I.BUSINESS_UNIT
/
• Denormalized temporary table version:
非正规化的临时表版本:
INSERT INTO PS_ITEM_ENTRY_TAO . . . . .
SELECT DISTINCT %BIND(PROCESS_INSTANCE), I.BUSINESS_UNIT, I.ACCOUNTING_DT, E.ENTRY_TYPE... . . .
FROM PS_PENDING_ITEM I, PS_PG_REQUEST_TAO R,
PS_ITEM_ENTRY_TBL E, PS_SET_CNTRL_REC S, PS_BUS_UNIT_TBL_AR B
WHERE R.PROCESS_INSTANCE = %BIND(PROCESS_INSTANCE)
AND R.PGG_GROUP_TYPE = 'B'
AND I.POSTED_FLAG = 'N'
AND R.GROUP_BU = I.GROUP_BU
AND R.GROUP_ID = I.GROUP_ID
AND E.EFF_STATUS = 'A'
AND S.RECNAME = 'ITEM_ENTRY_TBL'
AND S.SETID = E.SETID
AND S.SETCNTRLVALUE = I.BUSINESS_UNIT
AND E.ENTRY_TYPE = I.ENTRY_TYPE
AND E.EFFDT = ( SELECT MAX(EFFDT) FROM PS_ITEM_ENTRY_TBL Z
WHERE Z.SETID = E.SETID
AND Z.ENTRY_TYPE = E.ENTRY_TYPE
AND Z.EFF_STATUS = 'A'
AND Z.EFFDT <= I.ACCOUNTING_DT )
AND B.BUSINESS_UNIT = I.BUSINESS_UNIT
/
INSERT INTO PS_PG_PENDDST_TAO (...) SELECT ...
( (I.ENTRY_AMT_BASE - I.VAT_AMT_BASE) * L.DST_LINE_MULTIPLR * L.DST_LINE_PERC⇒
ENT / 100 ),
( (I.ENTRY_AMT - I.VAT_AMT) * L.DST_LINE_MULTIPLR * L.DST_LINE_PERCENT / 100 )⇒ ,
. . . . .
FROM PS_PENDING_ITEM I, PS_PG_REQUEST_TAO R, PS_ITEM_LINES_TBL L,
PS_ITEM_ENTRY_TAO E . . . . .
WHERE . . . . .
AND L.ENTRY_REASON = I.ENTRY_REASON
AND L.SETID = E.SETID
AND L.ENTRY_TYPE = E.ENTRY_TYPE
AND L.EFFDT = E.EFFDT . . . . .
AND E.BUSINESS_UNIT = I.BUSINESS_UNIT
AND E.ACCOUNTING_DT = I.ACCOUNTING_DT
AND E.ENTRY_TYPE = I.ENTRY_TYPE
/