Search code examples
oracle-databasecycle

Why Oracle stuck after few deletes?


I have an application that do like:

delete from tableA where columnA='somevalue' and rownum<1000

In cycle like:

 while(deletedRows>0) {
    begin tran
    deletedRows = session ... "delete from tableA where columnA='somevalue' 
    and rownum<1000"....
    commit tran

}

It runs few times (each deleting takes near 20 seconds) and after hungs for long time Why? Does it possible to fix? Thanks.


The reason why the deletes are run in a loop rather than as a single SQL statement is lack of rollback space. See this question for more info.


Solution

  • Every time the query scans the table from the beginning. So, it scans the zones where there are no rows to delete(columnA='somevalue'). They are more and more far away from the first block of the table.

    If the table is big and there would be no columnA='somevalue' the query will take the time to verify all the row for your condition.

    What you can do is to make an index on columnA. In this case the engine will know faster where are the rows with that condition(search on index is exponential time faster).

    Another possibility, if you are in a concurent system, is that someone updated a row that you ar trying to delete, but doesn't commited the transaction, so the row is locked.