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


Submitted By: Shilpa Gunjan (L&T Infotech)

Here's an example on sorted internal table using INDEX access. This example will help you to understand how to improve performance using Sorted Internal Tables.



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 NON-UNIQUE KEY 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.

READ TABLE it_marc INTO wa_marc WITH KEY werks = '1501'.
IF sy-subrc EQ 0.
v_tabix = sy-tabix + 1.
ADD 1 TO v_counter.

LOOP AT it_marc INTO wa_marc from v_tabix.

IF wa_marc-werks NE '1501'.
EXIT.
ENDIF.
ADD 1 TO v_counter.

ENDLOOP.

ENDIF.

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): 1,419



ANALYSIS:

Here we have two programs. In the first program we used KEY access where the total time (minimum total time – CASE IV) taken to process the data was: 1607ms

In the second program INDEX was used and the total time taken to process the data was even small: 1419ms.

Hence Sorted Internal table is efficient where only a part of the table key is used and when the data are processed using INDEX instead of key.


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.