Sei sulla pagina 1di 6

SDN Contribution

Using Sort & Index While Processing Internal Tables


Applies to:
SAP R/3 ABAP Version 4.7

Summary
In situations where we process large entries in internal table based on a WHERE condition we can use sorted internal table with index access to improve the overall performance. This article gives you a comparative study of 3 internal tables accessed differently to process multiple records and shows how the data entries can be processed faster using SORTED TABLE with INDEX SEARCH. Author(s): SHARATH KUMAR RADHAKRISHNA Company: CARITOR India Private Ltd. Created on: 9 March 2006

Author Bio
Sharath is a Senior Software engineer working for Caritor, Bangalore. His area of expertise is SAP-ABAP with 3 domestic implementation experience.

2006 SAP AG

Table of Contents
ILLUSTRATION ............................................................................................................................... 2 Detailed Info ................................................................................................................................. 2 Source Code............................................................................................................................. 3 Results and Analysis ................................................................................................................ 5 Disclaimer and Liability Notice......................................................................................................... 6

ILLUSTRATION
Often we encounter a situation in our ABAP program where we need to process the records of an internal table which meets certain criteria. So how do we interpret this in ABAP, simply using LOOP AT <ITAB> WHERE <SOME CONDITION>. Main idea here is to show you three different ways of achieving the same result and also a compare study of time it takes in these 3 different techniques.

3 Techniques are Normal loop with where on a STANDARD internal table. Loop with where on a sorted table, table is sorted according to the WHERE condition used. Using READ with WHERE CONDITION at first and then use READ with INDEX for subsequent Records.

Detailed Info Let us start with selecting records from MARC table into an internal table. Now I will loop at those records which have plant equal to the plant entered in selection screen. So logic for these 3 techniques would be

Logic1: Loop at the internal table using LOOP statement and use Where condition to match the plant values. Here SAP is going to do linear search on all the records of internal table which will match the records. Therefore inherently this is going to take a lot of time.

Sample Code:
LOOP AT T_MARC INTO WA_MARC WHERE WERKS EQ P_WERKS. ADD 1 TO COUNT. ENDLOOP.

Logic 2: Here we use a sorted internal table with sort key equal to the key used in the where condition of the loop, in this case it will be WERKS. So we use LOOP on Sorted internal table with WHERE condition 2006 SAP AG 2

using WERKS. Since the table is sorted by WERKS, access is faster resulting in faster processing of records. Declaration of Table:
DATA : T_MARC1 TYPE SORTED TABLE OF TP_MARC WITH NON-UNIQUE KEY WERKS MATNR.

Sample Code:
LOOP AT T_MARC1 INTO WA_MARC WHERE WERKS EQ P_WERKS. ADD 1 TO COUNT. ENDLOOP.

Logic 3: This logic is the emphasis of this article. Sample Code:


READ TABLE T_MARC1 INTO WA_MARC WITH KEY WERKS = P_WERKS. IF SY-SUBRC EQ 0. L_TABIX = SY-TABIX + 1. ADD 1 TO COUNT. LOOP AT T_MARC1 INTO WA_MARC FROM L_TABIX. IF WA_MARC-WERKS NE P_WERKS. EXIT. ENDIF. ADD 1 TO COUNT. ENDLOOP. ENDIF.

If you look at the above code, first we read the record which appears first in the list of records, hence the index of that entry is stored in a local variable, further records can be read using index from loop which is very fast in access.

Source Code Execute the source code to find out the time taken for each loop technique. Below is the complete source code:
*&---------------------------------------------------------------------* *& Report ZLOOPCOMPARE * *& * *&---------------------------------------------------------------------* *& * *& * *&---------------------------------------------------------------------*

2006 SAP AG

REPORT

ZLOOPCOMPARE.

TYPES : BEGIN OF tp_marc, werks TYPE marc-werks, matnr TYPE marc-matnr, END OF tp_marc. DATA DATA WITH DATA DATA : t_marc TYPE STANDARD TABLE OF tp_marc. : t_marc1 TYPE SORTED TABLE OF tp_marc NON-UNIQUE KEY werks matnr. : wa_marc TYPE tp_marc. : p1 TYPE i, p2 TYPE i. DATA : l_tabix TYPE sy-tabix. DATA : count TYPE i. PARAMETERS : p_werks TYPE marc-werks OBLIGATORY. SELECT werks matnr FROM marc INTO TABLE t_marc1. t_marc[] = t_marc1[]. *------------------------------------------------------------*Case1 - standard table with where condition. GET RUN TIME FIELD p1. LOOP AT t_marc INTO wa_marc WHERE werks EQ p_werks. ADD 1 TO count. ENDLOOP. GET RUN p2 = p2 WRITE : WRITE : ULINE. TIME FIELD p2. - p1. / p2. / count.

*------------------------------------------------------------*Case2- sorted table sorted as per the where condition. CLEAR count. GET RUN TIME FIELD p1. LOOP AT t_marc1 INTO wa_marc WHERE werks EQ p_werks. ADD 1 TO count. ENDLOOP. GET RUN TIME FIELD p2. p2 = p2 - p1. WRITE : / p2. WRITE : / count. ULINE. *------------------------------------------------------------*Case3- using index to get faster access. CLEAR count. GET RUN TIME FIELD p1.

2006 SAP AG

READ TABLE t_marc1 INTO wa_marc WITH KEY werks = p_werks. IF sy-subrc EQ 0. l_tabix = sy-tabix + 1. ADD 1 TO count. LOOP AT t_marc1 INTO wa_marc FROM l_tabix. IF wa_marc-werks NE p_werks. EXIT. ENDIF. ADD 1 TO count. ENDLOOP. ENDIF. GET RUN p2 = p2 WRITE : WRITE : ULINE. TIME FIELD p2. - p1. / p2. / count.

Results and Analysis Fallowing are the statistics for the above 3 parts.

1. Records: 4006. Time taken by Loop at standard internal table in Micro seconds. 38,405 36,505 37,745 39,850 37,770 Time taken by Loop at SORTED internal table in micro seconds 4,438 4,432 4,476 4,524 4,625 Time taken by Read with Loop at INDEX technique on a SORTED table in micro seconds 3,952 3,926 3,597 3,908 3,821

Iteration Number

1 2 3 4 5

2006 SAP AG

2. Records: 55184. Time taken by Loop at standard internal table in Micro seconds. 245,820 278,824 271,210 274,324 276,581 Time taken by Loop at SORTED internal table in micro seconds 64,227 65,120 63,264 64,425 64,581 Time taken by Read with Loop at INDEX technique on a SORTED table in micro seconds 56,928 56,679 55,939 57,596 56,994

Iteration Number

1 2 3 4 5

Conclusion:
So from the statistics we can say that technique number 3 is slightly better than using SORTED internal table for processing large number of records. Since the number of entries are not very high (test environment), the variation is not very much, but in a production environment the performance improvement will be good enough to take this technique into serious consideration.

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

2006 SAP AG

Potrebbero piacerti anche