‎2006 Mar 30 4:24 AM
Hello experts, I have a function module that runs so slow, I just want to seek some advice to help me have a better performance speed what can I used and alternatives to make my codes faster... any advice can help
Heres my <b> code </b>
<b>Declaration</b>
FUNCTION-POOL YXBW_0PRODORDER_ATTR. "MESSAGE-ID ..
TYPE-POOLS: RSAP, RSSG, SRSC, SRSD, RSAOT, RSFH.
DATA: ippord_bw LIKE ppord_bw.
DATA : itabix LIKE sy-tabix.
DATA : V_OBJNR LIKE JEST-OBJNR.
DATA : BEGIN OF i_data_temp OCCURS 0.
INCLUDE STRUCTURE ppord_bw.
DATA : END OF i_data_temp.
data: begin of i_jest_obj occurs 0,
objnr like jest-objnr,
end of i_jest_obj.
********************************
*TYPES Declaration *
********************************
TYPES : BEGIN OF t_afpo,
aufnr LIKE afpo-aufnr, "Order Number
plnum LIKE afpo-plnum, "Planned order number
psamg LIKE afpo-psamg, "Scrap quantity in item
psmng LIKE afpo-psmng, "Order item quantity
wemng LIKE afpo-wemng, "Quantity of goods for the order item
amein LIKE afpo-amein, "Unit of measure for in-house production
meins LIKE afpo-meins, "Base unit of measure
pamng LIKE afpo-pamng, "Fixed quantity of scrap from production
pgmng LIKE afpo-pgmng, "Total planned order quantity
ablad LIKE afpo-ablad, "Unloading point
END OF t_afpo.
TYPES: BEGIN OF t_afko,
aufnr LIKE afko-aufnr, "Order Number
gltrp LIKE afko-gltrp, "Basic finish date
gstrp LIKE afko-gstrp, "Basic start date
ftrms LIKE afko-ftrms, "Scheduled release date
gltrs LIKE afko-gltrs, "Scheduled finish
gstrs LIKE afko-gstrs, "Scheduled start
gstri LIKE afko-gstri, "Actual start date
gltri LIKE afko-gltri, "Actual finish date
ftrmi LIKE afko-ftrmi, "Actual release date
gmein LIKE afko-gmein, "Base unit of measure
plnty LIKE afko-plnty, "Task list type
plnnr LIKE afko-plnnr, "Key for task groupKey
plnal LIKE afko-plnal, "Group counter
aufld LIKE afko-aufld, "Date of BOM routing transfer
aufpl LIKE afko-aufpl,"Routing number for operations
igmng LIKE afko-igmng, "Yield confirmed order confirmation
iasmg LIKE afko-iasmg, "Scrap confirmed for order
END OF t_afko.
TYPES: BEGIN OF t_afvc,
aufpl LIKE afvc-aufpl, "Routing number for operations order
arbid LIKE afvc-arbid, "Object ID of the resource
END OF t_afvc.
TYPES: BEGIN OF t_zbw_afvv_c,
aufpl LIKE zbw_afvv_c-aufpl, "Routing number
ism03 LIKE zbw_afvv_c-ism03, "Previously confirmed activity
ile03 LIKE zbw_afvv_c-ile03, "Unit of measure
vgw02 LIKE zbw_afvv_c-vgw02, "Standard value
vge02 LIKE zbw_afvv_c-vge02, "Unit of measure standard value
isdd LIKE zbw_afvv_c-isdd, "Actual start: Execution (date)
isdz LIKE zbw_afvv_c-isdz, "Actual start: Execution (time)
END OF t_zbw_afvv_c.
TYPES: BEGIN OF t_CRHD,
objid LIKE crhd-objid, "Object ID of the resource
arbpl LIKE crhd-arbpl, "Work Center
END OF t_CRHD.
TYPES: BEGIN OF t_jest,
objnr LIKE jest-objnr, "Object Number
stat LIKE jest-stat, "Status
END OF t_jest.
********************************
*Internal Table Declaration *
********************************
DATA: i_afpo TYPE STANDARD TABLE OF t_afpo,
wa_afpo TYPE t_afpo,
i_afko TYPE STANDARD TABLE OF t_afko,
wa_afko TYPE t_afko,
i_afvc TYPE STANDARD TABLE OF t_afvc,
wa_afvc TYPE t_afvc,
i_afvc_d TYPE STANDARD TABLE OF t_afvc,
wa_afvc_d TYPE t_afvc,
i_jest TYPE STANDARD TABLE OF t_jest,
wa_jest TYPE t_jest,
i_zbw_afvv_c TYPE STANDARD TABLE OF t_zbw_afvv_c,
wa_zbw_afvv_c TYPE t_zbw_afvv_c,
i_CRHD TYPE STANDARD TABLE OF t_CRHD,
wa_CRHD TYPE t_CRHD.
<b>Actual Code</b>
i_data_temp[] = i_t_data[].
IF i_data_temp[] IS INITIAL.
RAISE rsap_customer_exit_error.
EXIT.
ELSE.
retrieves values of appended fields from afpo table
SELECT aufnr "Order Number
plnum "Planned order number
psamg "Scrap quantity in item
psmng "Order item quantity
wemng "Quantity of goods for the order item
amein "Unit of measure for in-house production
meins "Base unit of measure
pamng "Fixed quantity of scrap from production
pgmng "Total planned order quantity
ablad "Unloading point
FROM afpo
INTO TABLE i_afpo
FOR ALL ENTRIES IN i_data_temp
WHERE aufnr = i_data_temp-aufnr.
IF sy-subrc = 0.
ENDIF.
retrieves values of appended fields and aufpl from afko table that
will be used as a look up in table afvv
SELECT aufnr "Order Number
gltrp "Basic finish date
gstrp "Basic start date
ftrms "Scheduled release date
gltrs "Scheduled finish
gstrs "Scheduled start
gstri "Actual start date
gltri "Actual finish date
ftrmi "Actual release date
gmein "Base unit of measure
plnty "Task list type
plnnr "Key for task groupKey
plnal "Group counter
aufld "Date of BOM routing transfer
aufpl "Routing number for operations
igmng "Yield confirmed order confirmation
iasmg "Scrap confirmed for order
FROM afko
INTO TABLE i_afko
FOR ALL ENTRIES IN i_afpo
WHERE aufnr = i_afpo-aufnr.
IF sy-subrc = 0.
ENDIF.
loop at i_data_temp.
CONCATENATE 'OR' i_data_temp-aufnr into i_jest_obj-objnr.
append i_jest_obj.
endloop.
retrieves appended fields from afvv table
SELECT aufpl "Routing number
ism03 "Previously confirmed activity
ile03 "Unit of measure
vgw02 "Standard value
vge02 "Unit of measure standard value
isdd "Actual start: Execution (date)
isdz "Actual start: Execution (time)
FROM zbw_afvv_c
INTO TABLE i_zbw_afvv_c
FOR ALL ENTRIES IN i_afko
WHERE aufpl = i_afko-aufpl.
IF sy-subrc = 0.
ENDIF.
retrieves ARBID from afvc table that will be used as a lookup in CHRD
table to get the workcenter
SELECT aufpl "Routing number for operations order
arbid "Object ID of the resource
FROM afvc
INTO TABLE i_afvc
FOR ALL ENTRIES IN i_afko
WHERE aufpl = i_afko-aufpl.
IF sy-subrc = 0.
ENDIF.
retrieves Work Center based on the arbid selected from afvc table
SELECT objid "Object ID of the resource
arbpl " Work Center
FROM crhd
INTO TABLE i_crhd
FOR ALL ENTRIES IN i_afvc
WHERE objid = i_afvc-arbid.
IF sy-subrc = 0.
ENDIF.
SELECT objnr "Object Number
stat "Status
FROM jest
INTO TABLE i_jest
for all entries in i_jest_obj
where objnr = i_jest_obj-objnr
AND ( stat EQ 'I0045' OR stat EQ 'I0076' OR stat EQ 'I0012' ).
IF SY-SUBRC = 0.
ENDIF.
ENDIF.
MAIN PROCESSING *****
i_afvc_d[] = i_afvc[].
SORT i_afvc BY aufpl ASCENDING.
SORT i_afvc_d BY aufpl DESCENDING.
SORT : i_afko BY aufnr,
i_afpo BY aufnr,
i_zbw_afvv_c BY aufpl.
LOOP AT i_t_data INTO ippord_bw.
itabix = sy-tabix.
clears work area
CLEAR: wa_afpo,
wa_afko,
wa_afvc,
wa_zbw_afvv_c,
wa_crhd.
read internal table i_afpo
READ TABLE i_afpo INTO wa_afpo
WITH KEY aufnr = ippord_bw-aufnr.
IF sy-subrc = 0.
ippord_bw-zzwemng = wa_afpo-wemng.
ippord_bw-zzamein = wa_afpo-amein.
ippord_bw-amein = wa_afpo-amein.
ippord_bw-zzpsamg = wa_afpo-psamg.
ippord_bw-zzpgmng = wa_afpo-pgmng.
ippord_bw-zzpamng = wa_afpo-pamng.
ippord_bw-zzpsmng = wa_afpo-psmng.
ippord_bw-psmng = wa_afpo-psmng.
ippord_bw-zzmeins = wa_afpo-meins.
ippord_bw-zzplnum = wa_afpo-plnum.
ippord_bw-zzablad = wa_afpo-ablad.
ENDIF.
read internal table i_afko
READ TABLE i_afko INTO wa_afko
WITH KEY aufnr = ippord_bw-aufnr.
IF sy-subrc = 0.
ippord_bw-zzftrms = wa_afko-ftrms.
ippord_bw-zzgstri = wa_afko-gstri.
ippord_bw-zzftrmi = wa_afko-ftrmi.
ippord_bw-zzgstrp = wa_afko-gstrp.
ippord_bw-zziasmg = wa_afko-iasmg.
ippord_bw-zzigmng = wa_afko-igmng.
ippord_bw-zzgmein = wa_afko-gmein.
ippord_bw-zzplnnr = wa_afko-plnnr.
ippord_bw-zzplnty = wa_afko-plnty.
ippord_bw-zzplnal = wa_afko-plnal.
ippord_bw-zzaufld = wa_afko-aufld.
ippord_bw-zzgltrs = wa_afko-gltrs.
ippord_bw-zzgstrs = wa_afko-gstrs.
ippord_bw-zzgltri = wa_afko-gltri.
ippord_bw-zzgltrp = wa_afko-gltrp.
read internal table i_afvv
READ TABLE i_zbw_afvv_c INTO wa_zbw_afvv_c
WITH KEY aufpl = wa_afko-aufpl.
IF sy-subrc = 0.
ippord_bw-ism03 = wa_zbw_afvv_c-ism03.
ippord_bw-ile03 = wa_zbw_afvv_c-ile03.
ippord_bw-vgw02 = wa_zbw_afvv_c-vgw02.
ippord_bw-vge02 = wa_zbw_afvv_c-vge02.
ippord_bw-isdd = wa_zbw_afvv_c-isdd.
ippord_bw-isdz = wa_zbw_afvv_c-isdz.
ENDIF.
*Pull first resource
READ TABLE i_afvc INTO wa_afvc
WITH KEY wa_zbw_afvv_c-aufpl.
IF sy-subrc = 0.
READ TABLE i_crhd INTO wa_crhd
WITH KEY objid = wa_afvc-arbid.
IF sy-subrc = 0.
ippord_bw-zzarbpl1 = wa_crhd-arbpl.
ENDIF. "read i_crhd
ENDIF. "read i_afvc
*Pull last resource
CLEAR: wa_afvc_d,
wa_crhd.
READ TABLE i_afvc_d INTO wa_afvc_d
WITH KEY wa_zbw_afvv_c-aufpl.
IF sy-subrc = 0.
READ TABLE i_crhd INTO wa_crhd
WITH KEY objid = wa_afvc_d-arbid.
IF sy-subrc = 0.
ippord_bw-zzarbpl2 = wa_crhd-arbpl.
ENDIF. "read i_crhd
ENDIF. "read i_afvc
ENDIF. "read i_afko
CLEAR v_objnr.
READ TABLE i_data_temp INDEX itabix.
SHIFT i_data_temp-aufnr LEFT BY 5 PLACES.
CONCATENATE 'OR00000' i_data_temp-aufnr INTO v_objnr.
TECO Status
READ TABLE i_jest INTO wa_jest
WITH KEY objnr = v_objnr
stat = 'I0045'.
IF sy-subrc = 0.
ippord_bw-zzteco = 'X'.
ENDIF.
Deletion Flag Status
READ TABLE i_jest INTO wa_jest
WITH KEY objnr = v_objnr
stat = 'I0076'.
IF sy-subrc = 0.
ippord_bw-zzdlfl = 'X'.
ENDIF.
Delivered Status
READ TABLE i_jest INTO wa_jest
WITH KEY objnr = v_objnr
stat = 'I0012'.
IF sy-subrc = 0.
ippord_bw-zzdlv = 'X'.
ENDIF.
MODIFY i_t_data FROM ippord_bw INDEX itabix.
ENDLOOP.
refreshes content of internal table
REFRESH: i_data_temp,
i_afko,
i_afpo,
i_afvc,
i_afvc_d,
i_jest,
i_zbw_afvv_c.
frees up the memory space containing the internal table
FREE: i_data_temp,
i_afko,
i_afpo,
i_afvc,
i_afvc_d,
i_jest,
i_crhd.
‎2006 Mar 30 4:44 AM
I didn't look at your selects; maybe they could be improved. What I noticed is that after the selects, you basically do:
loop at i_t_data.
read table1 with key field1 = ...
read table2 with key field2 = ...
read tableetc with key fieldetc = ...
This will be inefficient. Try doing this:
sort table1 by field1.
sort table2 by field2.
sort tableetc by fieldetc.
loop at i_t_data.
read table1 with key field1 = ... binary search.
read table2 with key field2 = ... binary search.
read tableetc with key fieldetc = ...binary search.
endloop.
Rob
‎2006 Mar 30 4:36 AM
It might help if you can use SORTED tables, so that you can use a BINARY SEARCH in the READ statement. It might make a difference.
Looks like most of the impact is from the SELECT statements though.
regards,
Ravi
‎2006 Mar 30 4:41 AM
‎2006 Mar 30 4:44 AM
I didn't look at your selects; maybe they could be improved. What I noticed is that after the selects, you basically do:
loop at i_t_data.
read table1 with key field1 = ...
read table2 with key field2 = ...
read tableetc with key fieldetc = ...
This will be inefficient. Try doing this:
sort table1 by field1.
sort table2 by field2.
sort tableetc by fieldetc.
loop at i_t_data.
read table1 with key field1 = ... binary search.
read table2 with key field2 = ... binary search.
read tableetc with key fieldetc = ...binary search.
endloop.
Rob
‎2006 Mar 30 4:52 AM
sir I already have a sort in the above code, so I must used the binary search in the read table? btw can I implement index in this code? If yes how?, Is the used of Field-Symbol better than work area?
thanks and regards
Chris
‎2006 Mar 30 4:59 AM
I missed the sorts, but you have to specify the addition 'binary search', or it will just do a sequential read and exit after the first hit. I would do that first because it'll be the easiest thing to do. If it doesn't help, we can look at the selects.
But if the tables are large, it should help.
Rob
‎2006 Mar 30 4:59 AM
I would suggest trying to use the BINARY SEARCH on your READ statements as others have suggested, this will help. You select statements look pretty good, I believe that you are using the keys for all, so indexes wouldn't help. Field symbols will help a little, but probably not enough to notice, so it may not be worth it.
Regards,
Rich Heilman
‎2006 Mar 30 5:05 AM
Thanks Guyz,
Yes the there are lots opf records because this is a Function Module is being used in BW and currently in the Assurance box, I'll add the binary search in the read table and post my questions again. Thanks to everyone that replied, Ive already assign points, I really appreciate all your help
Regards,
Chris
‎2006 Mar 30 5:13 AM
Also this select could be improved:
* retrieves Work Center based on the arbid selected from afvc table
SELECT objid "Object ID of the resource
arbpl " Work Center
FROM crhd
INTO TABLE i_crhd
FOR ALL ENTRIES IN i_afvc
WHERE objid = i_afvc-arbid.
IF sy-subrc = 0.
ENDIF.
Try:
* retrieves Work Center based on the arbid selected from afvc table
SELECT objid "Object ID of the resource
arbpl " Work Center
FROM crhd
INTO TABLE i_crhd
FOR ALL ENTRIES IN i_afvc
<b>WHERE OBJTY in ('A', 'AA', 'FH', 'H')</b>
WHERE objid = i_afvc-arbid.
IF sy-subrc = 0.
ENDIF.
‎2006 Mar 30 5:34 AM
sir why did you include this OBJTY in ('A', 'AA', 'FH', 'H')? thanks...
‎2006 Mar 30 3:42 PM
OBJTY is the first key field in the primary key and OBJID is the second. If you specify only the second field, it won't be able to use the index as effectively.
I went to the domain for OBJTY and found all of the allowed values for the field and included them in the select. Oddly enough, this allows the select to use the primary index.
However, if CRHD is small, it probably won't make much of a difference. But it can improve performance on larger tables.
I'm working on a BLOG on this subject. I hope to have it ready next week.
Rob
‎2006 Mar 31 3:00 AM