简体   繁体   中英

how can i improve the performance of this oracle sql delete query?

I got to delete some unwanted rows from a table based on the result of a select query from another table

DELETE /*+ parallels(fe) */  FROM fact_x fe
WHERE fe.key NOT IN(
  SELECT DISTINCT di.key
  FROM dim_x di
  JOIN fact_y fa
  ON fa.code         = di.code
  WHERE fa.code_type = 'ABC'
 );

The inner select query returns 77 rows and executes in few milliseconds. but the outer delete query runs forever(for more than 8 hrs). I tried to count how many rows got to be deleted by converting the delete to select count(1) and its around 66.4 million fact_x rows out of total 66.8 million rows. I am not trying to truncate though. I need to retain remaining rows.

Is there any other way to achieve this? will deleting this by running a pl/sql cursor will work better?

Would it not make more sense just to insert the rows you want to keep into another table, then drop the existing table? Even if there are FKs to disable/recreate/etc. it almost certain to be faster.

Could you add a "toBeDeleted" column? The query to set that wouldn't need that "NOT IN" construction. Deleting the marked rows should also be "simple".

Then again, deleting 99,4% of the 67 million rows will take some time.

Try /*+ parallel(fe) */. No "S".

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