SAP ABAP性能優化





1、使用where語句
不推薦
Select * from zflight.
Check : zflight-airln = ‘LF’ and zflight-fligh = ‘BW222’.
Endselect.
推薦
Select * from zflight where airln = ‘LF’ and fligh = ‘BW222’.
Endselect.

2、使用聚合函數
不推薦
Maxnu = 0.
Select * from zflight where airln = ‘LF’ and cntry = ‘IN’.
Check zflight-fligh > maxnu.
Maxnu = zflight-fligh.
Endselect.
推薦
Select max( fligh ) from zflight into maxnu where airln = ‘LF’ and cntry = ‘IN’.


3、使用視圖代替基本表查詢
不推薦
Select * from zcntry where cntry like ‘IN%’.
Select single * from zflight where cntry = zcntry-cntry and airln = ‘LF’.
Endselect.
推薦
Select * from zcnfl where cntry like ‘IN%’ and airln = ‘LF’.
Endselect.

4、使用INTO table 代替select endselect
不推薦
Refresh: int_fligh.
Select * from zflight into int_fligh.
Append int_fligh. Clear int_fligh.
Endselect.
推薦
Refresh: int_fligh.
Select * from zflight into table int_fligh.

5、使用批量修改內表代替逐行修改
不推薦
Loop at int_fligh.
If int_fligh-flag is initial.
Int_fligh-flag = ‘X’.
Endif.
Modify int_fligh.
Endloop.
推薦
Int_fligh-flag = ‘X’.
Modify int_fligh transporting flag where flag is initial.

6、使用二分法查詢,提高查詢內表數據速度
不推薦
Read table int_fligh with key airln = ‘LF’.
推薦
Read table int_fligh with key airln = ‘LF’ binary search.


SAP ABAP 性能優化技巧 — 使用二分查找(Binary Search)選項

READ命令使用順序查找數據表,這會降低處理速度。取而代之,使用binary search的附加命令,可以使用二分查找算法,可以幫助加快內表查找速度。 在使用binary search之前必須首先將內表排序,否則有可能找不到記錄,因爲二分查找反覆將查找區間對半劃分,如果要查找的值小於查找區間的中間位置的數據項值,則查找區間將縮小到前半個區間,否則查找將侷限於後半區間.

 

不推薦使用:

 

Read table int_fligh with key airln = ‘LF’.

 

 

推薦使用:

SORT int_fligh by airln.
Read table int_fligh with key airln = ‘LF’ binary search.

 

應用:

with key 後面不能使用比較符 < >.  

read table itab with key   matnr = lt_mseg_n-matnr

                            pvprs > 0 .   這種寫法不對。

 

可以變通的寫這條語句。

  LOOP at lt_ckmlcr WHERE  matnr = lt_mseg_n-matnr AND pvprs > 0.

.........

  exit.

endloop.


7、兩個內表添加使用批量增加代替逐行
不推薦
Loop at int_fligh1.
Append int_fligh1 to int_fligh2.
Endloop.
推薦
Append lines of int_fligh1 to int_fligh2.

8、 使用FOR ALL Entries
不推薦
Loop at int_cntry.
Select single * from zfligh into int_fligh where cntry = int_cntry-cntry.
Append int_fligh.
Endloop.
推薦
Select * from zfligh appending table int_fligh
For all entries in int_cntry
Where cntry = int_cntry-cntry.

9、使用sort by 代替order by

10、避免使用SELECT DISTINCT語句
使用的 ABAP SORT + DELETE ADJACENT DUPLICATES 代替.


11、兩個實例

DATA: BEGIN OF it_mara OCCURS 0,
matnr LIKE mara-matnr,
maktx LIKE makt-maktx,
END OF it_mara.
第一種寫法:
Select matnr
INTO it_mara
FROM mara.
APPEND it_mara.
ENDSelect.
第二種寫法(high performace):
Select matnr
INTO TABLE it_mara
FROM mara.
==========================================
DATA: BEGIN OF it_mara OCCURS 0,
matnr LIKE mara-matnr,
maktx LIKE makt-maktx,
END OF it_mara.
DATA: BEGIN OF it_makt OCCURS 0,
matnr LIKE mara-matnr,
maktx LIKE makt-maktx,
END OF it_makt.
第一種寫法:
LOOP AT it_mara.
Select SINGLE maktx
INTO it_mara-maktx
FROM makt
Where matnr = it_mara-matnr AND
spras = sy-langu.
MODIFY it_mara TRANSPORTING maktx.
ENDLOOP.
第二種寫法(high performace)
Select matnr maktx
INTO TABLE it_makt
FROM makt
FOR ALL ENTRIES IN it_mara
Where matnr = it_mara-matnr and
spras = sy-langu.
=========================================

1 數據——>工作區,工作區——>內表,
2 數據——>內表
很明顯少了一個過程 效率自然高了 如果數據量越大,效果是可想而知的



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章