文章目录
1 Find right table
The Requirment.
Now w fetch data from Finance table :bkpf (head) bseg(line) or faglflext . faglflext
is a good method.
.
2.Fetch data.
2.1 Select /endselect
it loop database with the sentence. it is very slowly. We use “select single” or
select into table for alternative.
2.2 read table
2.2.1 Exampl1.
sort table by matnr.
read table assigning <fs_makt> where manr = . gt_alv-matnr binnary.
2.2.2 Exampl2.
In the loop we try to more use field symbol. Now it don’t up memory.
loop at gt_nakt assigning <fs_makt>.
…
endloop.
2.2.2 Exampl3.
Before optimization
We fetch matnr description and I like it, But the effective is very low.
LOOP
SELECT SINGLE .
ENGLOOP.
After optimization
We try to more use for all entris in and read table.
check gt_mseg[] is not initial.
select matnr maktx into corresponding fields of table lt_makt
from makt
for all entries in gt_mseg
where matnr = gt_mseg-matnr.
sort lt_makt by matnr.
check gt_mseg[] is not initial.
select wgbez matkl into corresponding fields of table lt_t023t
from t023t
for all entries in gt_mseg
where matkl = gt_mseg-matkl.
sort lt_t023t by matkl.
loop at gt_mseg assigning <fs>.
read table lt_makt[] assigning <lfs1> with key matnr = <fs>-matnr binary search.
check sy-subrc = 0.
<fs>-maktx = <lfs1>-maktx.
read table lt_t023t[] assigning <lfs2> with key matkl = <fs>-matkl binary search.
check sy-subrc = 0.
<fs>-wgbez = <lfs2>-wgbez.
search <fs>-wgbez for '/' .
if sy-subrc = 0.
split <fs>-wgbez at lv into <fs>-wgbez1 <fs>-wgbez2 <fs>-wgbez3.
else.
<fs>-wgbez1 = <fs>-wgbez .
<fs>-wgbez2 = <fs>-wgbez .
endif.
<fs>-zmenge = <fs>-menge * <fs>-matgr.
move-corresponding <fs> to gw_alv.
append gw_alv to gt_alv.
modify gt_mseg from <fs> transporting maktx wgbez wgbez1 wgbez2 zmenge .
endloop.
CHECK…
2.3 Loop in the loop.
1:15 mins
3 Summary
When you query for it , the condition is better main key.
You use into table .and you try to use less corresponding ields of table .
We more use spefic field and less use * for query.
When you only get sy-subrc. And you use transporting no fields .
1.尽可能少用多个大表的select,每多一个select就会肯数据库交互一次
2.尽可能不用聚合函数,取出来在内表在操作(比如说group order)
3.尽可能不要用 select *, 尽可能用精确查询,不要用这样的模糊查询;
4.如果只需要一个字段,就用select single ,如果只需要一行,就up 1 rows.
5.尽量不要用loop中用loop,如果数据量足够大,有可能是循环次数是几何倍数的增加;(这种情况下可以通过SELECT… ENDSELECT来一条一条的读,然后sy_dbcnt可以看到读的行数,一般与select single来配合)
6.自建表,如果查询比较慢,则应该做索引。
以上是我目前能想到的如何优化程序的方法,后面如果有,我再慢慢的添加进来.