Application Development 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: 

Need Help in Select Statement

Former Member
0 Kudos

Dear gurus.

Below is my select statement fetching data from table BKPF and comparing it from my internal table.

Problem is that BKPF table contain more then 50,000 entries. which take my program to hang

is there any solution to help my problem.

My Program is a Print program which take two inputs

1) Document No.

2) Facial year.

SELECT belnr bldat xblnr
     FROM bkpf
    INTO TABLE zbkpf
    FOR ALL ENTRIES IN document_tab
    WHERE belnr EQ document_tab-belnr
            AND bukrs EQ 'TPF'
          and  awtyp = 'BKPF'
          AND gjahr = document_tab-gjahr.

Please Help

regards

Saad.Nisar

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Saad,

Please check this code:

If sy-subrc = 0.

Sort document_tab by xblnr gjahr.

If not document_tab[] is initial.

SELECT belnr bldat xblnr

FROM bkpf

INTO TABLE zbkpf

FOR ALL ENTRIES IN document_tab

WHERE belnr EQ document_tab-belnr

AND bukrs EQ 'TPF'

and awtyp = 'BKPF'

AND gjahr = document_tab-gjahr.

endif.

endif.

11 REPLIES 11

Former Member
0 Kudos

Dear Nisar,

1. Try to use inner join instead of for all entries.

2. in below stmt follow key fields order i.e

SELECT belnr bldat xblnr

FROM bkpf

INTO TABLE zbkpf

FOR ALL ENTRIES IN document_tab

WHERE belnr EQ document_tab-belnr

AND gjahr = document_tab-gjahr

AND bukrs EQ 'TPF'

and awtyp = 'BKPF'.

rgds,

Kiran

Former Member
0 Kudos

Are you sure this statement is causing an error? Well for all entries will cause performance issues. But then 50000 records to be queried. Not much I feel. Check your trace to see which statement is causing performance issues. It could be the nested loops or some bad program construct.

Also check if the table used in for all entries is not initial.


if not table[] is initial.
  select ...
  from bkpf
  for all entries in table
  where ....
endif.

Regards,

Abdullah Ismail

Edited by: ZAFCO ABAP on May 28, 2009 10:23 AM

Former Member
0 Kudos

Hi,

For All Entries is not always better than Join.

But, I think 'For All Entries' should do for this query because the query is dealing with just one database table. As a suggestion, try sorting the internal table, remove all the duplicate entries, sort the database table and also check if the internal table is not initial before it processes the query. This will avoid execution of the query if the internal table is empty (For All Entries queries all records of the database table if the internal table is empty). On sorting, the performance could possibly tune itself decelty because the query need not scan the entire table for finding an entry.

Edited by: Nitwick on May 28, 2009 9:09 AM

Former Member
0 Kudos

first put if document_tab not initial command,

then put some more where condition in the select table

just check secondary index in bkpf table

Former Member
0 Kudos

Hi Saad,

Please check this code:

If sy-subrc = 0.

Sort document_tab by xblnr gjahr.

If not document_tab[] is initial.

SELECT belnr bldat xblnr

FROM bkpf

INTO TABLE zbkpf

FOR ALL ENTRIES IN document_tab

WHERE belnr EQ document_tab-belnr

AND bukrs EQ 'TPF'

and awtyp = 'BKPF'

AND gjahr = document_tab-gjahr.

endif.

endif.

Former Member
0 Kudos

Hi Saad Nisar,

You can write the code in this way:

IF NOT document_tab[] IS INITIAL.

SORT document_tab BY BELNR

SELECT belnr bldat xblnr

FROM bkpf

INTO TABLE zbkpf

FOR ALL ENTRIES IN document_tab

WHERE belnr = document_tab-belnr

AND bukrs = 'TPF'

AND gjahr = document_tab-gjahr

AND awtyp = 'BKPF'.

ENDIF.

Hope this may some what increase the performance.

Or Try to check if there is any secondary index present.

Regards,

Prashant

Jelena
Active Contributor
0 Kudos

As correctly suggested above, always check if the table is not initial. Otherwise it will cause the full table scan and the program will run forever.

Additionally, I would recommend using the same order in the SELECT statement as in the primary key of the table, like this:

SELECT belnr bldat xblnr
FROM bkpf 
INTO TABLE zbkpf
FOR ALL ENTRIES IN document_tab 
WHERE bukrs = 'TPF'
AND belnr = document_tab-belnr
AND gjahr = document_tab-gjahr

Although normally the optimizer should take care of this, sometimes coding in the exact order helps (also it makes the code a bit easier to maintain). There is no need for a secondary key since all the fields from a primary key are known.

former_member194613
Active Contributor
0 Kudos

> Although normally the optimizer should take care of this, sometimes coding in the exact order helps

is this really the case, in simple selects I have never seen that

> (also it makes the code a bit easier to maintain).

yes, this is true, also for cursor cache, it is recommended to have the same order as in the table

But if there are more conditions than the primary key, then all conditions should be used, the additional

one are restrictions, i.e. not all lines come into the result !!!

Additionally check whether the FOR ALL ENTRIES has duplicates, if yes, do sort and delete adjacent duplicates.

At the then if there 50.000 records, expected, then it can need 50.000.000 micrsosec, i.e. 50seconds

as a rough estimate. How many records are in BKPF and who fast is your hardware. Even good performance takes some time

Siegfried

Former Member
0 Kudos

Hi,

One thing I would like to bring into your notice is that, when ever you are using for all entries, do select all the key fields, weather you are using those or not using those fields.

Else unexpected result will come.

Thanks,

Krishna..

0 Kudos

Hi,

i am wondering why the biggest issue in this select is not mentioned. Whenever you use FOR ALL ENTRIES try all you can to use only one field of the table in the WHERE clause. As soon as you use more than one table field in the WHERE clause the table buffers are bypassed. So check if in your context one of the field might be the same for all entries.

Rgds.

Roman

Former Member
0 Kudos

Hi,

To increase the performance first do a sort function and add binary search with the key fields.

Regards,

subhashini