如何提高 2 个表在 abap 中具有大量条目的嵌套循环的性能?
how to improve the performance in nested loops with 2 tables having huge number of entries in abap?
两个 table 均按键 KNO
排序
LOOP AT lt_header INTO lwa_header.
LOOP AT lt_items INTO lwa_item
WHERE key = lwa_header-KNO
“……….
ENDLOOP.
ENDLOOP.
如果 table 中的条目数很大,这将需要更多时间来执行。我应该如何修改代码以提高性能?
您应该能够通过使用辅助 table 键改进第二个 table 中的查找。声明table变量时需要声明这样的key:
DATA lt_items TYPE TABLE OF whatever WITH NON-UNIQUE SORTED KEY k1 COMPONENTS key.
然后您可以使用此键来加速 table 从 O(n)
或 O(log n)
时间复杂度的查找:
LOOP AT lt_header INTO lwa_header.
LOOP AT lt_items INTO lwa_item
USING KEY k1
WHERE key = lwa_header-KNO.
"...
ENDLOOP.
ENDLOOP.
当你能保证lt_items-key中的值总是唯一的(没有两行“key”的值相同),那么你甚至可以使用散列键,这样会更快(常数时间):
DATA lt_items TYPE TABLE OF whatever WITH UNIQUE HASHED KEY k1 COMPONENTS key.
LOOP AT lt_header INTO lwa_header.
READ TABLE lt_items INTO lwa_item
WITH TABLE KEY k1
COMPONENTS key = lwa_header-KNO.
IF sy-subrc = 0.
"...
ENDIF.
ENDLOOP.
您可以使用平行光标。这是提高嵌套循环性能的好方法。有关详细信息,请查看此 link.
字段符号也更利于性能。
DATA lv_tabix TYPE sy-tabix.
SORT: lt_header BY kno,
lt_items BY kno.
LOOP AT lt_header ASSIGNING FIELD-SYMBOL(<lfs_header>).
READ TABLE lt_items TRANSPORTING NO FIELDS
WITH KEY kno = <lfs_header>-kno
BINARY SEARCH.
lv_tabix = sy-tabix.
LOOP AT lt_items FROM lv_tabix ASSIGNING FIELD-SYMBOL(<lfs_item>).
IF <lfs_header>-kno <> <lfs_item>-kno.
EXIT.
ENDIF.
"Your logic should be here..
ENDLOOP.
ENDLOOP.
两个 table 均按键 KNO
排序 LOOP AT lt_header INTO lwa_header.
LOOP AT lt_items INTO lwa_item
WHERE key = lwa_header-KNO
“……….
ENDLOOP.
ENDLOOP.
如果 table 中的条目数很大,这将需要更多时间来执行。我应该如何修改代码以提高性能?
您应该能够通过使用辅助 table 键改进第二个 table 中的查找。声明table变量时需要声明这样的key:
DATA lt_items TYPE TABLE OF whatever WITH NON-UNIQUE SORTED KEY k1 COMPONENTS key.
然后您可以使用此键来加速 table 从 O(n)
或 O(log n)
时间复杂度的查找:
LOOP AT lt_header INTO lwa_header.
LOOP AT lt_items INTO lwa_item
USING KEY k1
WHERE key = lwa_header-KNO.
"...
ENDLOOP.
ENDLOOP.
当你能保证lt_items-key中的值总是唯一的(没有两行“key”的值相同),那么你甚至可以使用散列键,这样会更快(常数时间):
DATA lt_items TYPE TABLE OF whatever WITH UNIQUE HASHED KEY k1 COMPONENTS key.
LOOP AT lt_header INTO lwa_header.
READ TABLE lt_items INTO lwa_item
WITH TABLE KEY k1
COMPONENTS key = lwa_header-KNO.
IF sy-subrc = 0.
"...
ENDIF.
ENDLOOP.
您可以使用平行光标。这是提高嵌套循环性能的好方法。有关详细信息,请查看此 link.
字段符号也更利于性能。
DATA lv_tabix TYPE sy-tabix.
SORT: lt_header BY kno,
lt_items BY kno.
LOOP AT lt_header ASSIGNING FIELD-SYMBOL(<lfs_header>).
READ TABLE lt_items TRANSPORTING NO FIELDS
WITH KEY kno = <lfs_header>-kno
BINARY SEARCH.
lv_tabix = sy-tabix.
LOOP AT lt_items FROM lv_tabix ASSIGNING FIELD-SYMBOL(<lfs_item>).
IF <lfs_header>-kno <> <lfs_item>-kno.
EXIT.
ENDIF.
"Your logic should be here..
ENDLOOP.
ENDLOOP.