‎2019 Jul 01 1:56 PM
Hi,
As I am new to ABAP world, I needed some suggestion regarding an performance issue.
In my program there are multiple select single query hitting database where 100k of data are stored and taking 4-5 hours to fetch the result. I referred many blogs and googled and came to know that I can take all the data from database into internal table and fetch the data. But I am confused how to achieve it. Below is the code. Can anyone please guide me how to make the performance more faster and achieve the fetching in much lesser time. Here c_t_data is being populated with 100k's of records. and then many select single statements are being fired inside it.
LOOP AT c_t_data.
CLEAR: wa_za.
SELECT SINGLE matnr
INTO mara-matnr FROM mara
WHERE matnr = c_t_data-matnr
AND lvorm = space.
CHECK sy-subrc = 0.
SELECT SINGLE user4 waers
INTO (wa_za-yyuser4, wa_za-yywaers)
FROM aufk
WHERE aufnr = c_t_data-aufnr.
SELECT SINGLE kursk prsdt
INTO (wa_za-yykursk, wa_za-yyprsdt)
FROM vbkd
WHERE vbeln = c_t_data-vbeln
AND posnr = '000000'.
SELECT SINGLE kursk prsdt
INTO (wa_za-yykursk, wa_za-yyprsdt)
FROM vbkd
WHERE vbeln = c_t_data-vbeln
AND posnr = c_t_data-posnr.
SELECT SINGLE vbegdat venddat
INTO (wa_za-yyvbegdat, wa_za-yyvenddat)
FROM veda
WHERE vbeln = c_t_data-vbeln
AND vposn = '000000'.
SELECT SINGLE vbegdat venddat
INTO (wa_za-yyvbegdat, wa_za-yyvenddat)
FROM veda
WHERE vbeln = c_t_data-vbeln
AND vposn = c_t_data-posnr.
SELECT tj30t~txt04 INTO wa_za-yycontsts
FROM jest
JOIN jsto
ON jsto~objnr = jest~objnr
JOIN tj30t
ON tj30t~stsma = jsto~stsma
WHERE jest~objnr = c_t_data-objnr
AND jest~inact = space
AND tj30t~stsma = jsto~stsma
AND tj30t~estat = jest~stat
AND tj30t~spras = 'EN'.
ENDSELECT.
CLEAR wa_za-yyconroot.
MOVE c_t_data-vbeln(7) TO wa_za-yyconroot.
SELECT SINGLE knumv INTO vbak-knumv
FROM vbak
WHERE vbeln = c_t_data-vbeln.
SELECT SINGLE kbetr INTO wa_za-yykbetr
FROM konv
WHERE knumv = vbak-knumv and
KPOSN = '000000' and
KSCHL = 'ZDIS'.
SELECT vbeln parvw kunnr pernr parnr
FROM vbpa INTO TABLE itab_vbpa
WHERE vbeln = c_t_data-vbeln.
Thanks in advance,
Kumar.
‎2019 Jul 01 2:06 PM
Hi,
Why are
SELECT SINGLE kursk prsdt
INTO (wa_za-yykursk, wa_za-yyprsdt)
FROM vbkd
WHERE vbeln = c_t_data-vbeln
AND posnr = '000000'.
SELECT SINGLE kursk prsdt
INTO (wa_za-yykursk, wa_za-yyprsdt)
FROM vbkd
WHERE vbeln = c_t_data-vbeln
AND posnr = c_t_data-posnr.
And
SELECT SINGLE vbegdat venddat
INTO (wa_za-yyvbegdat, wa_za-yyvenddat)
FROM veda
WHERE vbeln = c_t_data-vbeln
AND vposn = '000000'.
SELECT SINGLE vbegdat venddat
INTO (wa_za-yyvbegdat, wa_za-yyvenddat)
FROM veda
WHERE vbeln = c_t_data-vbeln
AND vposn = c_t_data-posnr.
Repeated?
You should consolidate some separate single selects into a join.
Other than that you're using header lines which are obsolete, so I would suggest to find an experienced ABAP-er in your company to assist you in this task.
Kind regards, Rob Dielemans
‎2019 Jul 01 2:18 PM
For the repetitious SELECTs (looking for the same record over and over again), you might consider building hashed ITABs for those tables and searching the ITABs first, then looking at database and saving the entries into the ITAB. For your example, this should be useful for the MARA-MATNR at least..
‎2019 Jul 01 2:39 PM
posnr/vposn = '0000000' is fetching for all items. Where as posnr/vposn = c_t_data-posnr is fetching for req order/item.
‎2019 Jul 01 2:43 PM
Rahul,
But why would you do both? When you select for POSNR or VPOSN = zeroes, store the results in an ITAB, then look up individual items from the ITAB and not the database.
‎2019 Jul 01 3:01 PM
Loyd,
You are correct. But I think it is being done posnr/vposn = '000000' first to get at least one data into work area (wa_za is a work area) and then posnr/vposn = c_t_data-posnr to get required data. If c_t_data-posnr is not found then at least wa_za-yykursk/yyprsdt and wa_za-yyvbegdat/yyvenddat will have a value. Please correct me if I am wrong.
‎2019 Jul 01 3:29 PM
Rahul,
I guess that works, but it still seems redundant to go to the database twice. If the result of the '000000' SELECT is stored within the program, then you will have either the specific line item or the "default" result to be used if no specific item exists.
But whoever did the functional requirements either didn't grasp that or couldn't explain it properly in the specs.
‎2019 Jul 01 2:11 PM
Please use the code button when posting code.You can replace:
SELECT tj30t~txt04 INTO wa_za-yycontsts
FROM jest
JOIN jsto
ON jsto~objnr = jest~objnr
JOIN tj30t
ON tj30t~stsma = jsto~stsma
WHERE jest~objnr = c_t_data-objnr
AND jest~inact = space
AND tj30t~stsma = jsto~stsma
AND tj30t~estat = jest~stat
AND tj30t~spras = 'EN'.
ENDSELECT.
with
SELECT single tj30t~txt04 INTO wa_za-yycontsts
FROM jest
JOIN jstoON jsto~objnr = jest~objnr
JOIN tj30tON tj30t~stsma = jsto~stsma
WHERE jest~objnr = c_t_data-objnr
AND jest~inact = space
AND tj30t~stsma = jsto~stsma
AND tj30t~estat = jest~stat
AND tj30t~spras = 'EN'.
It won't help performance, but it makes more sense. You can also merge the VBAK and KONV selects into one select with an inner join. Maybe some of the others. You should look at FOR ALL ENTRIES.
Where is c_t_data populated?
How much data is in the database tables being referenced?
‎2019 Jul 02 2:24 AM
1. your select with item number = 00000 and specific item number from internal table doesnt make sense. put it in 1 select with or condition or just select with document number.
2. loop and select is really poor performance. seem like c_t_data is a standard parameter from an enhancement you may try to sort it then use FOR ALL ENTRIES. if not, try to use join.
‎2019 Jul 02 7:50 AM
Hi Rahul,
I can see that you are using multiple select queries within a loop. As per SAP Standard you must try to decrease the use of Select queries in nested methods.
You can improve ABAP codes by using:
2. Use ABAP CDS
3. Using an Internal Table wherever possible
All the above examples have implemented these concepts.
‎2019 Jul 02 2:03 PM
Hi,
1. First check if there are duplicates in the table c_t_data. if yes, do a copy and delete adj dups and use that for subsequent selects.
2. Remove all selects from loop.
‎2019 Jul 02 3:17 PM
Hi Rahul Kumar, Try to understand how transactions ATRA, ST12 and ST05 work to improve that as well as internal tables like Hashed, Sorted and Standard.
Regards, Roberto
‎2019 Jul 03 9:59 AM
Hi,
If I will remove all the select queries inside of the loop, then how would I implement it then?
Thanks.
‎2019 Jul 03 12:54 PM
Rahul Kumar I guess Dev Parbutteea proposal was to use SELECT ... FOR ALL ENTRIES IN itab_copy_no_duplicates ... before the loop.
‎2019 Jul 09 9:55 AM
Hello,
Thanks for all your valuable feedbacks. I achieved the task.