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: 

Delete in loop

Former Member
0 Kudos
1,383

Hi,

I have a delete statement inside a loop like

LOOP AT DATA_PACKAGE.

lv_tabix = sy-tabix.

clear gt_mattab.

READ TABLE gt_mattab

WITH KEY MATERIAL = DATA_PACKAGE-MATERIAL.

IF SY-SUBRC NE 0.

DELETE DATA_PACKAGE index lv_tabix.

ENDIF.

ENLOOP.

Can you please help me in deleting the data packages with out using it in the loop?

Will that increase the performance of the code and how?

1 ACCEPTED SOLUTION

Former Member
0 Kudos
234

i dont see any necessity to change your code. it is fine like it is.

what you would need is some inversion the FOR ALL ENTRIES statement.

Since this statement is known to be aof bad performnce (it forces a fuill table scan) it wont improve your performance doing it outside the loop, but using FOR ALL ENTRIES.

10 REPLIES 10

Former Member
0 Kudos
234

hi ,

loop at itab.

delete itab where field = 'value'.

endloop.

regards,

venkat.

Former Member
0 Kudos
234

Hi Prasad,

No need to use read table statement, just delete it by using condition.

LOOP AT DATA_PACKAGE.

lv_tabix = sy-tabix.

clear gt_mattab.

READ TABLE gt_mattab

WITH KEY MATERIAL = DATA_PACKAGE-MATERIAL.

IF SY-SUBRC NE 0.

DELETE DATA_PACKAGE with table keyMATERIAL = DATA_PACKAGE-MATERIAL .

ENDIF.

ENLOOP.

This will be some what efficient.

Regards,

Subbu

Former Member
0 Kudos
235

i dont see any necessity to change your code. it is fine like it is.

what you would need is some inversion the FOR ALL ENTRIES statement.

Since this statement is known to be aof bad performnce (it forces a fuill table scan) it wont improve your performance doing it outside the loop, but using FOR ALL ENTRIES.

Former Member
0 Kudos
234

HI,

describe table t_tab lines w_lines.

if w_int lt w_lines.

Read table t_tab with index w_int.

if sy-subrc eq 0.

delete t_tab from fs_tab.

endif.

w_int = w_int + 1.

endif.

I hope it will help u.

vinod_vemuru2
Active Contributor
0 Kudos
234

Hi Prasad,

Yes u r right. Delete inside loop is very bad coding practice. But in some of the places we cant avoid this. Few tips to improve the performance of ur code.

SORT gt_mattab BY material.

LOOP AT DATA_PACKAGE.

lv_tabix = sy-tabix.

clear gt_mattab.

READ TABLE gt_mattab

WITH KEY MATERIAL = DATA_PACKAGE-MATERIAL.

IF SY-SUBRC NE 0.

DELETE DATA_PACKAGE index lv_tabix.

ENDIF.

READ TABLE gt_matttab WITH KEY MATERIAL = DATA_PACKAGE-MATERIAL

BINARY SEARCH

TRANSPORTING NO FIELDS.

CHECK sy-subrc IS INITIAL.

DELETE DATA_PACKAGE index lv_tabix.

ENLOOP.

Use Binary search when ever ur table entries are more than 10.

If u want to check the existance of an entry then u can use addition TRANSPORTING NO FIELDS. Even u u can use this if u are using few fields of the read statement like TRANSPORTING f1 f2....

Also avoid using header lines. Use explicit work areas.

Thanks,

Vinod.

Former Member
0 Kudos
234

Every one ,Thanks for your spontaneous replies.

Vinod,

So do you think that there is no other way to delete the data packages with out using it in the loop?

Can you also suggest me if 'do with in the loop' increases the performance or not?

Thanks and Regards,

Pra.

0 Kudos
234

Hi Prasad,

Let me explain exactly what happens if u delete a record from the internal table.

If u defined your internal table like TYPE STANDARD TABLE OF or TYPE SORTED TABLE OF then it will work on INDEX mechanism.

So if have 100 records in ur internal table then each record will be identified with its position i.e INDEX. When u delete a record from the table then it has to recreate the indexes for all other records(Except in case of deletion of last record) after that record. It is the majar disadvantage of delete statement. If u use delete statement inside loop this process has to happen equal to number of deletions.

So there is one more solution for this which will be faster than

DELETE inside the loop. Instead of deleting the records from the original table APPEND the records to another internal table.In this case it has to create INDEX for one time for one append in contrary to DELETE whcih has to recreate INDEXS for all rows.

Check the logic below. Implement and use SQL trace to moniter the performance(transaction ST05).

LOOP AT DATA_PACKAGE.

lv_tabix = sy-tabix.

clear gt_mattab.

READ TABLE gt_mattab

WITH KEY MATERIAL = DATA_PACKAGE-MATERIAL.

CHECK SY-SUBRC EQ 0.

APPEND DATA_PACKAGE TO DATA_PACKAGE1.

ENLOOP.

Also if u dont have else part and no code after ENDIF then always use CHECK statement which is much faster than IF.

Thanks,

Vinod.

Edited by: Vinod Kumar Vemuru on Mar 20, 2008 6:32 PM

Former Member
0 Kudos
234

Hi,

Data: v_tabix LIKE sy-tabix.

LOOP AT DATA_PACKAGE INTO w_DATA_PACKAGE.

v_tabix = sy-tabix.

READ TABLE gt_mattab WITH KEY MATERIAL = w_DATA_PACKAGE-MATERIAL

TRANSPORTING NO FIELDS.

IF sy-subrc <> 0.

DELETE DATA_PACKAGE INDEX v_tabix.

ENDIF.

ENDLOOP.

CLEAR: w_DATA_PACKAGE.

Try this.May be this will help .

Thanks & Regards,

Harsha.

Former Member
0 Kudos
234

Hi prasad,

Use continue statement. Because the deleted line will change the index.

Reward points if helpful.

Regards,

Sankar.

0 Kudos
234

Hi,

If you are going for appending into another internal table then

Sort DATA_PACKAGE by material.

sort gt_matttab by material.

Refresh: DATA_PACKAGE1.

LOOP AT DATA_PACKAGE.

READ TABLE gt_mattab

WITH KEY MATERIAL = DATA_PACKAGE-MATERIAL

binary search.

CHECK SY-SUBRC EQ 0.

APPEND DATA_PACKAGE TO DATA_PACKAGE1.

ENLOOP.

Refresh: DATA_PACKAGE.

DATA_PACKAGE [ ] = DATA_PACKAGE1 [ ].

Hope this will improve your performance issue.

Please reward if useful.

Regards,

Ramesh.

Edited by: Ramesh Jakkula on Mar 20, 2008 2:23 PM

Edited by: Ramesh Jakkula on Mar 20, 2008 2:24 PM