二维码

[性能] ABAP中DELETE内表的若干方法性能分析

Twilight发表于 2016-11-15 15:56mas涓细流 最后回复于 2017-03-10 16:07 [复制链接] 4297 2

1、测试案例
  1. DATA: lv_flag TYPE flag,
  2.       lv_sta_time TYPE timestampl,
  3.       lv_end_time TYPE timestampl,
  4.       lv_diff_w   TYPE p DECIMALS 5,
  5.       lv_diff_f   LIKE lv_diff_w,
  6.       lv_save     LIKE lv_diff_w.

  7. TYPES:
  8.   BEGIN OF ty_header,
  9.     vbeln   TYPE char10,
  10.     field1  TYPE char50,
  11.   END   OF ty_header.
  12. TYPES: tt_header TYPE STANDARD TABLE OF ty_header.

  13. TYPES:
  14.   BEGIN OF ty_item,
  15.     vbeln   TYPE char10,
  16.     posnr   TYPE char6,
  17.     field2  TYPE char50,
  18.   END   OF ty_item.
  19. TYPES: tt_item TYPE STANDARD TABLE OF ty_item.
  20. TYPES: tt_item2 TYPE SORTED TABLE OF ty_item WITH UNIQUE KEY vbeln posnr.
  21. TYPES: tt_item3 TYPE SORTED TABLE OF ty_item
  22.                      WITH UNIQUE KEY vbeln posnr
  23.                      WITH NON-UNIQUE SORTED KEY key2nd COMPONENTS field2.

  24. DATA: t_header  TYPE tt_header,
  25.       t_item    TYPE tt_item,
  26.       t_item2   TYPE tt_item2,
  27.       t_item3   TYPE tt_item3.

  28. PARAMETERS:  p_num TYPE i DEFAULT 1000.

  29. START-OF-SELECTION.
  30.   DATA: ls_header LIKE LINE OF t_header,
  31.         ls_item   LIKE LINE OF t_item.

  32.   DO p_num TIMES.
  33.     ls_header-vbeln = sy-index.
  34.     ls_header-field1 = sy-abcde.
  35.     APPEND ls_header TO t_header.
  36.     DO 10 TIMES.
  37.       ls_item-vbeln = ls_header-vbeln.
  38.       ls_item-posnr = sy-index.
  39.       APPEND ls_item TO t_item.
  40.     ENDDO.

  41.   ENDDO.

  42.   t_item2[] = t_item[].
  43.   t_item3[] = t_item[].

  44. *-->DELETE WHERE
  45.   DATA: lv_mod_3 TYPE i.
  46.   GET TIME STAMP FIELD lv_sta_time.
  47.   SORT t_item BY vbeln.
  48.   LOOP AT t_header INTO ls_header.
  49.     lv_mod_3 = sy-tabix MOD 3.
  50.     IF lv_mod_3 IS INITIAL.
  51.       DELETE t_item WHERE vbeln = ls_header-vbeln.
  52.     ENDIF.
  53.   ENDLOOP.
  54.   GET TIME STAMP FIELD lv_end_time.
  55.   lv_diff_w = lv_end_time - lv_sta_time.
  56.   WRITE: /(30) 'DELETE WHERE', lv_diff_w.

  57. *-->DELETE Parallel
  58.   CLEAR:lv_mod_3.
  59.   GET TIME STAMP FIELD lv_sta_time.
  60.   SORT t_item BY vbeln.
  61.   LOOP AT t_header INTO ls_header.
  62.     lv_mod_3 = sy-tabix MOD 3.
  63.     IF lv_mod_3 IS INITIAL.

  64.       READ TABLE t_item TRANSPORTING NO FIELDS WITH KEY vbeln = ls_header-vbeln BINARY SEARCH.
  65.       IF sy-subrc EQ 0.
  66.         LOOP AT t_item INTO ls_item FROM sy-tabix.
  67.           IF ls_item-vbeln NE ls_header-vbeln.
  68.             EXIT.
  69.           ENDIF.
  70.           DELETE t_item INDEX sy-tabix.
  71.         ENDLOOP.
  72.       ENDIF.

  73.     ENDIF.
  74.   ENDLOOP.
  75.   GET TIME STAMP FIELD lv_end_time.
  76.   lv_diff_w = lv_end_time - lv_sta_time.
  77.   WRITE: /(30) 'DELETE Parallel', lv_diff_w.

  78. *-->DELETE UNIQUE SORTED KEY
  79.   CLEAR:lv_mod_3.
  80.   GET TIME STAMP FIELD lv_sta_time.
  81.   LOOP AT t_header INTO ls_header.
  82.     lv_mod_3 = sy-tabix MOD 3.
  83.     IF lv_mod_3 IS INITIAL.
  84.       DELETE t_item2 WHERE vbeln = ls_header-vbeln.
  85.     ENDIF.
  86.   ENDLOOP.
  87.   GET TIME STAMP FIELD lv_end_time.
  88.   lv_diff_w = lv_end_time - lv_sta_time.
  89.   WRITE: /(30) 'DELETE UNIQUE SORTED KEY', lv_diff_w.

  90. *-->DELETE NON-UNIQUE SORTED KEY
  91.   CLEAR:lv_mod_3.
  92.   GET TIME STAMP FIELD lv_sta_time.
  93.   LOOP AT t_header INTO ls_header.
  94.     lv_mod_3 = sy-tabix MOD 3.
  95.     IF lv_mod_3 IS INITIAL.

  96.       DELETE t_item3 USING KEY key2nd WHERE field2 = ls_header-vbeln.

  97.     ENDIF.
  98.   ENDLOOP.
  99.   GET TIME STAMP FIELD lv_end_time.
  100.   lv_diff_w = lv_end_time - lv_sta_time.
  101.   WRITE: /(30) 'DELETE NON-UNIQUE SORTED KEY', lv_diff_w.
复制代码

2、测试结果:
it_delete.jpg
还是第二中效率最高,第一种传统的写法,懒人多采用第一种 ,如果内表数据太多,直接delete效率会差很多
回复

使用道具 举报

zzxbat007
感谢分享
SAP Business One 凭借单一系统 优化你的整个业务  www.iwilley.com
回复

使用道具 举报

mas涓细流
谢谢分享,学习中
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表