You are on page 1of 41

Performance & Tunning

Programao em ABAP/4

Programao em

Performance &

Assuntos abordados
Performance e Tunning
Acesso a Banco de Dados
Manipulao de Strings
Manipulao de Tabelas Internas
Tipos de Campos
Outros

Programao em

Performance &

Select + Check X Select...Where


select * from sbook.
check: sbook-carrid = LH and
sbook-connid = 0400.
endselect.

select * from sbook


where carrid = LH
and connid = 0400.
endselect.

79.508 s

Programao em

Performance &

54.395 s

Select usando ndice


select * from t100
where arbgb = 00
and msgnr = 999.
endselect.

select * from t002.


select * from t100
where sprsl = t002-spras
and arbgb = 00
and msgnr = 999.
endselect.
endselect.

46.565.368 s

Programao em

Performance &

987.930 s

Select...Endselect X Select Single


select * from scarr
where carrid = LH.
endselect.

select single * from scarr


where carrid = LH.

29.477 s

Programao em

Performance &

28.409 s

Select + Append X Select into table


refresh x006.
select * from t006 into x006.
append x006.
endselect.

select * from t006


into table x006.

58.864 s

Programao em

Performance &

17.962 s

Select...Endselect X Loop
select * from t006.
endselect.

select * from t006


into table x006.
loop at x006.
endloop.

42.985 s

Programao em

Performance &

23.385 s

Operaes automticas do SGBD


c4a = 000.
select * from t100
where sprsl = D
and arbgb = 00.
check: t100-msgnr > c4a.
c4a = t100-msgnr.
endselect.

select max(msgnr)from t100


into c4a
where sprsl = D
and arbgb = 00.

2.187.488 s

Programao em

Performance &

167.781 s

Select utilizando Views


select * from dd01l
where domname like CHAR%
and as4local
= A.
select single * from dd01t
where domname = dd01l-domname
and as4local = A
and as4vers = dd01l-as4vers
and ddlanguage = sy-langu.
endselect.

select * from dd01v


where domname like CHAR%
and ddlanguage = sy-langu.
Endselect.

12.372.996 s

3.890.100 s

Programao em

Performance &

Select com For all entries


select * from t002.
select * from t100
where sprsl = t002-spras
and arbgb = 00
and msgnr = 999.
endselect.
endselect.

Programao em

select * from t002


into table t_t002.
select * from t100
into table t_t100
for all entries in t_t002
where sprsl = t_t002-spras
and arbgb = 00
and msgnr = 999.

Performance &

For all entries X Inner Join


select matnr mfrnr
into table t_mara
from mara.
select lifnr
into
from
for
where

select m~matnr m~mrfnr l~name1


into table t_result
from mara as m inner join
lfa1 as l
on m~mrfnr = l~lifnr.

name1
table t_lfa1
lfa1
all entries in t_mara
lifnr = t_mara-mrfnr.

clear
t_result.
refresh t_result.
loop at t_mara.
read table t_lfa1
with key lifnr = t_mara-mrfnr
binary search.
check sy-subrc is initial.
move: t_mara-matnr to t_result-matnr,
t_mara-mrfnr to t_result-mrfnr,
t_lfa1-name1 to t_result-name1.
append t_result.
endloop.

140.000 s

Programao em

Performance &

114.000 s

For all entries X Letf Outer Join


select matnr mfrnr
into table t_mara
from mara.
select lifnr name1
into table t_lfa1
from lfa1
for all entries in t_mara
where lifnr = t_mara-mrfnr.
clear
t_result.
refresh t_result.
loop at t_mara.
read table t_lfa1
with key lifnr = t_mara-mrfnr
binary search.
if sy-subrc is initial.
move: t_mara-matnr to t_result-matnr,
t_mara-mrfnr to t_result-mrfnr,
t_lfa1-name1 to t_result-name1.
append t_result.
else.
move: t_mara-matnr to t_result-matnr,
t_mara-mrfnr to t_result-mrfnr.
clear t_result-name1.
append t_result.
endloop.

select m~matnr m~mrfnr l~name1


into table t_result
from mara as m left outer join
lfa1 as l
on m~mrfnr = l~lifnr.

143.000 s

Programao em

Performance &

125.000 s

Select de colunas especficas


select * from dd01l
where domname like CHAR%
and as4local
= A.
endselect.

select domname from dd01l


into dd01l-domname
where domname like CHAR%
and as4local
= A.
endselect.

1.645.070 s

Programao em

Performance &

266.631 s

Select de tabelas bufferizadas


select single * from t100
bypassing buffer
where sprsl = D
and arbgb = 00
and msgnr = 999.

select single
where
and
and

32.273 s

Programao em

Performance &

* from t100
sprsl = D
arbgb = 00
msgnr = 999.

2.037 s

Resumo de Acesso a BD
Sempre que utilizar o comando SELECT
procure:
No utilizar o comando GET
Evitar o operador LIKE
Acessar a tabela por ndice
Carregar a tabela numa tabela interna
Utilizar Views
Especificar as colunas
Utilizar bufferizao
Utilizar o automatismo do SGBD
Programao em

Performance &

Atualizao Por Linha X Por Bloco


loop at tab.
insert into customers values tab.
Endloop.

Programao em

insert customers from table tab.

Performance &

Update Por Linha X Por Coluna


select * from sflight.
subtract 1 from sflght-seatsocc.
update sflight.
endloop.

update sflight
set seatsocc = seatsocc - 1.

260.952 s

Programao em

Performance &

56.595 s

Resumo de Atualizao
Sempre que utilizar comandos de
atualizao de BD procure:
Executar os comandos para blocos de
linhas
Executar os comandos para o menor
nmero de colunas possvel

Programao em

Performance &

Operadores para Strings


assign cha(1) to <c>.

if cha(200) ca ().
...
endif.

do 200 times.
if <c> = ( or <c> = ).
...
exit.
endif.
assign <c>+1 to <c>.
enddo.

8.413 s

Programao em

Performance &

607 s

Funes X Comandos para Strings


call function
string_concatenate_3
exporting
string1
= t100-arbgb
string2
= t100-msgnr
string3
= t100-text
importing
string
= cla
exceptions
too_small = 01.

concatenate t100-arbgb
t100-msgnr
t100-text
into cla.

1.468 s

Programao em

Performance &

117 s

Algoritmos X Comandos para Strings


move Jos
to cma.
move Silva
to cmb.
move So Paulo to cmc.

move Jos
to cma.
move Silva
to cmb.
move So Paulo to cmc.

i1 = strlen(cma).
i2 = strlen(cmb).
i3 = strlen(cmc).

concatenate Sr.
cma
cmb
de
cmc
into cha
separated by spaces.

move
move
i1 =
move
i1 =
move
i1 =
move

Sr. to
cma
to
i1 + 5.
cmb
to
i1 + i2 +
de to
i1 + 3.
cmc
to

cha.
cha+4.
cha+i1.
1.
cha+i1.
cha+i1.

Sr. Jos Silva de So Paulo o valor final


de CHA.

Sr. Jos Silva de So Paulo o valor final de


CHA.

638 s

Programao em

Performance &

195s

Resumo Sobre Strings


Sempre que tiver que manipular strings
procure:
Utilizar operadores especficos do ABAP/4
Utilizar comandos especficos do ABAP/4

Programao em

Performance &

Criando uma Tabela Condensada


loop at tab_src.
read table tab_dest
with key k = tab_scr-k
binary search.
if sy-subrc is initial.
add: tab_src-val1 to
tab_dest-val1,
tab_src-val2 to
tab_dest-val2.
modify tab_dest
index sy-tabix.
else.
insert tab_src into tab_dest
index sy-tabix.
endif.
endloop.

loop at tab_src.
collect tab_src into tab_dest.
endloop.
sort tab_dest by k.

9.324.565 s

Programao em

Performance &

270.060 s

Criando Tabela Sem Duplicidades


refresh tab_dest.
loop at tab_src.
read table tab_dest
with key k = tab_scr-k
binary search.
if not sy-subrc is initial.
insert tab_src into tab_dest
index sy-tabix.
endif.
endloop.

loop at tab_src.
Append tab_src to tab_dest.
endloop.
sort tab_dest by k.
delete adjacent duplicates
from tab_dest
comparing k.

335.602 s

Programao em

Performance &

257.364 s

Busca Linear X Binria


read table tab with key k = X.

read table tab with key k = X


binary search.

10.604 s

Programao em

Performance &

206 s

Chave Implcita X Explcita


move space to tab.
tab-k = X.
read table tab bynary search.

read table tab with key k = X


binary search.

331 s

Programao em

Performance &

148 s

Busca Linear X ndice Secundrio


read table tab_index with key
date = sy-datum
binary search.
if sy-subrc is initial.
read table tab index
tab_index-indx.
...
endif.

read table tab with key


date = sy-datum.
if sy-subrc is initial.
...
endif.

5.576 s

Programao em

Performance &

281 s

Loop + Check X Loop...Where


loop at tab.
check tab-k = kval.
...
endloop.

loop at tab where k = kval.


...
endloop.

9.091 s

Programao em

Performance &

3.284 s

Movimentao Explcita X Automtica


tab = tab_wa.
append tab.

append tab_wa to tab.

134 s

Programao em

Performance &

95 s

Cpia de Tabelas Internas


refresh tab_dest.
loop at tab_src into tab_dest.
append tab_dest.
endloop.

tab_dest[] = tab_src[].

8.554 s

Programao em

Performance &

2.446 s

Comparao de Tabelas Internas


describe table: tab1 lines l1,
tab2 lines l2.
if l1 <> l2.
tab_dif = X.
else.
clear tab_dif.
loop at tab1.
read table tab2
index sy-tabix.
if tab1 <> tab2.
tab_dif = X.
exit.
endif.
endloop.
endif.
if not tab_dif is initial.
...
endif.

if tab1[] <> tab2[].


...
endif.

14.292 s

Programao em

Performance &

5.506 s

Join de Tabelas Internas


i2 = 1.
loop at tab1.
read table tab2 index i2.
if not sy-subrc is initial.
exit.
endif.
if tab2-k = tab1-k.
...
endif.
endloop.

loop at tab1.
read table tab2
with key k = tab1-k
binary search.
if sy-subrc is initial.
...
endif.
endloop.

234.312 s

Programao em

Performance &

83.260 s

Loops Aninhados de Tabelas Internas


loop at tab1.
loop at tab2 where k = tab1-k.
...
endloop.
endloop.

i2 = 1.
loop at tab1.
loop at tab2 from i2.
if tab2-k <> tab1-k.
I2 = sy-tabix.
exit.
endif.
...
endloop.
endloop.

3.026.439 s

Programao em

Performance &

76.059 s

Modificao de Tabelas Internas


tab-date = sy-datum.
do 500 times.
modify tab index sy-index.
enddo.

tab-date = sy-datum.
do 500 times.
modify tab index sy-index
transporting date.
enddo.

50.234 s

Programao em

Performance &

27.735 s

Incluso ao Final de Tabelas Internas


loop at tab_src.
append tab_src to tab_dest.
endloop.

append lines of tab_src


to tab_dest.

49.025 s

Programao em

Performance &

16.397 s

Incluso no Meio de Tabelas Internas


idx = 250.
loop at tab_src.
insert tab_src into tab_dest
index idx.
add 1 to idx.
endloop.

idx = 250.
insert lines of tab_src
into tab_dest
index idx.

179.852 s

Programao em

Performance &

18.204 s

Eliminao de Linhas Duplicadas


read table tab_dest index 1
into prev_line.
loop at tab_dest from 2.
if tab_dest = prev_line.
delete tab_dest.
else.
prev_line = tab_dest.
endif.
endloop.

delete adjacent duplicates from


tab_dest comparing k.

239.180 s

Programao em

Performance &

37.750 s

Eliminao de Linhas em Sequncia


delete tab_dest from 450 to 550.

do 101 times.
delete tab_dest index 450.
enddo.

23.068 s

Programao em

Performance &

906 s

Resumo Sobre Tabelas Internas


Sempre que tiver que manipular tabelas
internas procure:
Utilizar comandos especficos do ABAP/4
Utilizar clusulas que melhorem a
performance (WHERE, FROM, etc)
Utilizar a busca binria ao invs da linear
Utilizar algoritmos previamente
desenvolvidos e testados
Utilizar manipulao em blocos
Programao em

Performance &

Tipos de Campos
Para melhorar a performance:
Especifique os tipos de qualquer campo;
Utiliza campos tipo I como ndices de
tabelas internas;
Faa movimentao entre campos de tipos
diferentes s se no existir alternativa;
No utilize tipos N para realizar clculos;
Faa clculos entre campos de tipos
diferentes s se no existir alternativa.
Programao em

Performance &

Outras Observaes
Para melhorar a performance:
O comando CASE melhor que o IF;
O comando WHILE melhor que o DO,
quando se aplicar;
O MOVE e a atribuio direta so
equivalentes;
O ADD/SUBTRACT/COMPUTE e a
atribuio direta com clculo so
equivalentes;
O IF e o CHECK so equivalentes.
Programao em

Performance &

You might also like