Improving_9182006114851PM_v1.doc
Improvin Impro vin g performance perfor mance of FO FOR R ALL ENTR ENTRIE IES S query
Appl Ap pl ies to : ABAP
Summary This This art article icle exp explain lains s how how to improv prove e the perfo erforrmance ance of “FOR ALL ENTRIES” query ery
Au th or (s): As hi m Ch ow dh ur y Company: Tata Consultancy Consultancy Services Services L td. Created on: 12 September 2006
Auth Au thor or Bi Bio o Ashim Chowdhury Chowdhury is an SAP Technical consultant consultant with with Tata Consult C onsultancy, ancy, 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 Select…for 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 sortin g 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 ru n
Records in MARA
1
124
2 3
Records in MAKT
Time required ( mili-seconds) Af ter so rt and del ete adjacent duplicates
Before sort
After Sort
124
1131
942
19
124
124
1550
1126
14
124
124
1427
929
12
1369
999
15
Average Time
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 Liabili ty 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. Y ou agree that you will not hold, or seek to hold, S AP 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