I have a table with users and addresses.
I need to delete all records from the table where multiple different users are associated with the same address.
So, I've prepared the following query:
DELETE FROM MYTABLE A
WHERE EXISTS (
SELECT USER_ID
FROM MYTABLE B
WHERE A.ADDRESS = B.ADDRESS AND B.USER_ID > A.USER_ID
)
I found that sometimes this query hangs while most of the times it's working ok. I suspect that this hanging could be caused by locks.
Can someone pls confirm if this is the case? Generally, is this a valid way to achieve the goal in Oracle?
The oracle version is 12.1.
Thanks,
This is a bit long for a comment.
1) It is impossible to tell what is causing the slowness that you are seeing without you providing additional data.
2) However, when it comes to deleting duplicates, a solution that usually has good performance in Oracle is to use a correlated subquery with ANY
. I would advocate for the following query:
DELETE FROM MYTABLE A
WHERE A.USER_ID > ANY (SELECT USER_ID FROM MYTABLE B WHERE A.ADDRESS = B.ADDRESS)
For this query (and for yours too), consider an index on (USER_ID, ADDRESS )
.
If you want to delete all rows where there are duplicates, I might suggest:
DELETE FROM MYTABLE A
WHERE A.ADDRESS IN (SELECT A2.ADDRESS
FROM MYTABLE A2
GROUP BY A2.ADDRESS
HAVING COUNT(DISTINCT A2.USER_ID) > 1
);
The subquery should be evaluated once. An index on (ADDRESS, USER_ID)
would be helpful for the query.
If you want to keep the minimum user id (which is not what you say but seems reasonable), then I would suggest:
DELETE FROM MYTABLE A
WHERE A.USER_ID > (SELECT MIN(A2.USER_ID)
FROM MYTABLE A2
WHERE A2.ADDRESS = A.ADDRESS
);
For this, the same index is beneficial.
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.