You are on page 1of 6

Improving_9182006114851PM_v1.

doc

Improving performance of FOR ALL ENTRIES query

Applies to:
ABAP

Summary
This article explains how to improve the performance of FOR ALL ENTRIES query

Author(s): Ashim Chowdhury Company: Tata Consultancy Services Ltd. Created on: 12 September 2006

Author Bio
Ashim Chowdhury is an SAP Technical consultant with Tata Consultancy, India. He works in the SAP technical area (ABAP, Net weaver, XI).

SAP DEVELOPER NETWORK | sdn.sap.com 2006 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

Improving performance of FOR ALL ENTRIES query

Table of Contents
Applies to: ........................................................................................................................................ 1 Summary.......................................................................................................................................... 1 Author Bio ........................................................................................................................................ 1 Improving performance of FOR ALL ENTRIES query..................................................................... 3 Example code and performance data comparison: ..................................................................... 3 Comparison result: ....................................................................................................................... 4 Conclusion: ...................................................................................................................................... 5 Disclaimer and Liability Notice......................................................................................................... 6

SAP DEVELOPER NETWORK | sdn.sap.com 2006 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com 2

Improving performance of FOR ALL ENTRIES query

Improving performance of FOR ALL ENTRIES query


Selectfor all entries is a widely used open sql statement in ABAP. This article explains how we can improve the performance of those queries. In select.for all entries query we have two internal table, the driver internal table and the target internal table. For all entries in the driver table we select data from some database table and store in the target internal table. The open sql processor parse the query and create multiple select statement depending on the size of driver table. There are 2 ways to improve the performance of such a query: 1. By sorting the comparison filed of the driver table before the query - When sorted values are used the query performs better. 2. Using a unique driver table Some time the driver table has duplicate values for the comparison field. Using a unique and sorted driver table it is possible to improve the performance of the query. The reason is eliminating duplicate entries from the driver table will enable the OPEN SQL to have fewer queries for getting the same data, hence there is performance boost. This effect is very much prominent when we try to access a master table using a transaction table as the driver (where chances of duplicate entries are high).

Example code and performance data comparison: For the purpose of comparison of the query performance a dummy program has been used. I have used two tables, MARA and MAKT. First try to get the Materials from table MARA and then for all entries in MARA, try to get the data from MAKT. To make the performance difference more prominent the records in the driver table has been duplicated. Here is the source code:
DATA: it_mara TYPE wa_mara TYPE it_makt TYPE wa_makt TYPE it_temp_mara wa_temp_mara STANDARD TABLE OF mara, mara, STANDARD TABLE OF makt, makt, TYPE STANDARD TABLE OF mara, TYPE mara.

* Get all the records from MARA SELECT * FROM mara INTO TABLE it_temp_mara. IF sy-subrc = 0. IF it_temp_mara[] IS NOT INITIAL. * Duplicate the driver table with the data do 100 times. append lines of it_Temp_mara to it_mara. enddo. IF it_mara[] IS NOT INITIAL. * Select MAKT perform select_makt. sort it_mara by matnr. perform select_makt.

SAP DEVELOPER NETWORK | sdn.sap.com 2006 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com 3

Improving performance of FOR ALL ENTRIES query

After deleting duoplicateentries DELETE ADJACENT DUPLICATES FROM it_mara COMPARING matnr. perform select_makt.

ENDIF. ENDIF. ENDIF. *&---------------------------------------------------------------------* *& Form select_makt *&---------------------------------------------------------------------* * Select data friom MAKT *----------------------------------------------------------------------* form select_makt . DATA: t1 TYPE i, t2 TYPE i, tmin TYPE i. refresh it_makt[]. GET RUN TIME FIELD t1. SELECT * FROM makt INTO TABLE it_makt FOR ALL ENTRIES IN it_mara WHERE matnr = it_mara-matnr. GET RUN TIME FIELD t2. tmin = t2 - t1. tmin = tmin . WRITE:/ ' Time(ms) = ', tmin. Endform.

Comparison result: This program has been run in a R3 system (ABAP release 6,20) having 124 materials. For better demonstration of the effect, the records in it_mara has been duplicated by 100 times.

SAP DEVELOPER NETWORK | sdn.sap.com 2006 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com 4

Improving performance of FOR ALL ENTRIES query

No of run 1 2 3

Records in MARA

Records in MAKT Before sort

Time required ( mili-seconds) After Sort 942 1126 929 999 After sort and delete adjacent duplicates 19 14 12 15

124 124 124

124 124 124 Average Time

1131 1550 1427 1369

From the result it is quite evident that using a sorted and unique driver table will improve the performance of a select for all entries query (especially if there are duplicate records in the driver table).

Conclusion:
Using a sorted and unique driver table boost the performance of FOR ALL ENTRIES query, especially in cases where driver table has many duplicate rows.

SAP DEVELOPER NETWORK | sdn.sap.com 2006 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com 5

Improving performance of FOR ALL ENTRIES query

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.

SAP DEVELOPER NETWORK | sdn.sap.com 2006 SAP AG

BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com 6

You might also like