tags:

views:

2515

answers:

5

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.

+1  A: 

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

Wael Dalloul
yup. correct! This was a huge parent table
Ajay
+6  A: 

Hi Ajay,

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).
Vincent Malgrat
thanks for the reply, but not options 1 and 2 .
Ajay
3rd was infact the solution :) .Now it executes within 2 secs !
Ajay
glad to help =). In fact I should probably have put the third point in the number 1 spot because it is the most likely to be overlooked
Vincent Malgrat
Thanks ;-)
Ajay
The link is broken. Can you please fix it?
Aaron Digulla
@Aaron: I updated the answer, thanks for reporting the broken link
Vincent Malgrat
+1  A: 

How selective is that index? If your table has one million rows and that value hits one hundred and fifty thousand of them then your index is useless. In fact it may be worse than useless if it is actually being used. Remember, a DELETE is a like a SELECT statement: we can tune its access path.

Also, deletes take up a lot of undo tablespace, so you might be suffereing from contention, if the system is experiencing heavy use. In a multi-user system another session might have a lock on the rows(s) you want to delete.

Do you have ON DELETE triggers? Do you have ON DELETE CASCADE foreign key constraints?

Edit: Given all that you say, and especially the column in question being the primary key so you are attempting to delete a single row, if it is taking a long time it is much more likely that some other process or user has a lock on the row. Is anything showing up in V$LOCK?

APC
No On Delete triggers. Table has more than 1 million rows . Column indexed is the primary key. No ON DELETE CASCADE Constraints.I have yet another parent table with > 1 million rows taking just about a second to delete a row !
Ajay
A: 

Does your table holds more number of records ?
Is there some recursive programs(some nested loops etc..) running on the database server ?
Check network problems if database server is on different machines ?

Shyju
No recursive pgms on server side. No n/w problm
Ajay
A: 

If something's slow, and you don't know why, trace it and find out.

http://stackoverflow.com/questions/104066/how-do-i-troubleshoot-performance-problems-with-an-oracle-sql-statement

Dave Costa