delete statement: statistical and graphical comparison
TRANSCRIPT
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 1/17
DELETING rows of theinternal table within theLOOP. Is it a Taboo? A bigNO NO?TOPICS: Data Overflow Index Internal Table
Performance Issue Performance Tuning
Regenerating Index
POSTED BY: SAP YARD AUGUST 6, 2015
All these years, I was told that DELETE statementwithin a LOOP is always prohibited. If you delete anyrows inside a LOOP, it is guaranteed that the QualityReviewer would give you a point and ask you tomodify your code to delete the entries outside theLOOP.
Enter email
Subscribe
RECENT POSTS
Simple SAP Security BreachPlaying Sherlock Holmes todetect CONVT_CODEPAGEruntime error mysteryDELETING rows of theinternal table within theLOOP. Is it a Taboo? A bigNO NO?
SAP YARDYOUR BACKYARD FOR SAP TECHNICAL TIPS AND SOLUTIONS
HOME SEE ALL POSTS ASK YOUR QUESTIONS ABOUT ME CONTACT ME
You and 92 other friends like this
SAP Yard173 likes
Liked
SEARCH …
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 2/17
After going through Naimesh’s post on performance onDELETE, I wondered how would deleting the entries ofthe table on which we are looping would behave in twoscenarios. This is a very practical scenario where weneed to DELETE entries of the LOOPING table based onsome checks (For simplicity, I am checking a field of thesame table, but in real projects, you might check anothertable fields and then delete the driver LOOPing table).
Since most of the developers use STANDARDINTERNAL tables, I am analysing only STANDARDinternal tables (SORTED/HASHED Tables will have towait for some other day)..
The two scenario DELETEs we are talking todayare:1) Parallel Cursor Technique with DELETE w.r.tINDEX within the LOOP.2) Parallel Cursor Technique with marking the rowto be DELETED and then DELETING all at oneshot outside the LOOP.
What does your logical mind say?
Let me be honest. Since my training days, almost adecade ago, we were told that DELETE within LOOP isnever acceptable. So, I always took the second route.Mark by Parallel Cursor technique and DELETEoutside the LOOP.
Later on, as I started understanding the concept ofIndex and Table scan, I started believing that,DELETING within LOOP in parallel cursor with thetarget INDEX known, would be better in the view ofPerformance. Also, I thought that if we mark andDELETE outside loop with WHERE clause, the wholetable needs to be scanned for the item to be deletedand it would affect the performance negatively. So Ibegan to believe, DELETING within LOOP should beacceptable too.
Quick Reference for VistexTechnicalOffshore DevelopmentModel in 10 Steps
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 3/17
But, never before did I try to do the quantitativeanalysis. Finally yesterday, after months ofprocrastination, I thought of wearing the statistician’shat. I spent almost a day playing with the numbers. Itried to increase my sample size as much as I could. Iran the data multiple times in multiple hours of theday (as system can have variable load in differenthours of the day) to get the best average figure for theanalysis.
Here is the code snippet which I used for doing thiscomparison. I used the same set of data for both DELETEstatements so that I could compare the two in neutralvenue.
1) Parallel Cursor Technique with DELETE w.r.tINDEX within the LOOP.
PARAMETERS p_row TYPE i DEFAULT 1000.DATA: i_cdpos TYPE STANDARD TABLE OF cdpos i_cdpos1 TYPE STANDARD TABLE OF cdpos ls_cdpos TYPE cdpos, ls_cdpos1 TYPE cdpos, lv_sta_time TYPE timestampl, lv_end_time TYPE timestampl, lv_diff_w TYPE p DECIMALS 5, lv_no TYPE i.FIELD-SYMBOLS: <lfs_cdpos> TYPE cdpos. * Get entries from Change Log Item tableSELECT * UP TO p_row ROWS FROM cdpos INTO TABLE i_cdpos. * Sorting for BINARY SearchSORT i_cdpos BY objectclas.* Keeping a copy in another tablei_cdpos1[] = i_cdpos[].
* Start timeGET TIME STAMP FIELD lv_sta_time.* Scenario1: Parallel Cursor with DELETE within LOOP (The driver table rows are deleted)LOOP AT i_cdpos INTO ls_cdpos. READ TABLE i_cdpos TRANSPORTING NO FIELDS WITH KEY objectclas = ls BINARY SEARCH. IF sy-subrc EQ 0.
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 4/17
2) Parallel Cursor Technique with marking the rowto be DELETED and then DELETING outside theLOOP.
LOOP AT i_cdpos INTO ls_cdpos1 FROM sy- IF ls_cdpos-objectclas NE ls_cdpos1-objectclas EXIT. ELSEIF ls_cdpos1-chngind EQ 'I'.* DELETING with INDEX DELETE i_cdpos INDEX sy-tabix. ENDIF. ENDLOOP. ENDIF.ENDLOOP.* End timeGET TIME STAMP FIELD lv_end_time.* Time taken for LOOP and DELETElv_diff_w = lv_end_time - lv_sta_time.WRITE: /(25) 'DELETE Inside Loop', lv_diff_w.* Entries left in tablelv_no = lines( i_cdpos ).* Number of entries DELETEDlv_no = p_row - lv_no.WRITE:/(25) 'No of entries deleted', lv_no
CLEAR: lv_no, lv_sta_time, lv_end_time, lv_diff* Start timeGET TIME STAMP FIELD lv_sta_time.* Parallel Cursor with MARKING of rows to be DELETED* Actual DELETE outside of the LOOPLOOP AT i_cdpos1 INTO ls_cdpos. READ TABLE i_cdpos1 TRANSPORTING NO FIELDS WITH KEY objectclas = ls BINARY SEARCH. IF sy-subrc EQ 0. LOOP AT i_cdpos1 ASSIGNING <lfs_cdpos> IF ls_cdpos-objectclas NE <lfs_cdpos> EXIT. ELSEIF <lfs_cdpos>-chngind EQ 'I'.* MARKING the ROW to be DELETED.* Outside the LOOP, objectclas field would be used to identify CLEAR <lfs_cdpos>-objectclas. ENDIF. ENDLOOP. ENDIF.ENDLOOP.* DELETE the MARKED rows at one shotDELETE i_cdpos1 WHERE objectclas IS INITIAL. * End timeGET TIME STAMP FIELD lv_end_time.* Time taken for LOOP and DELETElv_diff_w = lv_end_time - lv_sta_time.
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 5/17
The time taken is for the complete activity set i.e.complete LOOP and DELETE in both scenarios.
Below is the output data which I collected afternumerous runs.
Table 1. Multiple DELETE within LOOP comparison with respectto DELETE outside LOOP
Graph 1. Chart showing tables with rows 13K to 30K has better
WRITE: /(25) 'DELETE Outside Loop', lv_diff_w* Entries left in table (this should be same as above)lv_no = lines( i_cdpos1 ).* Number of entries DELETEDlv_no = p_row - lv_no.WRITE:/(25) 'No of entries deleted', lv_no.
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 6/17
performance while DELETE within LOOP
The first glance of the data shows; for table rows withentries less than 10,000; DELETE outside LOOP worksbetter. Look at column 6 (% change). This column isthe percentage change between DELETE outsideLOOP and DELETE within LOOP. Reference isDELETE outside LOOP.
How to interpret column 6.Negative number means, DELETE outside LOOP isbetter.
If you go through the above table 1 or graph 1, it clearlydepicts that DELETE inside LOOP using INDEX andPARALLEL Cursor is clear winner from 13,000 to 30,000records in internal table. But again after 35,000 rows ininternal table, DELETE outside LOOP is the winner.
So, it made me conclude (wrongly) that:i) For small internal tables, less than 10,000 rows, oneDELETE outside LOOP was good.ii) For medium tables 10K to 30K row entries,DELETING using index within LOOP in PARALLELcursor was good.iii) For large tables 35K and greater, one DELETEstatement outside LOOP has better performance.
Please note, I have mentioned I wrongly concluded.The above three statements are NOT completelycorrect.
I was still not convinced with above result. I waswondering, if there is any other factor which isinfluencing the result other than the number of entries.
Then I decided to run the program in a different client. Iran for the same numbers of records starting from 1000till 40,000.
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 7/17
Table 2. Multiple DELETE within LOOP showing negativeimprovement over DELETE outside LOOP
Table 2 shows the result of running the same programin different client. The output does not tally with Table1 results at all. Here one DELETE statement outsidethe LOOP is clear winner. The age old funda passedon by seniors/mentors holds good here. DELETE insideLOOP is never advisable.
Graph 2. Charts showing % improvement of DELETE within LOOPeven after following Parallel Cursor Technique is NEGATIVE
Look at the graph, the performance of DELETE withinLOOP is below zero (i.e not better than DELETE
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 8/17
outside LOOP)
But, I wanted to know the reason. WHY? Why isthat in the other client, there was differentpattern and in this client DELETE outside LOOP isbetter?
Then, I happened to notice the ‘No of Rows Deleted’column 2. I began to develop my theory and starteddoubting, is this one of the several factors which isinfluencing the overall performance.
Referring to Table 1Out of 1000 enteries in table, 430 were deleted. So bythe time the last entry was deleted, i.e. 430th entry,there were 1000-429 = 571 rows in the table. So, when430th was deleted, indexes were regenerated for 570rows in the internal table.
Which method impacts the performance more?
a) Regenerating the indexes every time the internal tablerow is deleted in LOOP; orb) Regenerating the indexes when it encounters theDELETE statement only once outside the LOOP.
Depending on the cost of the regeneration of indexes,probably the performance depends.
SAP Help document says: When deleting rows inan internal table, costs are incurred for updatingall existing table keys and table indexes.
Food for thought.In Table 2, line with 30,000 records in internal table.Which method would have better performance.a) Deleting 1931 rows in LOOP and regenerating theindex in every loop for maximum of 29999 rows (firstdelete) and minimum of 28069 records (last delete)?
When the first row is deleted, there are 29,999 rows in
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 9/17
internal table to be re-indexed. When the last row i.e.1931th is deleted, there are 28,069 rows in internaltable to be re-indexed. In total, the internal table isre-indexed 1931 times in LOOP.
b) Deleting 1931 rows at one shot outside the LOOPand re-indexing?
DELETING outside LOOP seems superior even thoughin the PARALLEL cursor we are DELETING usingINDEX. But, may be the cost of updating the existingtable keys and table indexes downgrades theadvantage of PARALLEL CURSOR DELETE usingINDEX.
I am not sure, how correct the above analysis is. But,one thing is clear, internal table re-indexing costshould be considered before deciding which route togo.
We can safely conclude that performance of thetwo scenarios is dependent on:i) The number of entries in the tableii) The number of entries to be deleted in the tableiii) The total number of entries left after delete statementiv) Total number of times the index of the internal tableneed to be regenerated
And for now, DELETE outside LOOP seems tohave an Upper Hand.. (I am stating something whichmight attract lots of criticism/debate from ParallelCursor Technique lovers).
I would like to request experts and mentors to throwmore light on this topic. If you have betterexplanation to this topic, please let us know. Alsoplease point us to the right source/link which areauthentic and where we can get more answers and
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 10/17
20 COMMENTS
ON "DELETING ROWS OF THE INTERNAL TABLE WITHIN THE LOOP. IS ITA TABOO? A BIG NO NO?"
better understanding.
Please share your experience and thoughts so that wemight learn from your experience.
If you want to get updates about our new posts, pleasesubscribe.
If you liked it, please share it. Thank you very much foryour time!!
Image source: kognitio.com.
Previous post Next post
Very Informative even though we use iteveryday we use it because we were told so /because everyone uses it. Please keep up thegood work.
Sandip Barnwal | August 6, 2015 at6:26 am | Reply (Edit)
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 11/17
Thanks Sandip..Sometimes when we try to question, weare startled by the results we get..
Raju..
Good work…Your effort is appreciated!!
Thanks Saket..
Regards,Raju.
excellent analysis dude……u put a lot of hardwork..
SAP Yard | August 6, 2015 at1:47 pm | Reply (Edit)
Saket Suman | August 6, 2015 at 6:46am | Reply (Edit)
SAP Yard | August 6, 2015 at1:47 pm | Reply (Edit)
krishnamraju | August 6, 2015 at 7:54am | Reply (Edit)
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 12/17
Thanks Krishnam..
Regards,Raju
Nice analysis.
Hi Angshuman – Glad youliked it.
Raju..
Hi Raju da,
Appreciate your efforts to analyse this !!!..Iam also of the same opinion to use deleteoutside the loop until data is manageable. Butmany times with huge data it would be betterto chose delete inside loop for a block of data.
SAP Yard | August 6, 2015 at1:49 pm | Reply (Edit)
Angshuman Pal | August 6, 2015 at8:32 am | Reply (Edit)
SAP Yard | August 6, 2015 at1:50 pm | Reply (Edit)
Rajeev Kumar | August 6, 2015 at 8:56am | Reply (Edit)
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 13/17
Parallel cursor is always a plus. So I vote for“Parallel Cursor Technique to mark the row tobe DELETED and then DELETING outside theLOOP”.
Also in your analysis you can put one moregraph by keeping the number of entries samebut changing the number of rows to bedeleted.
Best Regards,Rajeev
Hi Rajeev – True. Both themethods can be handy in differentscenarios. If we are sure of the pattern ofrows to be deleted in advance, thendefinitely we can plan better on whichmethod to use.Thank you for your suggestion. Will tryto incorporate.
Regards,Raju.
very good analysis
SAP Yard | August 6, 2015 at1:52 pm | Reply (Edit)
Sanjay | August 6, 2015 at 11:55 am |Reply (Edit)
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 14/17
Thank you Sanjay..
Regards,Raju.
Thank You so much…!!!
You are welcome William..
Regards,Raju.
This is good and informatic analysis.
SAP Yard | August 6, 2015 at1:53 pm | Reply (Edit)
Mutero william | August 6, 2015 at 2:17pm | Reply (Edit)
SAP Yard | August 6, 2015 at2:23 pm | Reply (Edit)
Sumanta Hati | August 7, 2015 at 3:55am | Reply (Edit)
SAP Yard | August 7, 2015 at4:27 am | Reply (Edit)
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 15/17
Thanks Sumanta.. Glad you liked it.
Regards,Raju.
Superb analysis bosss!!! Hats off!! It is reallyvery interesting statistics and made the topicsvery interesting to all of us. Really thanks forall your effort.
Dear Prasanta – Thankyou so much for going through the page.I am happy that you found this postinteresting.
Regards,Raju.
Good effort. Informative and very interestingcases.This is another proof what is good for
Prasanta Maiti | August 7, 2015 at 7:48pm | Reply (Edit)
SAP Yard | August 7, 2015 at7:55 pm | Reply (Edit)
Eric | August 8, 2015 at 6:15 pm | Reply(Edit)
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 16/17
handling high volume might not be good forlow volume.In a SAP environment, I am not sure of whowould really care about a few secondsperformance difference over inside loop oroutside loop deletion. When volume is low,either solution is ok. It is the volume thatmakes difference.. So it is generally a safe-approach to use mass operation instead ofindividual operation in a loop.
Dear Eric – Reallyhonoured that an expert like you writingyour thoughts here. Thank you so much.I follow your posts diligently. Thank youso much.
True, only for high volume we need tobe extra careful.
Regards,Raju
Leave a commentLogged in as SAP Yard. Log out?
Comment
SAP Yard | August 8, 2015 at6:33 pm | Reply (Edit)
8/18/2015 DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? | SAP Yard
http://www.sapyard.com/deletingrowsoftheinternaltablewithintheloopisitatabooabignono/ 17/17
Post Comment
COPYRIGHT 2015 | SAPYARD BY WWW.SAPYARD.COMALL PRODUCT NAMES ARE TRADEMARKS OF THEIR RESPECTIVE COMPANIES. SAPYARD.COM IS NOT AFFILIATED TO SAP AG.