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

is it OK to use SELECT SUM() inside loop ?

Former Member
0 Likes
1,958

Hi All,

I have used a SELECT SUM(field) ... inside a loop.

The table in use is BSID.

Will it hamper the performance or is it OK ?

Regards,

Ashish

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,303

hi,

Never use select statement inside a loop endloop .... try filtering the data outside it ...

Hi All,

I have used a SELECT SUM(field) ... inside a loop.

The table in use is BSID.

Will it hamper the performance or is it OK ?

Regards,

Ashish

8 REPLIES 8
Read only

Sm1tje
Active Contributor
0 Likes
1,303

Always try and avoid doing SELECTS within loops, this will definetly have a (major) impact on performance.

Read only

Former Member
0 Likes
1,304

hi,

Never use select statement inside a loop endloop .... try filtering the data outside it ...

Read only

0 Likes
1,303

But how will SELECT SUM() make a difference ?

I am not selecting any records .... just a single SUM() of some records based on condition.

Regards,

Ashish

Read only

0 Likes
1,303

the select will still have to go through the (entire) data base records the sum the data.

Read only

0 Likes
1,303

so what is the solution option to NOT using SELECT SUM() inside loop ?

Read only

0 Likes
1,303

First you will have to select all the necessary data from this table (which is a very BIG table normally), according to the data in your internal table used for LOOPing.

Select * (or some fields only) from BSID into itab

for all entries in itab

where ..... =

and......... =

Read only

Former Member
0 Likes
1,303

Hi Ashish,

Of course, you can use select sum() in a loop run. But it's causing huge performance problems. Therefore don't use it! Also don't use any select .... in a loop run.

I recommend to take a look at all of your select statements!

Good luck,

Heinz

Read only

Former Member
0 Likes
1,303

I am wondering, why nobody tells you how to avoid it. Maybe, because there is no alternative?

To move selects out of a loop you usually use FOR ALL ENTRIES. However, FAE can not be used with aggregate functions!

And it is definitely not recommended to read all data and do the summation by yourself, if an aggregate function could do the same.

Your question would be easier to understand, if you add a bit of coding. I assume the following


loop itab into wa
   select  sum(field1)
             into
             from db-tab
             where key1 = wa-key1.
endloop

key1 is not the full key of the db-table so there some or even many record for which the field1 is summed up.

Siegfried