Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
bruno_esperanca
Contributor
4,065

Yes, it's possible.


Dear community,

Performance vs dynamic. I have always been under the impression that if we create a dynamic table we will not be able to access it in a fast way. Or in other words, I didn't think it was possible to create a sorted or hashed dynamic table.

But now I know this is not true, and I want to share it with you.

 

The test case


I have created a small program to create a dynamic table with the key fields from SBOOK and a few fields more. Select all data from SBOOK into the dynamic table with the sorted key, and also another dynamic table without key fields (standard table). Analyzed the access time with ST12.

And here is the result, accessing one of them is 100 times faster than the other. I'll let you guess which one.



 

The code


Here's the code so you can execute the example yourself, and also check how to create the dynamic table with a sorted or hashed key.

Enjoy.
*&---------------------------------------------------------------------*
*& Report YTMP_DYN_TAB_W_KEYS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ytmp_dyn_tab_w_keys.


START-OF-SELECTION.

DATA table_w_keys_handle TYPE REF TO cl_abap_tabledescr.
DATA keyless_table_handle TYPE REF TO cl_abap_tabledescr.
DATA keys TYPE abap_table_keydescr_tab.
DATA struct_desc TYPE REF TO cl_abap_structdescr.
DATA table_w_keys TYPE REF TO data.
DATA keyless_table TYPE REF TO data.

PERFORM get_struct
CHANGING
struct_desc.

PERFORM get_keys
CHANGING
keys.

cl_abap_tabledescr=>create_with_keys(
EXPORTING
p_line_type = struct_desc " Type Description Object of Row Type of Table
p_keys = keys " Table Keys
RECEIVING
p_result = table_w_keys_handle " Type Description Object of Table
).
CREATE DATA table_w_keys TYPE HANDLE table_w_keys_handle.

cl_abap_tabledescr=>create(
EXPORTING
p_line_type = struct_desc " Line Type
RECEIVING
p_result = keyless_table_handle " Result Type
).
CREATE DATA keyless_table TYPE HANDLE keyless_table_handle.

PERFORM select_data
CHANGING table_w_keys.

PERFORM select_data
CHANGING keyless_table.

PERFORM read_with_key
USING table_w_keys.

PERFORM read_wo_key
USING keyless_table.

WRITE 'Done'.

*&---------------------------------------------------------------------*
*& Form GET_STRUCT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_STRUCT_DESC text
*----------------------------------------------------------------------*
FORM get_struct
CHANGING
ch_struct_desc TYPE REF TO cl_abap_structdescr.

DATA fields TYPE abap_component_tab.
DATA field LIKE LINE OF fields.

field-name = 'CARRID'.
field-type ?= cl_abap_datadescr=>describe_by_name( p_name = 'S_CARR_ID' ).
APPEND field TO fields.

field-name = 'CONNID'.
field-type ?= cl_abap_datadescr=>describe_by_name( p_name = 'S_CONN_ID' ).
APPEND field TO fields.

field-name = 'FLDATE'.
field-type ?= cl_abap_datadescr=>describe_by_name( p_name = 'S_DATE' ).
APPEND field TO fields.

field-name = 'BOOKID'.
field-type ?= cl_abap_datadescr=>describe_by_name( p_name = 'S_BOOK_ID' ).
APPEND field TO fields.

field-name = 'SMOKER'.
field-type ?= cl_abap_datadescr=>describe_by_name( p_name = 'S_SMOKER' ).
APPEND field TO fields.

field-name = 'CLASS'.
field-type ?= cl_abap_datadescr=>describe_by_name( p_name = 'S_CLASS' ).
APPEND field TO fields.

ch_struct_desc ?= cl_abap_structdescr=>create(
p_components = fields
).

ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_KEYS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_KEYS text
*----------------------------------------------------------------------*
FORM get_keys
CHANGING
ch_keys TYPE abap_table_keydescr_tab.

DATA key LIKE LINE OF ch_keys.
DATA component LIKE LINE OF key-components.

component-name = 'CARRID'.
APPEND component TO key-components.

component-name = 'CONNID'.
APPEND component TO key-components.

component-name = 'FLDATE'.
APPEND component TO key-components.

component-name = 'BOOKID'.
APPEND component TO key-components.

key-name = 'MY_KEY'.
key-is_primary = abap_true.
key-is_unique = abap_true.
key-access_kind = cl_abap_tabledescr=>tablekind_sorted.
key-key_kind = cl_abap_tabledescr=>keydefkind_user.

APPEND key TO ch_keys.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form SELECT_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_TABLE_W_KEYS text
*----------------------------------------------------------------------*
FORM select_data
CHANGING
ch_table_w_keys TYPE REF TO data.

FIELD-SYMBOLS <any_table> TYPE ANY TABLE.

ASSIGN ch_table_w_keys->* TO <any_table>.

SELECT * FROM sbook
INTO CORRESPONDING FIELDS OF TABLE <any_table>.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form READ_WITH_KEY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_TABLE_W_KEYS text
*----------------------------------------------------------------------*
FORM read_with_key
USING
im_table_w_keys TYPE REF TO data.

FIELD-SYMBOLS <any_table> TYPE ANY TABLE.

ASSIGN im_table_w_keys->* TO <any_table>.

READ TABLE <any_table> TRANSPORTING NO FIELDS
WITH TABLE KEY ('CARRID') = 'SQ'
('CONNID') = '0002'
('FLDATE') = '20170915'
('BOOKID') = '00000002'.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form READ_WO_KEY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_KEYLESS_TABLE text
*----------------------------------------------------------------------*
FORM read_wo_key
USING
im_keyless_table TYPE REF TO data.

FIELD-SYMBOLS <any_table> TYPE ANY TABLE.

ASSIGN im_keyless_table->* TO <any_table>.

READ TABLE <any_table> TRANSPORTING NO FIELDS
WITH KEY ('CARRID') = 'SQ'
('CONNID') = '0002'
('FLDATE') = '20170915'
('BOOKID') = '00000002'.

ENDFORM.
11 Comments
Labels in this area