应用场景:在优化过程中我们经常会碰到处理数据量上百万或者千万大数据内表的情况,本例介绍的是一种通过tabix减少读取数据量的思想。
注意事项: 1、首先需要排序
2、先通过二分查找read内表的方式找到我们数据所在的index
3、将sy-tabix赋给变量
4、通过 loop at itabinto wa_itab from l_v_tabix
Endloop,的方式指定读取的开始位置。
DATA: l_v_trans_idx TYPE sy-tabix,
l_v_trans_idx1 TYPE sy-tabix.
SORT l_i_trans BY bukrs kunnr agrid budat DESCENDING.
LOOP AT l_i_trans ASSIGNING <fs_trans>.
CLEAR l_v_balance.
l_v_balance = <fs_trans>-sp_balance.
READ TABLE fp_i_trans TRANSPORTING NO FIELDS
WITH KEY bukrs = <fs_trans>-bukrs
kunnr = <fs_trans>-kunnr
agrid = <fs_trans>-agrid BINARY SEARCH.
IF sy-subrc = 0.
MOVE sy-tabix TO l_v_trans_idx.
LOOP AT fp_i_trans INTO l_wa_trans FROM l_v_trans_idx.
IF l_wa_trans-bukrs <> <fs_trans>-bukrs OR
l_wa_trans-kunnr <> <fs_trans>-kunnr OR
l_wa_trans-agrid <> <fs_trans>-agrid.
EXIT.
ENDIF.
IF l_wa_trans-budat <= <fs_trans>-budat AND
l_wa_trans-ttype = c_ttype1 AND
l_wa_trans-sp_balance > 0.
l_v_balance = l_v_balance + l_wa_trans-sp_balance.
IF l_v_balance >= 0.
l_wa_trans-sp_balance = l_v_balance.
MODIFY fp_i_trans FROM l_wa_trans.
EXIT.
ELSE.
l_wa_trans-sp_balance = 0.
MODIFY fp_i_trans FROM l_wa_trans.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.
* 后进后出法对冲负数发放数据
SORT l_i_trans_ff BY bukrs kunnr agrid budat DESCENDING.
LOOP AT l_i_trans_ff ASSIGNING <fs_trans>.
CLEAR l_v_balance.
l_v_balance = <fs_trans>-ff_balance.
READ TABLE fp_i_trans TRANSPORTING NO FIELDS
WITH KEY bukrs = <fs_trans>-bukrs
kunnr = <fs_trans>-kunnr
agrid = <fs_trans>-agrid BINARY SEARCH.
IF sy-subrc = 0.
MOVE sy-tabix TO l_v_trans_idx1.
LOOP AT fp_i_trans INTO l_wa_trans FROM l_v_trans_idx1.
IF l_wa_trans-bukrs <> <fs_trans>-bukrs OR
l_wa_trans-kunnr <> <fs_trans>-kunnr OR
l_wa_trans-agrid <> <fs_trans>-agrid.
EXIT.
ENDIF.
IF l_wa_trans-budat <= <fs_trans>-budat AND
l_wa_trans-ttype <> c_ttype1 AND
l_wa_trans-ff_balance > 0.
l_v_balance = l_v_balance + l_wa_trans-ff_balance.
IF l_v_balance >= 0.
l_wa_trans-ff_balance = l_v_balance.
MODIFY fp_i_trans FROM l_wa_trans.
EXIT.
ELSE.
l_wa_trans-ff_balance = 0.
MODIFY fp_i_trans FROM l_wa_trans.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.