oracle delete query taking too much time

前端 未结 7 676
星月不相逢
星月不相逢 2020-12-05 07:34

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

相关标签:
7条回答
  • 2020-12-05 07:36

    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?

    0 讨论(0)
  • 2020-12-05 07:43

    There is a significant difference between Oracle and mysql :

    Oracle does not create index automatically for foreign keys but mysql does. Then if you have some parent table that you may execute delete command on it then you must create index on foreign keys in child tables otherwise the delete command on parent table will be very very slow if child tables has a lot of rows, because it must surf all records of child table per deletion of any parent records.

    Then be careful when you want to delete from parent table in Oracle database.

    0 讨论(0)
  • 2020-12-05 07:44

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

    0 讨论(0)
  • 2020-12-05 07:47

    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 ?

    0 讨论(0)
  • 2020-12-05 07:52

    So I'll just post my experience. Might be helpful for someone.

    The query

    delete from foo
    where foo_id not in ( 
      select max(foo_id) from foo group by foo_bar_id, foo_qux_id
    );
    

    took 16 sec. deleting 1700 records from 2300 total in table foo.

    I checked all the indexes on foreign keys as directed in other answers. That did not help.

    Solution:

    Changed the query to

    delete from foo
    where foo_id in ( 
      select foo_id from foo
      minus
      select max(foo_id) from foo group by foo_bar_id, foo_qux_id
    );
    

    I've changed not in to in and used minus to achieve correct result.

    Now the query executes in 0.04 sec.

    0 讨论(0)
  • 2020-12-05 07:59

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

    How do I troubleshoot performance problems with an Oracle SQL statement

    0 讨论(0)
提交回复
热议问题