If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > DELETE Top n rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-06-04, 09:26
soundharya_s soundharya_s is offline
Registered User
 
Join Date: May 2002
Posts: 30
DELETE Top n rows

Version 8.1 FP5 on AIX

Like SELECT * FROM TABLE1 FETCH FIRST 100 ROWS ONLY , is there an option to issue
DELETE FROM TABLE1 FIRST 100 ROWS ONLY

Thanks
Soundh
Reply With Quote
  #2 (permalink)  
Old 07-06-04, 10:06
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I think this should work in v8
Code:
DELETE FROM TABLE1 WHERE SOME_UNIQUE_ID IN (
  SELECT SOME_UNIQUE_ID FROM TABLE1 FETCH FIRST 100 ROWS ONLY
)
Reply With Quote
  #3 (permalink)  
Old 07-06-04, 16:27
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
A nice solution I've seen posted here is to create a view on the table including a row_number. Then delete from the view using the row number.

Code:
create view table1view as 
select *
,        row_number() over() rn 
from   table1;

delete from table1view where rn <= 100;
Reply With Quote
  #4 (permalink)  
Old 07-07-04, 01:17
soundharya_s soundharya_s is offline
Registered User
 
Join Date: May 2002
Posts: 30
Thank You guys .

Damain, How will the row_number solution perform when I have a few million qualifying rows, out of which I select to delete the first 1000 rows . will db2 materialize the view firt before selecting the row numbers 1 to 1000 ? If it does, isn't this very expensive ?

Thanks again
Reply With Quote
  #5 (permalink)  
Old 07-07-04, 02:25
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
DB2 may have to materialize the view because of several reasons. The most common is because a sort is needed (due to an ORDER BY or because of certain types of joins). If DB2 can use an index in lieu of a sort, the materialization can often be avoided. Check the Explain on the query to determine if a sort is required.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On