04-16-2009 7:19 AM
way of best performance of below code.
LOOP AT itab.
CLEAR wa_d_wt.
SELECT * FROM mseg WHERE matnr EQ itab-o_matnr AND charg EQ itab-o_charg AND
( bwart = '601' OR bwart = '641' OR bwart = 602 OR bwart = '642' ).
IF mseg-bwart = '601' OR mseg-bwart = '641'.
wa_d_wt = wa_d_wt + mseg-menge.
ELSE.
wa_d_wt = wa_d_wt - mseg-menge.
ENDIF.
ENDSELECT.
IF wa_d_wt IS NOT INITIAL.
itab-d_wt = wa_d_wt.
MODIFY itab INDEX sy-tabix TRANSPORTING d_wt.
ENDIF.
endloop.
04-16-2009 7:22 AM
First get MBLNR from MKPF according to ur requirement , then pass those MBLNR to MSEG to get
ur data. Sample select query as follows---
SELECT MBLNR FROM MKPF INTO CORRESPONDING FIELDS OF IT_MKPF WHERE BUDAT BETWEEN DATE-LOW AND DATE-HIGH
AND MJAHR = MJAHR.
SELECT * FROM MSEG INTO CORRESPONDING FIELDS OF IT_MSEG WHERE MBLNR = IT_MKPF-MBLNR
AND WERKS = S_WERKS AND MJAHR = MJAHR.
Edited by: mujib tirandaz on Apr 16, 2009 8:23 AM
04-16-2009 8:33 AM
Hi Tomar,
1st use for all entries in itab and fetch data from mseg filling i_mseg
Then loop itab and inside that read i_mseg with key and accordingly modify itab.
Let me know if you want more details about how to implement code.
Always remember to avoid select queries inside loop.
Regards,
Pranil Shinde.
04-16-2009 8:44 AM
Hi Tomar,
I cannot understand the IF clause in your below code. Irrespective of the mvoement type (BWART) you are adding the quantity (MENGE) hence you can
IF itab[] IS NOT INITIAL.
SELECT matnr charg menge
FROM mseg
INTO TABLE it_mseg << has only fields mantnr, charg and menge
FOR ALL ENTRIES IN itab
WHERE matnr EQ itab-o_matnr
AND charg EQ itab-o_charg
AND bwart IN r_bwart <<where r_bwart is a RANGE of mseg-bwart and contains the bwarts u want
ORDER BY matnr charg.
LOOP AT it_mseg INTO w_mseg. << use internal tables with header lines better for performance
AT END OF charg.
SUM.
READ TABLE itab INTO wa WITH KEY o_matnr = w_mseg-matnr o_charg = w_mseg-charg.
IF sy-subrc EQ 0.
wa-d_wt = w_mseg-menge.
MODIFY itab FROM wa INDEX sy-tabix TRANSPORTING d_wt. << If you loop assignging field
<< symbol it will improve performance further
ENDIF.
ENDAT.
ENDLOOP.
ENDIF.
There might be syntax errors and a few corrections, but the above covers gist of that I think can be improved.
Cheers,
Aditya
04-16-2009 11:54 AM
Hi,
please find the below optimized code.
1) First move the data from itab to itab_tmp.
2) sort itab_tmp by MATNR CHARG.
then delete the duplicate entries from iatb_tmp comapring MATNR CHARG.
For futher processing check the below code.
define internal table i_mseg with fields matnr charg menge.
data : v_menge type mseg-menge.
IF itab_tmp[] IS NOT INITIAL.
SELECT matnr charg menge
FROM mseg
INTO TABLE it_mseg
FOR ALL ENTRIES IN itab_tmp
WHERE matnr EQ itab-o_matnr
AND charg EQ itab-o_charg
AND bwart IN ('601', '641','602','642').
if sy-subrc = 0.
sort itab by matnr chagr.
sort i_mseg by matnr charg.
LOOP AT itab into wa_itab.
READ TABLE i_mseg INTO wa_mseg WITH KEY matnr = wa_itab-matnr charg = wa_itab-charg
bianry search.
IF sy-subrc EQ 0.
IF mseg-bwart = '601' OR mseg-bwart = '641'.
V_menge = V_menge + w_mseg-menge.
ELSE.
V_menge = V_menge + w_mseg-menge.
ENDIF.
wa_iatb-meng = v_menge.
MODIFY itab FROM wa_itab tranproting menge.
ENDLOOP.
ENDIF.
Edited by: Sudha Rani Pathuri on Apr 16, 2009 4:24 PM
04-16-2009 2:12 PM
04-16-2009 2:47 PM
Hi,
I have faced same problem, but my problem has been solved by creating index on table MSEG in SE11 transaction
so create an index on filelds which you are using in where condition in select statement
Regards
Krishna
04-16-2009 3:00 PM
>
> so create an index on filelds which you are using in where condition in select statement
There already is an SAP delivered index on MATNR.
Rob
04-16-2009 4:03 PM
What is that ???
IF mseg-bwart = '601' OR mseg-bwart = '641'.
wa_d_wt = wa_d_wt + mseg-menge.
ELSE.
wa_d_wt = wa_d_wt - mseg-menge.
ENDIF.
It is the same as
wa_d_wt = wa_d_wt - mseg-menge.
the conditions are useless !
Maybe there is a typo.
Otherwise, if there is no typo, then you could do the summation with an aggregatfunction.
Whether the aggregate is better than a FOR ALL ENTRIES can not be said in general.
Anyway, first of all your IF-clause must be explained.
Siegfried
04-16-2009 4:11 PM
I don't see what the problem is with the IF statement. Depending on the movement tyep, The amount is either added to or subtracted from the stored quantity.
Rob
04-16-2009 4:45 PM
for people not knowing the meaning of bwart it makes sense to mark the difference between the plus and nearly invisible minus.
I am wondering whether there is really a problem with that coding, it looks o.k. Maybe there is small improvement possible by using FAE, but neither of the above recommendation works really.
What is the structure of itab, are the moire fields besides itab-o_matnr and itab-o_charg ?
If not then you can use select for all entries, and create a new table
matnr charg bwart and menge
Sort it
Loop over that table and do the summation and subtraction and append into a new table at every
change of matnr charg.
Then a nested loop can be avoided, and then it could be faster.
Siegfried