Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

oracle delete query taking too much time

Tags:

sql

oracle

I have a query like

DELETE from tablename where colname = value; 

which takes awfully long time to execute. What could be the reason? I have an index on colname.

like image 563
Ajay Avatar asked Aug 25 '09 09:08

Ajay


People also ask

How Speed Up delete in SQL?

So for EVERY select/update/delete, SQL has to do a full scan of the entire table in order to find the records to action. In order to speed up the delete, create a clustered index on the date field.

Why truncate is faster than delete in Oracle?

TRUNCATE is faster than DELETE , as it doesn't scan every record before removing it. TRUNCATE TABLE locks the whole table to remove data from a table; thus, this command also uses less transaction space than DELETE . Unlike DELETE , TRUNCATE does not return the number of rows deleted from the table.


2 Answers

There could be several explanations as to why your query takes a long time:

  1. You could be blocked by another session (most likely). Before you delete you should make sure noone else is locking the rows, eg: issue SELECT NULL FROM tablename WHERE colname=:value FOR UPDATE NOWAIT,
  2. There could be a ON DELETE TRIGGER that does additional work,
  3. Check for UNINDEXED REFERENCE CONSTRAINTS pointing to this table (there is a script from AskTom that will help you determine if such unindexed foreign keys exist).
like image 69
Vincent Malgrat Avatar answered Oct 07 '22 18:10

Vincent Malgrat


it could be that your table is related to multiple tables have a huge row count.

like image 37
Wael Dalloul Avatar answered Oct 07 '22 19:10

Wael Dalloul