oracle delete query taking too much time

前端 未结 7 730
星月不相逢
星月不相逢 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: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.

提交回复
热议问题