0% found this document useful (0 votes)
72 views23 pages

ABAP Perofrmance Tuning

The document describes methodologies to improve performance in SAP ABAP coding. It discusses optimizing selection screen validations, select statements, and WHERE clauses. Key recommendations include selecting only required fields, using indexes, and specifying conditions in the same order as indexes.

Uploaded by

Sagar Ranasingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views23 pages

ABAP Perofrmance Tuning

The document describes methodologies to improve performance in SAP ABAP coding. It discusses optimizing selection screen validations, select statements, and WHERE clauses. Key recommendations include selecting only required fields, using indexes, and specifying conditions in the same order as indexes.

Uploaded by

Sagar Ranasingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

1

Declaration:
I here by declare that this document is based on my personal experiences. To
the best of my knowledge, this document does not contain any material that infringes the
copyrights of any other individual or organization including the customers of Infosys.
(Vemuru Vinod umar!
Target readers: "ll "#"$ers.
Introduction:
%"$ &'( is the most successful product for enterprise resource planning ()&$!. It is
being used by many companies and had many modules to cover end to end business of
most of the companies including human resource management (*&!. %"$ &'( is based on a
relational database management system (&+#,%! which is used to store all &'( related and
application data.
).g. -ompany.s sales and marketing information, *istorical data etc
Very good performance is crucial for the users of %"$ &'( system and is expected
from every individual. It is a general re/uirement to process various transactions
concurrently for thousands of users by %"$ &'( and the underlying database system. In
addition, the size of the database system will tend to grow day by day in tune with business
growth of the company. "s a result the performance of the system will tend to go down,
which is not expected from customer point of view. 0nfortunately, tuning the performance of
an %"$ &'( system is very difficult because both the &'( application system and the
underlying database system must be tuned which is not feasible in reality. 1urthermore, the
performance of an &'( system can be impacted because parts of &'( system were designed
and implemented in the early eighties at a time when commercial database systems were
immature. 1inally, %"$ &'( is an open system that allows customers and third2party vendors
to integrate specialized modules which are not part of the %tandard &'( system. %uch
modules can severely impact the performance of the whole %"$ system. #y taking care of
proper design of "#"$ applications we can address performance issues at least to some
extent.
This document describes the $erformance improvement methodologies and standards that
can be followed in "#"$ coding to improve the performance of the "#"$ ob3ects.
1. Selection screen validations:
%election screen validation is a common re/uirement in almost all the reports. $oints to be
remembered while doing selection screen validations.
4. 1irst check whether the selection screen field (%elect option'$arameter! has some
value or not before hitting the data base for validating. It is meaning less to do
validation for a blank value.
Performance tuning in ABAP
2
5. It.s a common practice to use %)6)-T %I786) 9 for selection screen validations. It is
needless to select all the table fields when we are interested to validate a single field.
It is advisable to select only the re/uired field instead of %)6)-T %I786) 9.
(. "lways check for the validation against -*)- table. )ven though we check for
validation in some other linked table, internally a check happens against the check
table i.e 6inked table entry will be created with reference to -heck table. "lso there
is no chance of duplicates in check table as the check field(s! will be the primary key.
This improves the performance to a great extent.
)8: %)6)-T2;$TI;7%: so<vkorg 1;& vbak2vkorg.
+"T": wa<vbak T=$) vbak,
w<vkorg T=$) tvko2vkorg.
Dont do:
AT SELECTI! SC"EE! #TP#T.
SELECT SI!$LE % I!T &a'v(a) *"+ v(a) ,-E"E v)org I! so'v)org.
I* !T s./s.(rc IS I!ITIAL.
+ESSA$E e100.
E!DI*.
Do:
AT SELECTI!/SC"EE! ! so'v)org.
C-EC1 !T so'v)org23 IS I!ITIAl.
PE"*"+ validate'v)org.
C-EC1 !T s./s.(rc IS I!ITIAL.
+ESSA$E e100.
*"+ validate'v)org.
DATA: l'v)org T4PE tv)o/v)org.
SELECT SI!$LE v)org I!T l'v)org *"+ tv)o ,-E"E v)org I!
so'v)org.
E!D*"+.
7;T): -heck table for sales org. is TV;. (>e can check this in %)44!
5. Select Statement:
%elect is the basic statement of any &eport. This is one of the most important areas
where we generally get performance issues. >e will look in to this in depth by considering
various factors which cause performance issues in select statements.
5.1SELECT CLA#SE:
It is always advisable to select only the re/uired fields instead of simply using %)6)-T 9.
This will reduce the transportation cost to a great extent. This is because of time involved in
transferring data of each field from data base server to application server. If we specify
%)6)-T 9 then it will transfer all the fields which is not advisable unless it is really re/uired.
1or this we need to specify field list of the re/uired fields in the select clause. "nother
3
important point to remember is the order of the fields in the field list should be same as
they appear in data base table.
If you want to avoid duplicates then simply select all the key fields plus the re/uired
fields. In this case we need not use +)6)T) "+?"-)7T +0$I-"T)%. If the duplication check
is based on fields other than key fields then we have to +)6)T) records explicitly by
comparing those fields.
If you are sure that you get a single record then use addition %)6)-T %I786).
5.5I!T CLA#SE:
"lways the structure specified in the I7T; clause should be same as the field list
specified in %)6)-T clause. If we have more fields in field list than the number of fields in
the structure of into clause or field types mismatching then a runtime error occurs. If the
field list is less than the number of fields in the structure of into clause, then the fields are
placed from left to right. %o the extra right most fields will have their initial values. "gain if
there is any field type mismatch then run time error occurs.
"lways the field types of the structure in the I7T; clause should match with that of the
source fields (+ata base fields! from where we fetch the data. This will avoid run time
conversion costs.
"void using I!T C""SP!DI!$ *IELDS addition. It involves additional cost for
placing the data into corresponding fields of the structure.
0se I!T TABLE i'ita(. >e will get data into internal table in a single operation.
5.6*"+ CLA#SE:
This is the place where we can specify Tables, Views and ?oins and whether to read data
directly from data base or from #uffer.
The %"$ database interface enables storage of database tables in local buffers, i.e. the
buffers reside locally on each application server of the system. #uffering is especially
important in a client'server environment because the access time using the network is much
greater than the access time to a locally buffered table.
%o never use #=$"%%I78 #011)& addition unless and until it is really re/uired to select
data directly from data base.
+on.t use 3oins for more than 5 tables. "s the number of 3oin tables increases, the
efficiency of the 3oin will be reduced. (6ogarithmically! "lso ?;I7 has peculiar property. It
automatically bypasses buffer. This further reduces the performance of the select by
increasing the database load.
4
5.7,-E"E CLA#SE:
This is the most important part of the select where we can concentrate more to improve
the performance. #efore writing conditions in >*)&) clause first thing we need to check is
I7+)@ of the database table. If any secondary I7+)@ already exists and the where clause
conditions match with the I7+)@ then specify the condition in where clause in the same
order as they appear in the I7+)@. If no I7+)@ exists then specify the fields in the same
order as they appear in the data base table.
The order of the fields in the where clause greatly affects the performance. "ccessing
data base by using I7+)@ is very much faster because index has the address of the
matching record. %o the cursor directly goes and fetches those records instead of searching
for the records in the whole data base.
>e encountered this example in one of our reports. >hen using ?;I7%, specify the
where clause conditions in the same order of the T"#6)% in the ?;I7 and it is advisable to
have header table as first ?;I7 table. This again depends on re/uirement. ?ust look at the
example below.
CASE1:
%)6)-T aAvbeln aAfkart aAvbtyp aAvkorg aAzuonr bAposnr
bAmeins bAfklmg bAntgew bAgewei bAmatnr bAvkbur (8erdat
I7T; T"#6) i<vbrp
1&;, vbrk "% a I77)& ?;I7 vbrp "% b
;7 aAvbeln )B bAvbeln
>*)&) aAvbtyp 7) lc<n
"7+ aAvbtyp 7) lc<o
A!D (8erdat I! r'erdat
"7+ bAaland )B c<aland.
CASE5:
%)6)-T aAvbeln aAfkart aAvbtyp aAvkorg a8erdat
aAzuonr bAposnr bAmeins bAfklmg
bAntgew bAgewei bAmatnr bAvkbur
I7T; T"#6) i<vbrp
1&;, vbrk "% a I77)& ?;I7 vbrp "% b
;7 aAvbeln )B bAvbeln
>*)&) aAvbtyp 7) lc<n
"7+ aAvbtyp 7) lc<o
A!D a8erdat I! r'erdat
"7+ bAaland )B c<aland.
*ere we can give the )&+"T condition both from V#& and V#&$. #ut if we see the
order of table in the ?;I7, V#& comes first. "fter changing this single condition we got
surprising performance improvement of (@.
5
%pecify as many key fields as we can in the where clause. This is the point where
performance of select /uery depends.
I read this logic in some forum. I tried implementing this. &esults are impressive for
some of the selects. It reduced the runtime of select /uery slightly.
If we can.t pass all the key fields then instead of not giving any thing in the where
clause we can implement this. +eclare some constants of same type of the key fields which
we can.t pass and I7ITI"6IC) to their I7ITI"6 values. 7ow use these variables in >*)&)
clause by using 8) comparison operator. ?ust look at the following example.
-;7%T"7T%: c<vbeln T=$) vbfa2vbeln V"60) I% I7ITI"6,
c<posnn T=$) vbfa2posnn V"60) I% I7ITI"6,
c<vbtyp<n T=$) vbfa2vbtyp<n V"60) I% I7ITI"6,
CASE1:
%)6)-T vbelv posnv vbeln posnn vbtyp<n rfmng meins
I7T; T"#6) i<vbfa 1&;, vbfa
>*)&) vbelv I7 so<vbeln
"7+ posnv I7 so<posnv.
CASE5:
%)6)-T vbelv posnv vbeln posnn vbtyp<n rfmng meins
I7T; T"#6) i<vbfa 1&;, vbfa
>*)&) vbelv I7 so<vbeln
"7+ posnv I7 so<posnv
"7+ vbeln 8) c<vbeln
"7+ posnn 8) c<posnn
"7+ vbtyp<n 8) c<vbtyp<n.
This will definitely improve the performance to a considerable extent.
6ast but the most important point is 9*irst :rimar. )e. of t;e ta(le s;ould
al&a.s e<ist in t;e &;ere clause of t;e select =uer.>. This thing we can simply check
by executing any table having more than one primary key field. If we are able to provide
first primary key then the select will be very fast. This again depends on business
re/uirements. In some of the cases we can.t provide the first primary key. In this case at
least try to provide second primary key.
"nother techni/ue is if we have non primary keys in where clause then first select
the data based on primary keys and later delete the internal table records which doesn.t
satisfy non primary key conditions. This techni/ue will also improve performance to some
extent.

5.? *" ALL E!T"IES:
6
%)6)-TD )7+%)6)-T or 7)%T)+ %)6)-T% can be effectively replaced with the usage
of 1;& "66 )7T&I)%. In case of %)6)-T )7+%)6)-T the data base will be fired e/ual to the
number of entries fetched which increases the network traffic to a great extent and hence
degrades the performance of not only your ob3ect but also other ob3ects which are accessing
the same data base. The best solution for this is to get the data in a single fetch. This can
be done by using 1;& "66 )7T&I)% addition in %)6)-T.
This will also work in the same way as %)6)-T )7+%)6)-T but we will get all the
records in a single fetch. "lthough it is advisable to use this we should do some mandatory
checks before using this. %ome of these are
4. -*)- whether the driver table is I7ITI"6 or 7;T.(,andatory!
5. %;&T the +river table based on the fields to be specified in >*)&) -6"0%). (To
Improve performance!
(. +)6)T) "+?"-)7T +0$6I-"T)% 1&;, the +river table. (To Improve performance!
+raw backs: "lthough it.s very good to use this, still there are a few drawbacks.
4. If we don.t check the +river table is I7ITI"6 or 7;T and if it is I7ITI"6 then the select
will fetch all the entries in data base which is an unexpected result.
5. 1ield type and length of +river table and driven table must match in the >*)&) clause.
;therwise program throws a syntax error. %ome times for this re/uirement we need to
take some extra internal table.
(. 1;& "66 )7T&I)% automatically deletes duplicate records. To avoid this we have select
all the key fields even if we are not going to use them.
6.@"DE" B4 or E<:licit S"TA
?ust look at the following results of the test program I have created. These results are
the average of 4E test runs for each set of number of records.
T"#6)%: mara.
%)6)-T2;$TI;7%: so<matnr 1;& mara2matnr.
+"T": w<matnr T=$) mara2matnr,
w<time4 T=$) p +)-I,"6% 5,
w<time5 T=$) p +)-I,"6% 5,
w<time( T=$) p +)-I,"6% 5,
w<lines T=$) i,
9 i<mara T=$) %;&T)+ T"#6) ;1 mara >IT* 07IB0) )= matnr.
i<mara T=$) %T"7+"&+ T"#6) ;1 mara.
9;rder by
8)T &07 TI,) 1I)6+ w<time4.
%)6)-T 9 I7T; T"#6) i<mara
1&;, mara
>*)&) matnr I7 so<matnr
;&+)& #= ,"T7&.
8)T &07 TI,) 1I)6+ w<time5.
w<time( F ( w<time5 2 w<time4 ! ' 4EEEE.
7
>&IT): G#y using ;rder by:G, w<time(.
9%ort
-6)"&: w<time4, w<time5, w<time(.
&)1&)%* i<maraHI.
8)T &07 TI,) 1I)6+ w<time4.
%)6)-T 9 I7T; T"#6) i<mara
1&;, mara
>*)&) matnr I7 so<matnr.
%;&T i<mara #= matnr.
8)T &07 TI,) 1I)6+ w<time5.
w<time( F ( w<time5 2 w<time4 ! ' 4EEEE.
>&IT): 'G#y using %ort:G, w<time(.
-6)"& w<lines.
+)%-&I#) T"#6) i<mara 6I7)% w<lines.
>&IT):' G7o. of recordsG, w<lines.
"esults for i'mara T4PE STA!DA"D TABLE * mara:
!o. of records
fetc;ed
Internal ta(le
t.:e
"un Time
rder B4 S"T
5J %T"7+"&+ .JK 4.5
LJ %T"7+"&+ 4.M 5.L
KEEE %T"7+"&+ ME.M 4E5.(
4JJJJ %T"7+"&+ NNO.JK 5OO(.(5
4OLKE %T"7+"&+ (4NK.5 MOJK.O5
N4J5M %T"7+"&+ 444OM.OM 54ENE.5M
8
"esults for i'mara T4PE S"TED TABLE * mara ,IT- #!IB#E 1E4 matnr:
9
Dra& (ac)s of rder (.: If we specify order by clause in the select then sorting is done
in +ata base server. This affects other users who access database at that time. In contrary
to ;rder by if we use explicit %;&T then sorting will be done on application server which
affects only a few users. %o it is advisable to use explicit sort. "lso as the number of records
in the data base increase both take almost same time. %o it is better to select the data and
then explicitly %;&T.
7ever ever use %)6)-T f1 f5CC.fn.. E!D SELECT. %elect all the data in a single
fetch, store it in an internal table and later process that as per your re/uirement. 1re/uent
triggering of data base not only affects your program execution time but also affects other
users who concurrently access the same data base which is /uite common scenario in
production. This is because data base access works on /ueuing principles.
6. LPS:
!o. of records
fetc;ed
Internal ta(le t.:e "un Time
rder B4 &it;out rder (.
5J %;&T)+ T"#6) 4.EL 5.EK
LJ %;&T)+ T"#6) 4.KL 5.LE
KEEE %;&T)+ T"#6) KK4.5 4E5K
4JJJJ %;&T)+ T"#6) L4O.M MMJL.(J
4OLKE %;&T)+ T"#6) LLL.K OMJN.O
N4J5M %;&T)+ T"#6) 4E4MM(.K 4EMNMM.(
10
;nce data is collected from various database tables into internal tables we have to
process the data as per the business re/uirements. To meet this re/uirement we often
use loops. 6oop is nothing but reading'processing the data record by record. If we have
huge volume of data then program performance goes further down in addition to the
time eaten by select statements. $roper design of the program logic will enhance the
performance to a great extent.
"lways try to avoid nested loops unless there is no way to achieve the re/uirement. 1ew
techni/ues to improve the performance of 6oops are as below.
1. LP AT ,-E"E or LP &it; C-EC1 statementA
>e often confuse between 6oop at >here clause or 6oop with check statement. "s
per my analysis it depends on number of matching records. If the number of
matches are less then both tends to takes almost same time. #ut as the number of
matching records increases 6oop with check is giving better performance. -heck
below test results.
,it; C;ec)
statement D +illi
seconds E
,it; ,;ere
clause D +illi
seconds E
!um(er of
matc;es
Total records
in internal
ta(le
M(E.J MMO.L 4EEE 4(K,5K5
5,MJJ 5,OJJ MEEE 4(K,5K5
M455 MM54 4EEEE 4(K,5K5
5JML4 5LJJN MEEEE 4(K,5K5
(NJ4N K5NO5 OMEEE 4(K,5K5
M5JK5 MOJ5O 4EEEEE 4(K,5K5
MOJJN JKM(E 4((,LN4 4(K,5K5
11
5. Parallel cursor tec;ni=ue:
7ested loops are one of the fear factors for all the "#"$ers as this also has ma3or
share of program execution time. If the number of entries in the nested internal
tables is huge, then the situation would be still worse. "lthough it is advisable
that not to use nested loops, it is not possible to completely avoid nested loops
because of business re/uirements. In these cases we have to look at alternative
ways of improving performance. ;ne of the solutions for this is to using parallel
cursor method whenever there is a need for nested loop. -heck below sample
code which is self explanatory and of course the amazing results.
&)$;&T zvinu.
T"#6)%: vbak.
%)6)-T2;$TI;7%: so<vbeln 1;& vbak2vbeln,
so<erdat 1;& vbak2erdat.
$"&",)T)&%: po<rows T=$) i.
T=$)%: #)8I7 ;1 t<vbak,
vbeln T=$) vbak2vbeln,
erdat T=$) vbak2erdat,
)7+ ;1 t<vbak,
#)8I7 ;1 t<vbap,
vbeln T=$) vbap2vbeln,
posnr T=$) vbap2posnr,
)7+ ;1 t<vbap.
+"T": i<vbak T=$) %T"7+"&+ T"#6) ;1 t<vbak,
i<vbap T=$) %T"7+"&+ T"#6) ;1 t<vbap,
wa<vbak T=$) t<vbak,
wa<vbap T=$) t<vbap,
w<time4 T=$) i,
w<time5 T=$) i,
w<time( T=$) i,
w<count T=$) i,
w<index T=$) sy2index.
%)6)-T vbeln erdat 0$ T; po<rows &;>%
I7T; T"#6) i<vbak
1&;, vbak
>*)&) vbeln I7 so<vbeln
"7+ erdat I7 so<erdat.
>&IT):'4 GTotal number of header recordsG, sy2dbcnt.
-*)- 7;T i<vbakHI I% I7ITI"6.
%)6)-T vbeln posnr
I7T; T"#6) i<vbap
1&;, vbap
1;& "66 )7T&I)% I7 i<vbak
>*)&) vbeln )B i<vbak2vbeln.
12
>&IT):'4 GTotal number of item recordsG, sy2dbcnt.
%;&T: i<vbak #= vbeln,
i<vbap #= vbeln.
+; 4E TI,)%.
-6)"&: w<time4, w<time5, w<time(, w<count.
8)T &07 TI,) 1I)6+ w<time4.
96oop at where clause
6;;$ "T i<vbak I7T; wa<vbak.
6;;$ "T i<vbap I7T; wa<vbap >*)&) vbeln )B wa<vbak2vbeln.
)7+6;;$.
)7+6;;$.
8)T &07 TI,) 1I)6+ w<time5.
w<time( F w<time5 2 w<time4.
>&IT): '4 GTime taken for where clauseG, w<time(,
'4 G7umber matching recordsG, w<count.
-6)"&: w<time4, w<time5, w<time(, w<count.
8)T &07 TI,) 1I)6+ w<time4.
9$arallel cursor techni/ue
,;V) 4 T; w<index.
6;;$ "T i<vbak I7T; wa<vbak.
6;;$ "T i<vbap I7T; wa<vbap 1&;, w<index.
I1 wa<vbak2vbeln 7) wa<vbap2vbeln.
w<index F sy2tabix.
)@IT.
)7+I1.
)7+6;;$.
)7+6;;$.
8)T &07 TI,) 1I)6+ w<time5.
w<time( F w<time5 2 w<time4.
>&IT): '4 GTime taken for parallel processingG , w<time(,
'4 G7umber matching recordsG, w<count.
)7++;.
Loo: at ,;ere
clause D +illi
seconds E
Parallel
cursor
D +illi
!um(er of item
records
!um(er of ;eader
records
(NO4O( 5N(( 4EK( 4EEE
5E5KOJ4 5OK(4 MNLL MEEE
55KO(JM OE544 45,(O4 4EEEE
5,(EL,E5M 4JL,MOO 5N,K44 5M,EEE
5,KON,J4N 55O,MOO MJ,LN( ME,EEE
5,OL(,EEK KKE,(K4 LJ,(NN OM,EEE
(,KLK,(LO KJE,JMM 44J,K4M 4EE,EEE
13
7. *ield S.m(ols:
1ield symbols are one of the most effective ways of modifying internal tables. They
improve the performance by 4E@ to ME@.
These exactly work in the same way as pointers work in P-Q language. 1ollowing
example shows how a field symbol can be used to modify internal table.
-6)"& wa<discounts.
6;;$ "T i<discounts I7T; wa<discounts.
-"%) wa<discounts2kschl.
>*)7 c<zdE4.
&)"+ T"#6) i<postdata I7T; wa<postdata >IT* )=
record<id F wa<discounts2record<id
fieldname F c<vvdE4
#I7"&= %)"&-*.
I1 sy2subrc I% I7ITI"6.
>a<postdata2value F wa<discounts2vvdE5.
,;+I1= i<postdata 1&;, wa<postdata I7+)@ sy2tabix.
)7+I1.
>*)7 c<zdE5.
&)"+ T"#6) i<postdata I7T; wa<postdata >IT* )=
record<id F wa<discounts2record<id
fieldname F c<vvdE5
#I7"&= %)"&-*.
I1 sy2subrc I% I7ITI"6.
14
>a<postdata2value F wa<discounts2vvdE5.
,;+I1= i<postdata 1&;, wa<postdata I7+)@ sy2tabix.
)7+I1.
)7+-"%).
)7+6;;$.
The same code can be written like this which improved performance by 4E@ in our case.
1I)6+2%=,#;6%: Rwa<postdataS T=$) bapi<copa<data.
-6)"& wa<discounts.
6;;$ "T i<discounts I7T; wa<discounts.
-"%) wa<discounts2kschl.
>*)7 c<zdE4.
07"%%I87 Rwa<postdataS.
&)"+ T"#6) i<postdata >IT* )=
record<id F wa<discounts2record<id
fieldname F c<vvdE4
"%%I87I78 Rwa<postdataS.
I1 sy2subrc I% I7ITI"6.
Rwa<postdataS2value F wa<discounts2vvdE4.
)7+I1.
>*)7 c<zdE5.
07"%%I87 Rwa<postdataS.
&)"+ T"#6) i<postdata >IT* )=
record<id F wa<discounts2record<id
fieldname F c<vvdE5
"%%I87I78 Rwa<postdataS.
I1 sy2subrc I% I7ITI"6.
Rwa<postdataS2value F wa<discounts2vvdE5.
)7+I1.
-6)"& wa<discounts.
)7+6;;$.
"fter assigning value to the field symbol if we change any value the same will be
reflected in the internal table. To clear the contents of field symbol use 07"%%I87
statement.
Dra& (ac): 7eed to take extra care because if we unintentionally do some thing in work
area the same will be updated in internal table.
?. "ead Ta(le:
$oints to be taken care while using read table statement:
15
4. "lways try to use #I7"&= %)"&-* addition in &)"+ T"#6) which improves
performance to a greater extent. If we don.t use #I7"&= %)"&-* then every time it
searches from the beginning of the internal table. ,ake sure that the table is sorted in
the same order of fields specified in >IT* )= addition. . "lso #I7"&= search will work
only if we sort the table in ascending order.
5. If you are sure of the position of the record then specify I7+)@ addition instead of
>IT* )= addition.
(. If you are interested in getting values of only some fields then use addition
T&"7%$;&TI78 field list. This reduces the transportation cost to a great extent.
K. If you want to check only the existence of the entry then use addition T&"7%$;&TI78
7; 1I)6+%.
M. 7ever ever use &)"+ without #I7"&= %)"&-* on standard and sorted table (Internal
table defined with T=$) %T"7+"&+ T"#6) ;1' T=$) %;&T)+ T"#6) ;1! if it has more
than 4E rows. The reading costs are logarithmic without #I7"&= %)"&-*.
J. If the number of records is less ("round 5E! then 6I7)"& %)"&-* (+efault search! is
faster than #I7"&= %)"&-*.
#elow are the results on a sample program. Internal tables are defined with T=$)
%T"7+"&+ T"#6) ;1 and with explicit work areas.
,it;out BI!A"4
SEA"C- D+illi
secondsE
,it; BI!A"4
SEA"C- D+illi
secondsE
!um(er of records
N 4E 4E
LO NL 5E
5MJ 4K4 ME
LEO 5LN 4EE
4J(EE 4MMM MEE
JK(EE (5JE 4EEE
O(M,NEM 444OO (EEE
4MNNMNK 4LJNE MEEE
4O(JJMO (NEEJ 4EEEE
5EOJ4KM 4OOO55 5MEEE
5K(MNNM 5MM(EK MEEEE
5MN5ON( KOLENJ OMEEE
5JE5LOK MN4JJM 4EEEEE
16
@. +odif.FDeleteFInsertFA::endFu:date:
1. +odif.:
It is a very common re/uirement to modify the internal tables while doing
logical processing. %ome of the points to be remembered while modifying internal
table are
1. "lways use addition T&"7%$;&TI78 f4 f5Dfn. >hen we are interested in
modifying only certain fields, then there is no need to transport all the fields of
the work area.
5. It is advisable to specify I7+)@ which is much faster than a >here clause. >here
clause always re/uires full table scan i.e. it checks each entry in the table with
where clause conditions. If we are using ,;+I1= with where clause inside loop
then you can assume how hectic it is.
)8:
,;+I1= i<tab 1&;, wa<tab I7+)@ sy2tabix T&"7%$;&TI78 fld4 fld5.
,;+I1= i<tab 1&;, wa<tab T&"7%$;&TI78 fld4 fld5.
#ut by specifying I7+)@ we can change one entry at a time. "dvantage with
>*)&) clause is, we can change more than one entry with single ,;+I1=
statement.
6. If we are modifying the internal table based on some field in a loop, check the
possibility of using "T 7)> event.
).g. If we can have duplicate records based on some field and ,;+I1= is also on
the same field then probably we can use "T 7)> event. ,;+I1= the table in "T
7)> event with I7+)@ and >*)&) -6"0%) additions.
5 Delete:
17
This is also one of the important performance barriers. >hen ever we delete a
record from an internal table the system has to recreate the index for all the
entries if the table is of type %T"7+"&+ or %;&T)+ table because these tables
work on I7+)@ mechanism. This will be more time consuming if the internal table
is of very big size and deletion fre/uency is high. It is always better to exclude
the entries through where clause of select statement. If it is not possible then
delete the entries in single operation. 7ever use +)6)T) statement inside a loop
unless there is no other way to achieve our re/uirement. +eleting in single step
will have index maintenance costs also once and in case of loop it is e/ual to
number of deletions.
Dont do:
6;;$ at i<ekpo I7T; wa<ekpo.
I1 wa<matnrTE(4! )B lc<m
+)6)T) i<ekpo 1&;, wa<ekpo I7+)@ sy2tabix.
)7+I1.
)7+6;;$.
D:
+)6)T) i<ekpo >*)&) matnrTE(4! )B lc<m.
Deleting adGacent du:licates:
7ever forgot to %;&T the table by the same fields that are used in
-;,$"&I78 clause of +)6)T) "+?"-)7T +0$6I-"T)%. If we want to delete
duplicates based on key fields (7ot with full primary key as we can.t have duplicates
for full primary key! then the best way is to select all the key fields in select
statement which automatically avoids duplicates.
7. A::end:
"ppend data into internal tables in a single step rather than doing in a loop.
Dont do:
6;;$ "T i<ekko I7T; wa<ekko.
"$$)7+ wa<ekko T; i<ekko<post.
)7+6;;$.
Do:
"$$)7+ 6I7)% ;1 i<ekko to i<ekko<post.
Co:.ing internal ta(les:
18
-opying in a single step will be faster than doing it in a loop.
Dont do:
6;;$ "T i<ekko I7T; wa<ekko.
"$$)7+ wa<ekko T; i<ekko<post.
)7+6;;$.
Do:
i<ekko<postHI F i<ekkoHI.
?. InsertF#:date:
%ame conditions as discussed above will apply to I7%)&T and update also. "lways
insert data into data base table in bulk but not line by line.
Internal table: If the internal table is of type %T"7+"&+ T"#6)' %;&T)+ T"#6) then it will
work on I7+)@ mechanism.
).g.: If the table is already sorted based on some key and if we I7%)&T the data then it
may disturb the %;&T order. To avoid this first &)"+ the table >IT* #I7"&= %)"&-*. %y2
subrc value will be set to K and sy2tabix value will be set to a value where this record would
have been if at all it exists.
-onsider below example.
If we have internal table (i<tab! data like below.
14 15 1(
4 asd fgfr
( fdf fdsf.
&)"+ T"#6) i<tab I7T; wa<tab >IT* )= 14 F 5 #I7"&= %)"&-*.
"fter this statement sy2subrc will be K and sy2tabix will be 5(>hich is nothing but the
position if a record with key 14 F 5 was there. %o instead of appending and sorting again we
can directly I7%)&T the record at this position.
H. Al&a.s #se *+s.
19
"lways try to use function modules instead of doing the calculations manually. >e have
%"$ provided 1,s readily available for most of the re/uirements. >e need to find the correct
one which suits our re/uirement.
"nother advantage of using 1,s is if at all any changes happen in future upgrades 1,s
will take care of these changes. %ome of the best examples where we can use 1, instead of
coding our own logic or selecting data from tables are.
4. 8etting customer address by using 1, "++&<8)T
5. 8etting user details by using #"$I<0%)&<8)T<+)T"I6
(. 8etting change documents using 1,s -*"78)+;-0,)7T<&)"+<*)"+)&% and
-*"78)+;-0,)7T<&)"+<$;%ITI;7% will be faster than getting data from -+*+&
and -+$;% tables.
K. -urrency'Buantity'+ate conversions.
M. -onverting some fields to internal'external format
).g. "dding or deleting leading zeros by using 1,
-;7V)&%I;7<)@IT<"6$*"<I7$0T ' ;0T$0T.
I. #sage of *"EE Statement:
>e can use this statement to relieve the memory occupied by the internal table. If
we are sure that we don.t use internal table anymore then use *"EE i'ita( to relieve the
internal memory occupied by the table. This statement causes slight improvement in
performance only when the number of internal tables is more. >hen internal memory is not
sufficient while executing program we will get a runtime error. #ut practically this situation
may not arise.
J. #se BAPI instead of BDC.
"lways search for some #"$I before starting the coding using #+-. #+- is already
obsolete in the current versions of %"$.
10.Conditional statements.
In programming we will be using so many conditional statements which are also
performance barriers. #y taking proper care of the design we can enhance the
performance.
a. "lways try to avoid use of 7egative statements like 7;T I7, I1 7;T.
b. &eplace 7ested ifs' If elseifD elseifDelseDwith -"%) statement.
c. >here ever possible replace I1 statement by -*)- statement.
11.Data declarations:
20
>onder how the data declarations affect the performanceU It.s true from the
analysis.
It is advisable to do the data declarations inside the 6ocal 1;&,s unless it is not being
used at multiple places. If we don.t use some variable at more than one place then what
is the need of declaring it in the global space and using global memory throughout the
execution of the programU It may not directly affect the performance but above
explanation makes sense.
Some useful transactions:
1. SE60:
%)(E is the one of the best transactions provided by %"$ to learn the optimization
techni/ues. There are thousands of techni/ues available which can be used to achieve
simple to complex re/uirements in most optimal way. #elow simple example shows
how to analyze a report by using %)(E.
STEPS:
1. 1ill in initial screen as shown below with your transaction'report'1, name to be
analyzed.
21
2. 7ext press on execute button, 7ow you will be directed to the program
selection screen. 1ill in the selection screen values, execute and come back
after output is displayed.
22
3. 7ow you will be in initial screen again and gets a message saying measurement
ended. 7ow press analyze button.
4. #elow screen will be displayed.
23
5. *ere we can observe ( things. Time taken from "#"$ side i.e. #usiness logic,
Time taken by the operations performed on database i.e. %elect statements
and the final one is &'( system which is time taken for system operations. *ere
u can observe time taken at data base level is near to NKV. This is because we
are not passing even single primary key to the select statements. That is why
the database time is more.
1or better performance, "#"$ time should be around OEV to NEV, database time
should be MV to 4EV 2 and &'( system time should be around 5EV 2 (EV.
To do a detailed performance analysis check below transactions.
ST0?: %B6, )n/ueue, +e/ueue and &1- trace.
ST60: 8lobal $erformance "nalysis
STAD: %tatistical &ecords.
T.:ical :erformance tuning c;ec) list:
;tt::FF&&&.sa:tec;nical.comFTutorialsFABAPFPerformanceC;ec)List.;tm
"eferences:
&&&.;el:.sa:.com
&&&.sdn.sa:.com
&&&.sa:tec;nical.com

You might also like