"if tables are too big, I got the following error"
You are executing a minus
operation, which requires Oracle to sort the two tables. When the tables are large Oracle uses disk to hold the intermediate sort results. This means writing to the Temporary tablespace; if the sort exceeds the capacity of your Temporary tablespace you get ORA-01652
.
First of all, talk to your DBA. Maybe there were a lot of sorts occurring and you might have better luck at a quieter time. Perhaps they can extend the TEMP tablespace. Or they may be able to provision a dedicated Temporary tablespace just for your user (possibly just standing it up for the purposes of this exercise - it depends on what you're doing and why).
"Is there any other way?"
That also depends on what you're doing and why. For a one off exercise you could split the tasks into multiple steps:
- Use the keys to find records in local database not in remote database
- Use the keys to find records in remote database not in local database
- Eliminate the records from the previous two steps in the set of candidate records
If the set of common records remains too large you could create a checksum of each record by concatenating all the columns and hashing them with ora_hash()
or one of Oracle's crypto functions. This will give you two much smaller chunks of data to compare.
If this is a repeating exercise you need to re-think your data management strategy. Maybe one of those tables ought to be a materialized view over the other.
Finally, remember that you have to use MINUS twice: for A minus B
and B minus A
. One of the attractions of the checksum is that it makes this operation easier
select t1.id as local_id
, t2.id as remote_id
, case
when t1.id is null then 'not in local'
when t2.id is null then 'not in remote'
else 'existing changed'
end as state
from your_table as t1
full outer join your_table@remote_db t2
on t1.id = t2.id
where t1.id is null
or t2.id is null
or t1.check_sum != t2.check_sum