Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Select single within Loop

Former Member
0 Likes
2,055

Hi,

I have to optimize an existing code, this program runs monthly with huge data. It is using an inner join with more than 2 database tables. Also, which is better:

1. Select into itab2 for all entries in itab1, then Loop at itab1 and Read itab2.

2. Loop at itab1, then select single from database table.

select afpo~matnr

makt~maktx

afpo~charg

afpo~wempf

afko~aufnr

afpo~wemng

afko~gamng

afko~stlbez

afko~stlst

afko~stlnr

afko~sdatv

mara~ferth

s022~arbpl

appending corresponding fields of table it_porder

from afpo inner join afko on afpoaufnr = afkoaufnr

inner join aufk on afpoaufnr = aufkaufnr

inner join mara on afpomatnr = maramatnr

inner join makt on afpomatnr = maktmatnr

and makt~spras = sy-langu

inner join s022 on afpoaufnr = s022aufnr

and afpomatnr = s022matnr

where afpo~matnr in p_matnr

and afko~aufnr in p_aufnr

and afko~gstrp in p_date

and mara~mtart in ('FERT', 'HALB')

and aufk~auart in ('FO11', 'PO11')

and afko~gltri ne '00000000'.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,465

Hi,

The first option is always better. But make sure your itab1 has entries.

Reward points if useful.

Regards,

Atish

8 REPLIES 8
Read only

Former Member
0 Likes
1,466

Hi,

The first option is always better. But make sure your itab1 has entries.

Reward points if useful.

Regards,

Atish

Read only

Former Member
0 Likes
1,465

As per my analysis since i have done Trace for complete select query's

You can use Inner joins max 3 tables ,if it is more than 3 tables then go for all entries

Use select single if you have primary key condition otherwise use Select upto 1 row

it is not good idea use select single within loop intead of that use normal select query outside of the loop and within loop use Read table with binary search.

Use Clear,refresh,free command properly

if you want to compare diffrent select query use get run time field command.

simple example :

data : a type i,

b type i,

c type i.

start-of-selection.

get runtime field a.

  • here select query

get runtime field b.

  • here a is starting time ,b ending time

c = b - a.

write 😕 c.

Thanks

Seshu

Read only

Former Member
0 Likes
1,465

Hi

It all depends upon the no of comparision primary fields available.

write the code for both and try running and check the performance during the peak hours then remove the other one

Comment the some tables check the performance andf remove the tables which are taking more time. for them use FOR ALL ENTRIES

and then use binary and read table

This will surely increase the performance of the report

Regards

Shiva

Read only

Former Member
0 Likes
1,465

Thanks for the responses.

I have a question on inner join. In the select statement above, if a record does not exist on table S022 but all the conditions in the where clause are satisfied, will the record still be included?

Read only

0 Likes
1,465

Hi

While using INNER JOINS, if any one condition fails for any of db table, it will not fetch the record. So, if there is no record existing in S022, it will not return any record.

Regarding you question on for all entries and select single in loop,

Do not use Select statement in loop.

FOR ALL ENTRIES: When the records in the internal table are in millions, then the select query will take lot of time.

Inorder to optimize the query, push the records of internal table into a buffer table and sort the records by the field used for comparing. Now delete adjacent duplicates from the buffer table.

This will reduce the number of records and hence optimize the query using for all entries.

Regards

Navneet

Read only

Former Member
0 Likes
1,465

The answer to your last question about an entry not exisitng in one table but existing in other is no. An inner join only returns records that match all join conditions. An outer join can be used to return values that exist in on table but not on another along with those that do match. But generally you are safer removing that tbale from your SELECT and peforming an additinal select using FOR ALL ENTRIES

Read only

Former Member
0 Likes
1,465

The join on S022 isn't using any key fields. If it is a large table, that is probably the source of your problem. Try taking just that out to see if the remaining SELECT runs more quickly.

rob

Read only

Former Member
0 Likes
1,465

Thanks everyone.