SAP ABAP - Sorted Internal Table With Key Access (With Example)


Submitted By: Shilpa Gunjan (L&T Infotech)


Here's an example on sorted internal table to verify it's performance.

REPORT z_internal_table_test.

*Sorted Internal table (Performance)

TYPES: BEGIN OF x_marc,
matnr LIKE marc-matnr,
werks LIKE marc-werks,
pstat LIKE marc-pstat,
flag TYPE c,
END OF x_marc.

*Internal Table Declaration
DATA: it_marc TYPE SORTED TABLE OF x_marc WITH UNIQUE KEY matnr werks.

* Table Workarea Declaration
DATA: wa_marc TYPE x_marc.

* Variable Declaration
DATA: v_counter TYPE i,
v_runtime1 TYPE i,
v_runtime2 TYPE i,
v_tabix LIKE sy-tabix.


SELECT matnr       " Material Number
werks                     " Plant
pstat                       " Maintenance status
FROM marc
INTO TABLE it_marc.


GET RUN TIME FIELD v_runtime1.
LOOP AT it_marc INTO wa_marc WHERE werks EQ '1501'.
ADD 1 TO v_counter.
ENDLOOP.

GET RUN TIME FIELD v_runtime2.

* Calculate Runtime
v_runtime2 = v_runtime2 - v_runtime1.

WRITE: 'Total No. Of Records Processed Is:'.
WRITE: v_counter.
WRITE:/ 'Total Time Taken To Process Data Is (In ms):'.
WRITE: v_runtime2.



OUTPUT:

Total No. Of Records Processed Is: 8,882
Total Time Taken To Process Data Is (In ms): 4,498


CASE II:

If the internal table definition is changed to:

DATA: it_marc TYPE SORTED TABLE OF x_marc WITH NON-UNIQUE KEY matnr werks.

OUTPUT:

Total No. Of Records Processed Is: 8,882
Total Time Taken To Process Data Is (In ms): 4,294


CASE III:

If the internal table definition is changed to:

DATA: it_marc TYPE SORTED TABLE OF x_marc WITH NON-UNIQUE KEY matnr.

OUTPUT:

Total No. Of Records Processed Is: 8,882
Total Time Taken To Process Data Is (In ms): 3,192


CASE IV:

If the internal table definition is changed to:

DATA: it_marc TYPE SORTED TABLE OF x_marc WITH NON-UNIQUE KEY werks.

OUTPUT:

Total No. Of Records Processed Is: 8,882
Total Time Taken To Process Data Is (In ms): 1,607




Looking at all the cases, we can now see that CASE IV is the most effective way of using sorted table for better efficiency. Here the key partly contains the table key and hence the access is efficient.


RELATED POSTS:

- INTERNAL TABLES - Introduction, Advantages & Types Of Internal Tables.

- STANDARD INTERNAL TABLE (Index Table) - Introduction With Sample Programs.

- SORTED INTERNAL TABLE (Index Table) - Introduction, Advantages & Performance.

- SORTED INTERNAL TABLE With Key Access (Performance) - Sample Program.

- SORTED INTERNAL TABLE With INDEX Access (Performance) - Sample Program.

- HASHED INTERNAL TABLE (Non-Index Table) - Introduction, Advantages & Performance.

- HASHED INTERNAL TABLE - Partial Key Access (Performance) - Sample Program.

- HASHED INTERNAL TABLE - Table's Key Access (Performance) - Sample Program.


Your suggestions and comments are welcome in this section.

Please mail all your contributions to administrator@abapmadeeasy.com We request you to mention your Name, Designation, Experience & Organization you are working for. Your posts will be verified and posted in this site with your name.