二维码

[性能] STANDARD, SORTED和HASHED Table在READ数据时性能比较分析

Twilight发表于 2016-10-25 16:39Twilight 最后回复于 2016-10-25 16:39 [复制链接] 3194 0

1、程序代码:
  1. TYPES:
  2.   BEGIN OF ty_vbpa,
  3.     vbeln TYPE vbpa-vbeln,
  4.     posnr TYPE vbpa-posnr,
  5.     parvw TYPE vbpa-parvw,
  6.     kunnr TYPE vbpa-kunnr,
  7.   END  OF ty_vbpa.

  8. DATA: t_std    TYPE STANDARD TABLE OF ty_vbpa.
  9. DATA: t_sorted TYPE SORTED TABLE OF ty_vbpa WITH UNIQUE KEY vbeln.
  10. DATA: t_hashed TYPE HASHED TABLE OF ty_vbpa WITH UNIQUE KEY vbeln.

  11. TYPES:
  12.   BEGIN OF ty_vbak,
  13.     vbeln TYPE vbak-vbeln,
  14.     found TYPE flag,
  15.   END   OF ty_vbak.
  16. DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
  17. DATA: lt_vbak TYPE STANDARD TABLE OF ty_vbak.
  18. FIELD-SYMBOLS: <lfs_vbak> LIKE LINE OF t_vbak.

  19. DATA: lv_flag TYPE flag,
  20.       lv_sta_time TYPE timestampl,
  21.       lv_end_time TYPE timestampl,
  22.       lv_diff   TYPE p DECIMALS 5.

  23. DATA: lv_num_main TYPE i,
  24.       lv_num_sub TYPE i.

  25. START-OF-SELECTION.
  26.   lv_num_main = 50000.            " Change for different number of records
  27.   lv_num_sub = lv_num_main / 2.
  28. *
  29.   SELECT vbeln
  30.     FROM vbak
  31.     INTO TABLE t_vbak
  32.     UP TO lv_num_main ROWS.
  33. *
  34.   SELECT vbeln posnr parvw kunnr
  35.     INTO TABLE t_std
  36.     FROM vbpa
  37.     UP TO lv_num_sub ROWS
  38.     FOR ALL ENTRIES IN t_vbak
  39.     WHERE vbeln = t_vbak-vbeln
  40.     AND   parvw = 'AG'.

  41. * Copying into the Sorted and Hashed table
  42.   t_sorted = t_std.
  43.   t_hashed = t_std.
  44. *----
  45. * READ on Standard Table
  46.   GET TIME STAMP FIELD lv_sta_time.
  47.   LOOP AT t_vbak ASSIGNING <lfs_vbak>.
  48.     READ TABLE t_std TRANSPORTING NO FIELDS
  49.       WITH KEY vbeln = <lfs_vbak>-vbeln.
  50.     IF sy-subrc EQ 0.
  51.       "..
  52.     ENDIF.
  53.   ENDLOOP.
  54.   GET TIME STAMP FIELD lv_end_time.
  55.   lv_diff  = lv_end_time - lv_sta_time.
  56.   WRITE: /(30) 'Standard Table', lv_diff.
  57. *
  58. *----
  59. * READ on Standard table with Binary Search
  60.   GET TIME STAMP FIELD lv_sta_time.
  61.   SORT t_std BY vbeln.
  62.   LOOP AT t_vbak ASSIGNING <lfs_vbak>.
  63.     READ TABLE t_std TRANSPORTING NO FIELDS
  64.       WITH KEY vbeln = <lfs_vbak>-vbeln
  65.       BINARY SEARCH.
  66.     IF sy-subrc EQ 0.
  67.       "..
  68.     ENDIF.
  69.   ENDLOOP.
  70.   GET TIME STAMP FIELD lv_end_time.
  71.   lv_diff  = lv_end_time - lv_sta_time.
  72.   WRITE: /(30) 'Standard Table - Binary', lv_diff.
  73. *
  74. *----
  75. * READ on Sorted Table
  76.   GET TIME STAMP FIELD lv_sta_time.
  77.   LOOP AT t_vbak ASSIGNING <lfs_vbak>.
  78.     READ TABLE t_sorted TRANSPORTING NO FIELDS
  79.       WITH KEY vbeln = <lfs_vbak>-vbeln.
  80.     IF sy-subrc EQ 0.
  81.       "..
  82.     ENDIF.
  83.   ENDLOOP.
  84.   GET TIME STAMP FIELD lv_end_time.
  85.   lv_diff  = lv_end_time - lv_sta_time.
  86.   WRITE: /(30) 'Sorted Table', lv_diff.
  87. *
  88. *----
  89. * READ on HASHED table
  90.   GET TIME STAMP FIELD lv_sta_time.
  91.   LOOP AT t_vbak ASSIGNING <lfs_vbak>.
  92.     READ TABLE t_hashed TRANSPORTING NO FIELDS
  93.       WITH TABLE KEY vbeln = <lfs_vbak>-vbeln.
  94.     IF sy-subrc EQ 0.
  95.       "..
  96.     ENDIF.
  97.   ENDLOOP.
  98.   GET TIME STAMP FIELD lv_end_time.
  99.   lv_diff  = lv_end_time - lv_sta_time.
  100.   WRITE: /(30) 'Hashed Table', lv_diff.
复制代码

2、测试结果:
Read Table.jpg
排除STANDARD TABLE读数前的排序,STANDARD TABLE使用read Binary和SORTED TABLE读数时几乎效差不多,但是HASHED Table性能几乎是STANDARD TABLE的两倍,所以内表数据较大时,建议选择Hashed Table
回复

使用道具 举报

快速回帖

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

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