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

High CPU Usage in a Loop / Read statement

Former Member
0 Likes
1,017

Hi guys,

I have a itab with around 190k lines of data. When I loop it and do a few READ on other itabs, the CPU resources almost hit 100%.

At times I've more complicated series of looping and reading but yet the CPU wouldn't even go near to 100%. Any idea why it causes the CPU to hit nearly 100% usage in this case?

Thanks!


  LOOP AT itab.

    IF itab-matnr NE tmpmatnr.
      mat_new = 'T'.
      tmpmatnr = itab-matnr.

* Stock in MS10 + CS10 Location
      READ TABLE stock WITH KEY matnr = itab-matnr
                                locn = 'MS'.
      IF sy-subrc = 0.
        it_tab-qohqty = stock-quant.
      ELSE.
        it_tab-qohqty = 0.
      ENDIF.

* Stock in VM10 Locn
      READ TABLE stock WITH KEY matnr = itab-matnr
                                locn = 'VM'.
      IF sy-subrc = 0.
        it_tab-vmiqty = stock-quant.
      ELSE.
        it_tab-vmiqty = 0.
      ENDIF.

      IF vm_excld = ''.
        ADD  it_tab-vmiqty TO  it_tab-qohqty.
      ENDIF.

* IQC Stock in RS10 location
      READ TABLE stock WITH KEY matnr = itab-matnr
                                locn = 'RS'.
      IF sy-subrc = 0.
        it_tab-iqcqty = stock-quant.
      ELSE.
        it_tab-iqcqty = 0.
      ENDIF.

* WIP Stock
      it_tab-wipqty = 0.

      IF rdb1 = 'X'.
        READ TABLE stock WITH KEY matnr = itab-matnr
                                  locn = 'WP'.
        IF sy-subrc = 0.
          it_tab-wipqty = stock-quant.
        ENDIF.
      ELSE.
        READ TABLE subconstk WITH KEY matnr = itab-matnr.
        IF sy-subrc = 0.
          it_tab-wipqty = subconstk-lblab.
        ENDIF.
      ENDIF.

      IF rdb6 = 'X'.                "Plant level Stock
        it_tab-wipqty = it_tab-wipqty + it_tab-qohqty.
      ENDIF.
    ELSE.
* Current WIP Qty = Prev Shortage Qty.
      it_tab-wipqty = it_tab-shtqty.
    ENDIF.

* Current Shortage qty = Available WIP Qty - Current requirements
    it_tab-shtqty = it_tab-wipqty - itab-reqqty.

    IF rdb1 = 'X' AND itab-disp_flag <> 'T'.
      CONTINUE.
    ENDIF.

    IF rdb3 = 'X'.
      CHECK it_tab-shtqty < 0.
    ENDIF.

    READ TABLE tmakt WITH KEY matnr = itab-matnr.
    it_tab-matnr = itab-matnr.
    it_tab-maktx = tmakt-maktx.
    it_tab-aufnr = itab-aufnr.
    it_tab-qty_per = itab-qty_per.
    it_tab-plnbez = itab-plnbez.
    it_tab-gstrp = itab-gstrp.
    it_tab-prdqty = itab-bomqty.
    it_tab-reqqty = itab-reqqty.

    APPEND it_tab.

    parts-matnr = it_tab-matnr.
    APPEND parts.
  ENDLOOP.

7 REPLIES 7
Read only

Former Member
0 Likes
880

Hello

Sort table stock by MATNR LOCN and use binary search with read statement.


SORT STOCK BY MATNR LOCN.
....
READ TABLE STOCK WITH KEY MATNR = ITAB-MATNR
                          LOCN = 'MS' 
                          BINARY SEARCH.
* and so on ...

Read only

Former Member
0 Likes
880

Hi,

Inorder to improve the performance during READ statements, sort the table which u want to read and then use the READ statement with the BINARY SEARCH addition.

Kindly note that the internal table needs to be SORTed and READ with the same fields.

Thanks,

Susanth Kumar

Read only

Former Member
0 Likes
880

Hi,

1. Sort the table Stock table with Matnr and location wise.

2. Also sort the Internal table Itab

3. Use Work Area Like

LOOP AT itab into wa.

4. Rewrite your Read statement like

READ TABLE stock INTO Wa_stock WITH KEY matnr = WA-Matnr

Locn = u2018u2019MS

BINARY SEARCH.

5. Donu2019t use Internal table header line.

Read only

Former Member
0 Likes
880

I don't really undestand what you mean with CPU 100% .... is your SAP system on your PC?

About internal tables, please read my blog

Measurements on internal tables: Reads and Loops:

/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables

Check what SAP basis release you use, in releases from 6.10 onward I would

recommend to check sorted or hashed tables.

Otherwise SORT table stock before the loop and use READ BINARY SEARCH, both with the key fields.

same with the other table.

For the LOOP use ASSIGNING into a field-symbol, but use typing for the field-symbol.

This will tremedously speed up your application.

Siegfried

Read only

former_member506713
Participant
0 Likes
880

First Sort the table and use Binary search in READ statement.

It will increase the performance

Read only

Former Member
0 Likes
880

sort all the tables that you are using.

Use binary search in all read statement.

IF itab-matnr NE tmpmatnr.

mat_new = 'T'.

tmpmatnr = itab-matnr.

rather than doing this

delete adjacent duplicates from itab comparing matnr.

Regards,

Lalit Mohan Gupta.

Read only

Former Member
0 Likes
880

Hi,

Here are few tips:

1. Use internal table without header lines and use explicit work area. Better is to use field symbols while looping. Field symbols give better performance as it is directly accessing the record in an internal table.

2. For read statements use binary search. If possible use hashed table.