简体   繁体   中英

Optimize of hibernate delete operation

The code below works but have performance issues when the SQL deletes more than a million rows. It takes a very long time for 2M tables to be deleted approx 1 hr. We don't have control on the sql condition passed to us. Is there a better way to make this run faster?

Apart from this we don't have DB admin access so stored procedures can't be a solution. Any help is greatly appreciated. Thanks

SAMPLE SQL QUERY

DELETE TBL_A WHERE ID IN (SELECT ID FROM TBL_B WHERE 1=1 AND TBL_B.BD < TO_DATE(ADD_MONTHS(SYSDATE, -240)));

DELETE METHOD

 public void deleteRows(String table, String condition){

    StringBuilder sql = new StringBuilder();
    sql.append("DELETE TABLE=:TABLE WHERE CONDITION=:CONDITION ");

    try{
    transaction = session.beginTransaction();

    int rows = session.createSQLQuery(sql.toString()).setParamater("TABLE",TABLE).setParameter("CONDITION",CONDITION).executeUpdate();

    transaction.commit();
    }
    catch{
    //logic here
    }
    }

There's no faster way for this specific "issue". Unfortunately, it is not a performance issue, it is an application design issue.

... BUT ...

You can:

  1. Create a list-partitioned replica of the table with a single partition and a set of indexes identical to the original table.
  2. Use insert /*+ append */ into the replica select * from the original table with NOT of the CONDITION .
  3. Swap the single replica partition with the original table.
  4. Truncate the single replica partition with drop storage option.

More specific answer would require a much more specific question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM