For my small dataset, Oracle provides almost all of these queries with the exact same plan that uses primary key indexes without touching the table. The exception is the MINUS version, which manages to make fewer agreed failures, despite the higher cost of the plan.
--Create Sample Data. drop table tableA; drop table tableB; create table tableA as ( select rownum-1 ID, chr(rownum-1+70) bb, chr(rownum-1+100) cc from dual connect by rownum<=4 ); create table tableB as ( select rownum ID, chr(rownum+70) data1, chr(rownum+100) cc from dual UNION ALL select rownum+2 ID, chr(rownum+70) data1, chr(rownum+100) cc from dual connect by rownum<=3 ); alter table tableA Add Primary Key (ID); alter table tableB Add Primary Key (ID); --View Tables. select * from tableA; select * from tableB; --Find all rows in tableA that don't have a corresponding row in tableB. --Method 1. SELECT id FROM tableA WHERE id NOT IN (SELECT id FROM tableB) ORDER BY id DESC; --Method 2. SELECT tableA.id FROM tableA LEFT JOIN tableB ON (tableA.id = tableB.id) WHERE tableB.id IS NULL ORDER BY tableA.id DESC; --Method 3. SELECT id FROM tableA a WHERE NOT EXISTS (SELECT 1 FROM tableB b WHERE b.id = a.id) ORDER BY id DESC; --Method 4. SELECT id FROM tableA MINUS SELECT id FROM tableB ORDER BY id DESC;
Leigh Riffel Dec 10 2018-10-10 14:36
source share