views:

252

answers:

4

Hi i have a table that is 5 GB now i was trying to delete like below:

delete  from tablename
where to_char(screatetime,'yyyy-mm-dd') <'2009-06-01' 

But its running long and no response. Meanwhile I tried to check if anybody is blocking with this below:

select l1.sid, ' IS BLOCKING ', l2.sid
from v$lock l1, v$lock l2
where l1.block =1 and l2.request > 0
and l1.id1=l2.id1
and l1.id2=l2.id2

but I didn't find any blocking also.

Can anybody tell me how can I delete this large data without any problem??

Thanks in advance

+1  A: 
Vinodh Ramasubramanian
Thank you. i have a question. it needed export?is it ok if i insert the needed data to a temporary table and truncate and insert back to the original table?? if i go with this way is there any problm?
Joseph
+1  A: 

If there is an index on screatetime your query may not be using it. Change your statement so that your where clause can use the index.

delete from tablename where screatetime < to_date('2009-06-01','yyyy-mm-dd')
Rene
thank you. but there is no index on this
Joseph
if the records you are trying to delete are only a small part of the total an index might be helpfull. If you are deleting a large part of the table the query will possibly do a full table scan and not use the index.Other things that may slow it down:triggers. Are any triggers firing on delete.foreign keys. You may be deleting more than just from this table if cascading foreign keys exist.
Rene
Thanks rene. Il check Appreciate your feedback
Joseph
Indexes may well slow things down as it needs to delete from the index as well as the table.
Gary
it's always best to apply a function in Oracle to invariant data (literals, constants, or bind variables) than to apply a function to variant data, like column values. Calling the to_date() costs some CPU for each exec, and the fastest way to do anything is to avoid doing it at all.
Adam Musch
A: 

It runs MUCH faster when you lock the table first. Also change the where clause, as suggested by Rene.

LOCK TABLE tablename IN EXCLUSIVE MODE;

DELETE FROM tablename
where screatetime < to_date('2009-06-01','yyyy-mm-dd');

EDIT: If the table cannot be locked, because it is constantly accessed, you can choose the salami tactic to delete those rows:

BEGIN
  LOOP
    DELETE FROM tablename
      WHERE screatetime < to_date('2009-06-01','yyyy-mm-dd')
        AND ROWNUM<=10000;
    EXIT WHEN SQL%ROWCOUNT=0;
    COMMIT;
  END LOOP;
END;

Overall, this will be slower, but it wont burst your rollback segment and you can see the progress in another session (i.e. the number of rows in tablename goes down). And if you have to kill it for some reason, rollback won't take forever and you haven't lost all work done so far.

ammoQ
Dear Ammo thanks for your feeds. if i lock the table then nobody can access right?this is a live table..
Joseph
If you lock it, nobody can write (INSERT, UPDATE, DELETE) into that table. If this is a problem, you can choose the salami tactic.
ammoQ
+1  A: 

5GB is not a useful measurement of table size. The total number of rows matters. The number of rows you are going to delete as a proportion of the total matters. The average length of the row matters.

If the proportion of the rows to be deleted is tiny it may be worth your while creating an index on screatetime which you will drop afterwards. This may mean your entire operation takes longer, but crucially, it will reduce the time it takes for you to delete the rows.

On the other hand, if you are deleting a large chunk of rows you might find it better to

  1. Create a copy of the table using 'create table t1_copy as select * from t1 where screatedate >= to_date('2009-06-01','yyyy-mm-dd')`
  2. Swap the tables using the rename command.
  3. Re-apply constraints, indexs to the new T1.

Another thing to bear in mind is that deletions eat more UNDO than other transactions, because they take more information to rollback. So if your records are long and/or numerous then your DBA may need to check the UNDO tablespace (or rollback segs if you're still using them).

Finally, have you done any investigation to see where the time is actually going? DELETE statements are just another query, and they can be tackled using the normal panoply of tuning tricks.

APC
Dear APC thank you verymuch. I think this is im looking for.il work out and comeback.thanks again
Joseph