Вы находитесь на странице: 1из 10

TIPS & TRICKS

NO YES

Select using an aggregate function


DATA: MAX_MSGNR type t100-msgnr. MAX_MSGNR = '000'. SELECT !ROM T100 "NTO T100_#A #$ERE S%RSL = 'D' AND AR&G& = '00'. C$EC': T100_#A-MSGNR ( MAX_MSGNR. MAX_MSGNR = T100_#A-MSGNR. ENDSELECT. DATA: MAX_MSGNR type t100-msgnr. SELECT MAX) MSGNR * !ROM T100 "NTO m+,_msgnr #$ERE S%RSL = 'D' AND AR&G& = '00'.

Select ... Up To 1 Rows


SELECT !ROM S&OO' "NTO S&OO'_#A #$ERE CARR"D = 'L$'. EX"T. ENDSELECT. If you are interested if there exists at least one row of a database table or view with a certain condition, use the Select ... Up To 1 Rows statement instead of a Select !ndselect loop with an !xit. If all primary "ey fields are supplied in the #here conditions you can even use Select Single. Select Single re$uires one communication with the database system, whereas Select !ndselect needs two. SELECT !ROM S&OO' "NTO S&OO'_#A -% TO 1 RO#S #$ERE CARR"D = 'L$'. ENDSELECT.

Single line Updates


SELECT !ROM S!L"G$T "NTO S!L"G$T_#A. S!L"G$T_#A-SEATSOCC = S!L"G$T_#A-SEATSOCC - 1. -%DATE S!L"G$T !ROM S!L"G$T_#A. ENDSELECT. #henever possible, use column updates instead of single row updates to update your database tables. %etwor" load is considerably less.

&olumn Update
-%DATE S!L"G$T SET SEATSOCC = SEATSOCC - 1.

Select without index support


SELECT !ROM S&OO' CL"ENT S%EC"!"ED "NTO S&OO'_#A #$ERE CARR"D = 'L$' AND CONN"D = '0.00'. ENDSELECT. 'or all fre$uently used Select statements, try to use an index. (ou always use an index if you specify )a generic part of* the index fields concatenated with logical +nds in the Select statement,s #here clause. %ote that complex #here clauses are poison for the statement optimi-er in any database system.

Select with primary index support


SELECT !ROM S&OO' CL"ENT S%EC"!"ED "NTO S&OO'_#A #$ERE MANDT "N ) SELECT MANDT !ROM T000 * AND CARR"D = 'L$' AND CONN"D = '0.00'. ENDSELECT.

Select without buffer support


SELECT S"NGLE !ROM T100 "NTO T100_#A &/%ASS"NG &-!!ER #$ERE S%RSL = 'D' AND AR&G& = '00' AND MSGNR = '000'. 'or all fre$uently used, read only tables, try to use S+. buffering. %etwor" load is considerably less.

Select with buffer support


SELECT S"NGLE !ROM T100 "NTO T100_#A #$ERE S%RSL = 'D' AND AR&G& = '00' AND MSGNR = '000'.

Select / +ppend statement


DATA T001_#A T/%E T001. CLEAR X001. SELECT !ROM T001 "NTO T001_#A. A%%END T001_#A TO X001. ENDSELECT.

Select Into Table


SELECT !ROM T001 "NTO TA&LE X001. It is always faster to use the Into Table version of a Select statement than to use +ppend statements.

Single line Inserts


Ravi K

+rray Insert
Page 1 4/8/2009

TIPS & TRICKS


LOO% AT TA& "NTO TA&_#A. "NSERT "NTO C-STOMERS 2AL-ES TA&_#A. ENDLOO%. #henever possible, use array operations instead of single row operations to modify your database tables. 're$uent communication between the application program and database system produces considerable overhead. "NSERT C-STOMERS !ROM TA&LE TA&.

Select Into Table t / 0oop at t.


SELECT !ROM T001 "NTO TA&LE X001. LOO% AT X001 "NTO X001_#A. ENDLOO%. If you process your data only once, use a Select !ndselect loop instead of collecting data in an internal table with Select Into Table. Internal table handling ta"es up much more space.

Select ... !ndselect.


SELECT !ROM T001 "NTO X001_#A. ENDSELECT.

%ested Select statements


SELECT !ROM DD01L "NTO DD01L_#A #$ERE DOMNAME L"'E 'C$AR3' AND AS.LOCAL = 'A'. SELECT S"NGLE !ROM DD01T "NTO DD01T_#A #$ERE DOMNAME = DD01L_#A-DOMNAME AND AS.LOCAL = 'A' AND AS.2ERS = DD01L_#A-AS.2ERS AND DDLANG-AGE = S/-LANG-. ENDSELECT.

Select with view


SELECT !ROM DD012 "NTO DD012_#A #$ERE DOMNAME L"'E 'C$AR3' AND DDLANG-AGE = S/-LANG-. ENDSELECT. To process a 1oin, use a view instead of nested Select statements. %etwor" load is considerably less.

%ested Select statements


SELECT !ROM S%!L" "NTO S%!L"_#A. SELECT !ROM S!L"G$T "NTO S!L"G$T_#A #$ERE CARR"D = S%!L"_#A-CARR"D AND CONN"D = S%!L"_#A-CONN"D. ENDSELECT. ENDSELECT. To read data from several logically connected tables use a 1oin instead of nested Select statements. %etwor" load is considerably less.

Select with 1oin


SELECT "NTO #A !ROM S%!L" AS % "NNER 4O"N S!L"G$T AS ! ON %5CARR"D = !5CARR"D AND %5CONN"D = !5CONN"D. ENDSELECT.

Using Two Selects


SELECT !ROM S%!L" "NTO TA&LE T_S%!L" #$ERE C"T/!ROM = '!RAN'!-RT' AND C"T/TO = 'NE# /OR''. SELECT !ROM S!L"G$T AS ! "NTO S!L"G$T_#A !OR ALL ENTR"ES "N T_S%!L" #$ERE SEATSOCC 6 !5SEATSMAX AND CARR"D = T_S%!L"-CARR"D AND CONN"D = T_S%!L"-CONN"D AND !LDATE &ET#EEN '10000101' AND '10000771'. ENDSELECT. Instead of using nested Select loops or '2R +00 !%TRI!S it is often possible to use sub $ueries. %etwor" load is considerably less.

Using + Sub $uery


SELECT !ROM S!L"G$T AS ! "NTO S!L"G$T_#A #$ERE SEATSOCC 6 !5SEATSMAX AND EX"STS ) SELECT !ROM S%!L" #$ERE CARR"D = !5CARR"D AND CONN"D = !5CONN"D AND C"T/!ROM = '!RAN'!-RT' AND C"T/TO = 'NE# /OR'' AND !LDATE &ET#EEN '10000101' AND '10000771'. ENDSELECT.

S!0!&T
SELECT !ROM S&OO' "NTO S&OO'_#A -% TO 10 RO#S. SELECT S"NGLE A"R%!ROM A"R%TO "NTO )A%18 A%9* !ROM S%!L" #$ERE CARR"D = S&OO'_#A-CARR"D AND CONN"D = S&OO'_#A-CONN"D. SELECT S"NGLE NAME "NTO NAME1 !ROM SA"R%ORT

&ontext )for durable data*


SELECT !ROM S&OO' "NTO S&OO'_#A -% TO 10 RO#S. S-%%L/ CARR"D = S&OO'_#A-CARR"D CONN"D = S&OO'_#A-CONN"D TO CONTEXT TRA21. DEMAND A"R%!ROM = A%1 A"R%TO = A%9 NAME_!ROM = NAME1 NAME_TO = NAME9 !ROM CONTEXT TRA21.

Ravi K

Page 2

4/8/2009

TIPS & TRICKS


#$ERE "D = A%1. SELECT S"NGLE NAME "NTO NAME9 !ROM SA"R%ORT #$ERE "D = A%9. ENDSELECT. ENDSELECT. &ontext advantages3 no double fetch by 4!5+%43 1. fetch, 6. get from buffer more performance )best S!0!&T statement* better survey of code

0inear search in an internal table


Entr:es: 10008 L:ne ;:<t=: 100 'ey ;:<t=: 90 T=e READ en<s ;:t= S/-S-&RC=. READ TA&LE "TA& "NTO #A #"T$ 'E/ ' = 'X'. If internal tables are assumed to have many )768* entries, a linear search through all entries is very time consuming. Try to "eep the table ordered and use binary search or used a table of type S2RT!4 T+90!. If T+9 has n entries, linear search runs in 2) n * time, whereas binary search ta"es only 2) log6) n * *.

9inary search in an internal table


Entr:es: 10008 L:ne ;:<t=: 100 'ey ;:<t=: 90 T=e READ en<s ;:t= S/-S-&RC=. READ TA&LE "TA& "NTO #A #"T$ 'E/ ' = 'X' &"NAR/ SEARC$.

4ynamically specified "ey


READ TA&LE "TA& "NTO #A #"T$ 'E/ )NAME* = 'X'. + dynamic "ey access is slower than a static one, since the "ey specification must be evaluated at runtime. :owever, for large tables the costs are dominated by number of comparison needed to locate the entry.

Statically specified "ey


READ TA&LE "TA& "NTO #A #"T$ 'E/ ' = 'X'.

%o secondary index ;7 linear search


READ TA&LE "TA& "NTO #A #"T$ 'E/ DATE = S/-DAT-M. "! S/-S-&RC = 0. > ... END"!. If you need to access an internal table with different "eys repeatedly, "eep your own secondary indices. #ith a secondary index, you can replace a linear search with a binary search plus an index access.

9inary search using secondary index


READ TA&LE SEC_"DX "NTO SEC_"DX_#A #"T$ 'E/ DATE = S/-DAT-M &"NAR/ SEARC$. "! S/-S-&RC = 0. READ TA&LE "TA& "NTO #A "NDEX SEC_"DX_#A-"NDX. > ... END"!.

<ey access with 022.=&:!&<


LOO% AT "TA& "NTO #A. C$EC' #A-' = 'X'. > ... ENDLOO%. 022. ... #:!R! is faster than 022.=&:!&< because 022. ... #:!R! evaluates the specified condition internally. +s with any logical expressions, the performance is better if the operands of a comparison share a common type. The performance can be further enhanced if 022. ... #:!R! is combined with 'R25 i1 and=or T2 i6, if possible.

<ey access with 022. ... #:!R!


LOO% AT "TA& "NTO #A #$ERE ' = 'X'. > ... ENDLOO%.

R!+4 on a uni$ue sorted table


DO 9?0 T"MES. N = . S/-"NDEX. READ TA&LE STA& "NTO #A #"T$ TA&LE 'E/ ' = N. "! S/-S-&RC = 0. > ... END"!. ENDDO. !ntries in a sorted table are located by binary search. The costs depend on the number of entries in the table )2 )log

R!+4 on a uni$ue hashed table


DO 9?0 T"MES. N = . S/-"NDEX. READ TA&LE $TA& "NTO #A #"T$ TA&LE 'E/ ' = N. "! S/-S-&RC = 0. > ... END"!. ENDDO. :ashed tables are optimi-ed for single entry access, whereas sorted tables are optimi-ed for partial se$uential loop

Ravi K

Page 3

4/8/2009

TIPS & TRICKS


n**. !ntries in a hashed table are located by an internal hash algorithm .The costs are constant )2 )1**, i.e. they do not depend on the table si-e. operations )see the separate example*.

.artial se$uential access on a sorted table


LOO% AT $TA& "NTO #A #$ERE ' = S-&'E/. > ... ENDLOO%. :ashed tables are optimi-ed for single entry access. The entries have no specific order. Therefore, a partial se$uential access cannot be optimi-ed. !very entry must be chec"ed to match the condition )full table scan*.

.artial se$uential access on a sorted table


LOO% AT STA& "NTO #A #$ERE ' = S-&'E/. > ... ENDLOO%. Sorted tables are ordered ascendingly by their "ey components. If a partial "ey of the form >"1 ; v1 +%4 ... +%4 "n ; vn> where "1... "n matches a left part of the table "ey, the access is optimi-ed by the "ernel. In that case, only the matching entries are visited.

2ne step approach


RE!RES$ "TA&9. LOO% AT "TA&1 "NTO #A. READ TA&LE "TA&9 #"T$ 'E/ ' = #A-' &"NAR/ SEARC$ TRANS%ORT"NG NO !"ELDS. "! S/-S-&RC 6( 0. "NSERT #A "NTO "TA&9 "NDEX S/-TA&"X. END"!. ENDLOO%. If the data amount is small )? 68 entries*, or if you need read access to the internal table while it is being filled, the one step approach using R!+4=I%S!RT is the right choice. If, however, the data amount is larger and you need read access only to the completely filled table, the three step algorithm is preferable. See also the comparable test for S2RT!4 vs. :+S:!4 T+90!s

Three steps3 move, sort, delete dupl.


"TA&9@A = "TA&1@A. SORT "TA&9 &/ '. DELETE AD4ACENT D-%L"CATES !ROM "TA&9 COM%AR"NG '.

I%S!RT in a sorted table with uni$ue "ey


RE!RES$ "TA&9. LOO% AT "TA&1 "NTO #A. "NSERT #A "NTO TA&LE "TA&9. "! S/-S-&RC 6( 0. > ... END"!. ENDLOO%. (ou can use the same syntax )generic I%S!RT ... I%T2 T+90! ...* for different types of tables )S2RT!4, :+S:!4*. 'illing a sorted table is e$uivalent to building it up by >R!+4 9I%+R( S!+R&:> and >I%S!RT ... I%4!@ S( T+9I@> but it is slightly faster and more elegant to use the generic >I%S!RT ... I%T2 T+90! ...>. See the example for building a uni$ue standard table, if you need no access to the table while it is build up.

I%S!RT in a hashed table with uni$ue "ey


RE!RES$ "TA&9. LOO% AT "TA&1 "NTO #A. "NSERT #A "NTO TA&LE "TA&9. "! S/-S-&RC 6( 0. > ... END"!. ENDLOO%. 'illing a hash table is faster than for a sorted table. (ou can access single entries very fast, but partial se$uential loops are more expensive than for sorted tables )see the separate example*.

5odifying the complete line


#A-DATE = S/-DAT-M. MOD"!/ "TA& !ROM #A "NDEX 1. #ith the 524I'( variant >524I'( itab ... TR+%S.2RTI%A f1 f6 ...> the tas" of updating a line of an internal table can be accelerated. The longer the table line is, the larger the speed up is. The effect increases for tables with complex structured line types.

5odifying selected components only


#A-DATE = S/-DAT-M. MOD"!/ "TA& !ROM #A "NDEX 1 DATE. TRANS%ORT"NG

5odifying all lines completely


LOO% AT "TA& "NTO #A. " = S/-TA&"X MOD 9. "! " = 0. #A-!LAG = 'X'.

5odifying selected components only


LOO% AT "TA& ASS"GN"NG 6#A(. " = S/-TA&"X MOD 9. "! " = 0. 6#A(-!LAG = 'X'.

Ravi K

Page 4

4/8/2009

TIPS & TRICKS


MOD"!/ "TA& !ROM #A. END"!. ENDLOO%. +ccessing the table entries directly in a >022. ... +SSIA%I%A ...> accelerates the tas" of updating a set of lines of an internal table considerably. !specially if inner tables must not be moved the speed up is high. END"!. ENDLOO%.

9ottom up strategy
Entr:es:?0)BCter t+DEe*810):nner t+DEes* L:ne ;:<t=: ?00 )BCter*8 . ):nner* DO ?0 T"MES. CLEAR #A. DO 10 T"MES. A%%END N TO #A-"NTTA&. ADD 1 TO N. ENDDO. A%%END #A TO "TA&. ENDDO. 'illing an internal table with the bottom up strategy, you have to pay move costs several times depending on the depth of the data structure. The contents of nested inner tables are moved to every super ordinate level of the data structure.

Top down strategy


DO ?0 T"MES. A%%END "N"T"AL L"NE TO "TA&. ENDDO. LOO% AT "TA& ASS"GN"NG 6!(. DO 10 T"MES. A%%END N TO 6!(-"NTTA&. ADD 1 TO N. ENDDO. ENDLOO%. 2n the contrary, the top down strategy fills the outer tables first and updates the inner tables directly by using >022. ... +SSIA%I%A>. That is, the contents of inner tables are only moved once.

&200!&T semantics using R!+4 9I%+R(


LOO% AT "TA&1 "NTO #A1. READ TA&LE "TA&9 "NTO #A9 #"T$ 'E/ ' = #A1-' &"NAR/ SEARC$. "! S/-S-&RC = 0. ADD: #A1-2AL1 TO #A9-2AL18 #A1-2AL9 TO #A9-2AL9. MOD"!/ "TA&9 !ROM #A9 "NDEX S/-TA&"X TRANS%ORT"NG 2AL1 2AL9. ELSE. "NSERT #A1 "NTO "TA&9 "NDEX S/-TA&"X. END"!. ENDLOO%.

&ollect via &200!&T


LOO% AT "TA&1 "NTO #A. COLLECT #A "NTO "TA&9. ENDLOO%. SORT "TA&9 &/ '. If you need the &200!&T semantics, 42 use &200!&T B R!+4 9I%+R( runs in 2) log6)n* * time, and the internal table,s index must be ad1usted with each I%S!RT. &200!&T, however, uses a hash algorithm and is therefore independent of the number of entries )i.e. 2)1** and does not need to maintain a table index. If you need the final data sorted, sort it after all data has been collected. If the amount of data is small, the R!+4=I%S!RT approach isn,t bad, but for large amounts of data )7 1888*, &200!&T is much faster. &aution3 #hen you fill an internal table, do not use &200!&T in combination with any other table filling statements )+..!%4, I%S!RT, 524I'(, S!0!&T C I%T2 T+90! and=or S!0!&T C +..!%4I%A T+90!*. If you mix &200!&T with the other statements, &200!&T cannot use its hash algorithm. In this case, &200!&T resorts to a normal linear search, which is dramatically slower3 2)n*.

Array operations .edestrian way to append a table


"TA&1 :s +ppen<e< E:ne Dy E:ne tB "TA&9. LOO% AT "TA&1 "NTO #A. A%%END #A TO "TA&9. ENDLOO%. #ith the +..!%4 variant >+..!%4 0I%!S 2' itab1 T2 itab6> the tas" of appending a table to another table can be transferred to the "ernel.

0et the "ernel do the wor"


"TA&1 :s +ppen<e< :n Bne step tB "TA&9. A%%END L"NES O! "TA&1 TO "TA&9.

Ravi K

Page 5

4/8/2009

TIPS & TRICKS


.edestrian way to insert a table
"TA&1 :s :nserte< E:ne Dy E:ne :ntB "TA&9 +t :n<e, ". " = 9?0. LOO% AT "TA&1 "NTO #A. "NSERT #A "NTO "TA&9 "NDEX ". ADD 1 TO ". ENDLOO%. #ith the I%S!RT variant >I%S!RT 0I%!S 2' itab1 I%T2 itab6 I%4!@ idx> the tas" of inserting a table into another table can be transferred to the "ernel.

0et the "ernel do the wor"


"TA&1 :s :nserte< :n Bne step :ntB "TA&9 +t :n<e, "DX. " = 9?0. "NSERT L"NES O! "TA&1 "NTO "TA&9 "NDEX ".

.edestrian way to delete duplicates


READ TA&LE "TA& "NDEX 1 "NTO %RE2_L"NE. LOO% AT "TA& !ROM 9 "NTO #A. "! #A = %RE2_L"NE. DELETE "TA&. ELSE. %RE2_L"NE = #A. END"!. ENDLOO%.

0et the "ernel do the wor"


DELETE AD4ACENT D-%L"CATES !ROM "TA& COM%AR"NG '. #ith the 4!0!T! variant >4!0!T! +4D+&!%T 4U.0I&+T!S> the tas" of deleting duplicate entries can be transferred to the "ernel.

.edestrian way to delete a se$. of lines


R+nge tB De <eEete<: .?0... ??0 DO 101 T"MES. DELETE "TA& "NDEX .?0. ENDDO. #ith the 4!0!T! variant >4!0!T! itab 'R25 ... T2 ...> the tas" of deleting a se$uence of lines can be transferred to the "ernel.

0et the "ernel do the wor"


DELETE "TA& !ROM .?0 TO ??0.

.edestrian way to copy internal tables


RE!RES$ "TA&9. LOO% AT "TA&1 "NTO #A. A%%END #A TO "TA&9. ENDLOO%.

0et the "ernel do the wor"


"TA&9@A = "TA&1@A. Internal tables can be copied by 52E! 1ust li"e any other data ob1ect. If an internal table itab has a header line, the table itself is accessed by itabFG.

.edestrian way to compare int. tables


&Bt= t+DEes =+Fe t=e s+me GBntents DESCR"&E TA&LE: "TA&1 L"NES L18 "TA&9 L"NES L9. "! L1 6( L9. TA&_D"!!ERENT = 'X'. ELSE. TA&_D"!!ERENT = S%ACE. LOO% AT "TA&1 "NTO #A1. READ TA&LE "TA&9 "NTO #A9 "NDEX S/-TA&"X. "! #A1 6( #A9. TA&_D"!!ERENT = 'X'. EX"T. END"!. ENDLOO%. END"!. "! TA&_D"!!ERENT = S%ACE. > ... END"!.

0et the "ernel do the wor" ...


&Bt= t+DEes =+Fe t=e s+me GBntents "! "TA&1@A = "TA&9@A. > ... END"!.

Internal tables can be compared in logical expressions 1ust li"e other data ob1ects. Two internal tables are e$ual if they have the same number of lines and each pair of corresponding lines is e$ual. If an internal table itab has a header line, the table itself is accessed by itabFG.

Sort int. table with default sort "ey


SORT "TA&. The more restrictively you specify the sort "ey, the faster the program will run.

Sort with sort "ey specified explicitly


SORT "TA& &/ '.

Ravi K

Page 6

4/8/2009

TIPS & TRICKS


Therefore, specify the sort "ey as restrictively as possible.

%aive 1oin3 loop IT+91, read IT+96 w="ey


&Bt= t+DEes sBrte< Dy Cn:HCe Iey ' +sGen<:ng LOO% AT "TA&1 "NTO #A1. READ TA&LE "TA&9 "NTO #A9 #"T$ 'E/ ' = #A1-' &"NAR/ SEARC$. "! S/-S-&RC = 0. > ... END"!. ENDLOO%. If IT+91 has n1 entries and IT+96 has n6 entries, the time needed for 1oining IT+91 and IT+96 with the straightforward algorithm is 2) n1 C log6) n6 * *, whereas the parallel cursor approach ta"es only 2) n1 / n6 * time. The above parallel cursor algorithm assumes that IT+96 is a secondary table containing only entries also contained in primary table IT+91. If this assumption does not hold, the parallel cursor algorithm gets slightly more complicated, but its performance characteristics remain the same.

5ore sophisticated3 use parallel cursors


DATA: " T/%E ". " = 1. LOO% AT "TA&1 "NTO #A1. <B. READ TA&LE "TA&9 "NTO #A9 "NDEX ". "! S/-S-&RC 6( 0. EX"T. END"!. "! #A9-' 6 #A1-'. ADD 1 TO ". ELSE"! #A9-' = #A1-'. > ... ADD 1 TO ". EX"T. ELSE. EX"T. en<:J. en<<B. :J sy-sCDrG 6( 0. e,:t. en<:J. ENDLOO%.

Straightforward nested loop


&Bt= t+DEes sBrte< Dy Iey ' LOO% AT "TA&1 "NTO #A1. LOO% AT "TA&9 "NTO #A9 #$ERE ' = #A1-'. > ... ENDLOO%. ENDLOO%. If IT+91 has n1 entries and IT+96 has n6 entries, the time needed for the nested loop with the straightforward algorithm is 2)n1 C n6*, whereas the parallel cursor approach ta"es only 2)n1 / n6* time.

5ore sophisticated loop3 parallel cursors


" = 1. LOO% AT "TA&1 "NTO #A1. LOO% AT "TA&9 "NTO #A9 !ROM ". "! #A9-' 6( #A1-'. " = S/-TA&"X. EX"T. END"!. > ... ENDLOO%. ENDLOO%. The above parallel cursor algorithm assumes that IT+96 contains only entries also contained in IT+91. If this assumption does not hold, the parallel cursor algorithm gets slightly more complicated, but its performance characteristics remain the same.

Using a sorted table temporarily


Entr:es: 900 )"TA&1*8 100 )"TA&9* "nterseGt:Bn: ?0 )"TA&7* L:ne ;:<t=: 1008 'ey ;:<t=: 90 STA&1 = "TA&1. RE!RES$ "TA&7. LOO% AT "TA&9 ASS"GN"NG 6#A(. READ TA&LE STA&1 !ROM 6#A( TRANS%ORT"NG NO !"ELDS. "! S/-S-&RC = 0. A%%END 6#A( TO "TA&7. END"!. ENDLOO%. !REE STA&1. The source tables IT+91 and IT+96 are standard tables. It is assumed that IT+91 ta"es more entries than IT+96. 2therwise, the table with more entries must be computed with >4!S&RI9! T+90! ... 0I%!S ...H Since both tables shall represent sets, it is assumed that their entries are uni$ue with respect to component <. The algorithm wor"s with a temporary table with uni$ue "ey <. The table is a copy of IT+91 and is used to locate the entries being also contained in IT+96. The matching entries are copied to IT+9I. The left hand and right hand side differ only by the "ind of the

Using a hashed table temporarily


$TA&1 = "TA&1. RE!RES$ "TA&7. LOO% AT "TA&9 ASS"GN"NG 6#A(. READ TA&LE $TA&1 !ROM 6#A( TRANS%ORT"NG NO !"ELDS. "! S/-S-&RC = 0. A%%END 6#A( TO "TA&7. END"!. ENDLOO%. !REE $TA&1.

Ravi K

Page 7

4/8/2009

TIPS & TRICKS


temporary table being used. 'or a hashed table, the R!+4 statement in the 022. is faster than for the sorted table.

Untyped parameters
%ER!ORM -%1 -S"NG 10 M1-D"M"D M1-KAE$L M1"SOCODE M1-ANDEC M1-%R"MAR/. !ORM -%1 -S"NG RE%EAT D"M"D KAE$L "SOCODE ANDEC %R"MAR/ . "<ent:G+E sBCrGe GB<e EeJt +n< r:g=t: DO RE%EAT T"MES. T001_#A-D"M"D = D"M"D. T001_#A-KAE$L = KAE$L. T001_#A-"SOCODE = "SOCODE. T001_#A-ANDEC = ANDEC. T001_#A-%R"MAR/ = %R"MAR/. ENDDO. END!ORM. If you specify the type for formal parameters in your source code, the +9+.=J compiler can optimi-e your code more thoroughly. In addition, the ris" of using the wrong se$uence of parameters in a .erform statement is much less.

Typed parameters
%ER!ORM -%9 -S"NG 10 M1-D"M"D M1-KAE$L M1"SOCODE M1-ANDEC M1-%R"MAR/. !ORM -%9 -S"NG RE%EAT T/%E " D"M"D L"'E T001-D"M"D KAE$L L"'E T001-KAE$L "SOCODE L"'E T001-"SOCODE ANDEC L"'E T001-ANDEC %R"MAR/ L"'E T001-%R"MAR/ . "<ent:G+E sBCrGe GB<e EeJt +n< r:g=t: DO RE%EAT T"MES. T001_#A-D"M"D = D"M"D. T001_#A-KAE$L = KAE$L. T001_#A-"SOCODE = "SOCODE. T001_#A-ANDEC = ANDEC. T001_#A-%R"MAR/ = %R"MAR/. ENDDO. END!ORM.

'ield Symbol without type


!"ELD-S/M&OLS: 6!( T/%E AN/ . DATA: "1 T/%E "8 "9 T/%E ". ASS"GN "1 TO 6!(. "9 = 6!(. If you specify the type of field symbols and formal parameters in your source code, the +9+.=J compiler can better optimi-e your code.

Typed 'ield Symbol


!"ELD-S/M&OLS: 6"( T/%E ". DATA: "1 T/%E "8 "9 T/%E ". ASS"GN "1 TO 6"(. "9 = 6"(.

If
DATA C T/%E C. "! C = 'A'. #R"TE '1'. ELSE"! C = '&'. #R"TE '9'. ELSE"! C = 'C'. #R"TE '7'. ELSE"! C = 'D'. #R"TE '.'. ELSE"! C = 'E'. #R"TE '?'. ELSE"! C = '!'. #R"TE '1'. ELSE"! C = 'G'. #R"TE 'L'. ELSE"! C = '$'. #R"TE 'M'. END"!. &+S! statements are clearer and a little faster than I' constructions.

&ase
DATA C T/%E C. CASE C. #$EN 'A'. #R"TE '1'. #$EN '&'. #R"TE '9'. #$EN 'C'. #R"TE '7'. #$EN 'D'. #R"TE '.'. #$EN 'E'. #R"TE '?'. #$EN '!'. #R"TE '1'. #$EN 'G'. #R"TE 'L'. #$EN '$'. #R"TE 'M'. ENDCASE.

I' i ; c.
DATA C)M* T/%E C. "! 197.?1LM = C. END"!. &omparing & with & its faster as comparing & with a great number

I' c ; c.
DATA C)M* T/%E C. "! '197.?1LM' = C. END"!. &omparing & with & its faster as comparing & with a great number

4o
DATA C T/%E C. DATA " T/%E ". " = 0. DO. "! C NE S%ACE. EX"T. END"!. ADD 1 TO ". "! " GT 10. C = 'X'. END"!. ENDDO.

#hile
DATA C T/%E C. DATA " T/%E ". " = 0. #$"LE C = S%ACE. ADD 1 TO ". "! " GT 10. C = 'X'. END"!. END#$"LE.

Ravi K

Page 8

4/8/2009

TIPS & TRICKS


If you can use #:I0! instead of a 42/!@IT construction, then do so. #:I0! is easier to understand and faster to execute.

&ase
)" = ? :n t=:s test* CASE ". #$EN 1. %ER!ORM #$EN 9. %ER!ORM #$EN 7. %ER!ORM #$EN .. %ER!ORM #$EN ?. %ER!ORM #$EN 1. %ER!ORM #$EN L. %ER!ORM #$EN M. %ER!ORM ENDCASE. %21. %29. %27. %2.. %2?. %21. %2L. %2M.

.erform i 2f ...
)" = ? :n t=:s test* %ER!ORM " O! %21 %29 %27 %2. %2? %21 %2L %2M.

+ very fast way to call a certain routine using a given index is the .erform i 2f ... statement.

FIELD CONVERSION

Type .
DATA % T/%E % 2AL-E 1. READ TA&LE TA& "NTO TA&_#A "NDEX %. Use fields of type I for typical integral variables li"e indices.

Type I
DATA " T/%E " 2AL-E 1. READ TA&LE TA& "NTO TA&_#A "NDEX ".

Type &
MO2E S%ACE TO S/-S-&RC. CASE S/-S-&RC. #$EN '1'. #$EN '9'. #$EN '7'. #$EN '.'. ENDCASE. Use numeric literals or named constants with a number type instead of character strings if you are dealing with type I or integral type . fields.

Type I
S/-S-&RC = 0. CASE S/-S-&RC. #$EN 1. #$EN 9. #$EN 7. #$EN .. ENDCASE.

0iteral Type &


DATA: !LOAT T/%E !. !LOAT = '7.1.1?091?7?M0L079'. Use properly typed constants instead of literals.

&onstant Type '


CONSTANTS: %" T/%E ! 2AL-E '7.1.1?091?7?M0L079'. DATA: !LOAT T/%E !. !LOAT = %".

Type %
DATA: N1)1?* T/%E N 2AL-E '197.?1LM00197.?'8 N9)1?* T/%E N 2AL-E '?.79100ML1?.791'8 N7)1?* T/%E N. N7 = N1 N N9. Use number types for arithmetic. Use type % fields only for pure digit strings that are not intended for calculations, e.g., telephone numbers or parts of a date or time field.

Type .
DATA: %1 T/%E % 2AL-E '197.?1LM00197.?'8 %9 T/%E % 2AL-E '?.79100ML1?.791'8 %7 T/%E %. %7 = %1 N %9.

Several Types
DATA: !1 T/%E " 2AL-E 98 !9 T/%E % DEC"MALS 9 2AL-E '7.1.'8 !7 T/%E !. !7 = !1 !9.

2nly 2ne Type


DATA: !1 T/%E ! 2AL-E 98 !9 T/%E ! 2AL-E '7.1.'8 !7 T/%E !. !7 = !1 !9.

Ravi K

Page 9

4/8/2009

TIPS & TRICKS


4on,t mix types unless absolutely necessary.

&haracter fields )fixed length*


<+t+ G1)900* type G. <+t+ G9)900* type G. <+t+ G7).00* type G. G1 = 'mys+p'. G9 = '.GBm'. GBnG+ten+te G1 G9 :ntB G7. 4epending on the length of the character field, string operation may be faster. The concatenate algorithm has to compute the length of the fixed character fields by scanning the first non blan" character from the end.

String )unfixed length*


<+t+ str:ng1 type str:ng. <+t+ str:ng9 type str:ng. <+t+ str:ng7 type str:ng. str:ng1 = 'mys+p'. str:ng9 = '.GBm'. GBnG+ten+te str:ng1 str:ng9 :ntB str:ng7.

&alling local Subroutines


perJBrm !1. There is no performance loss when you call local methods compared to local subroutines.

&alling 5ethods of local &lasses


G+EE met=B< C1=(M1.

&alling 'unction 5odules


G+EE JCnGt:Bn '!-NCT"ON1'. &alling methods of global classes is faster than calling function modules .

&alling 5ethods of global &lasses


G+EE met=B< CL_%ER!ORMANCE_TEST=(M1.

&alling local 5ethods


G+EE met=B< C1=(M1. &alling global methods is not much slower compared to calling local methods .

&alling global 5ethods


G+EE met=B< CL_%ER!ORMANCE_TEST=(M1.

Ravi K

Page 10

4/8/2009