Oracle minus Keywords
The minus keyword in sql
There is a minus keyword in SQL that uses two SQL statements to find out the results of the first SQL statement, and then see if the results are in the results of the second SQL statement. If so, the record is removed, not in the final result. If the result of the second SQL statement does not exist in the result of the first SQL statement, the data is discarded with the following syntax:
[SQL Segment 1]
Minus
[SQL Segment 2]
--------------------------------------------
CREATE TABLE 1
CREATE TABLE Test1
(
Name varchar (10),
Sex varchar (10),
);
INSERT into test1 values (' Test ', ' female ');
INSERT into test1 values (' test1 ', ' female ');
INSERT into test1 values (' test1 ', ' female ');
INSERT into test1 values (' test11 ', ' female ');
INSERT into test1 values (' test111 ', ' female ');
CREATE TABLE 2
CREATE TABLE Test2
(
Name varchar (10),
Sex varchar (10),
);
INSERT into test1 values (' Test ', ' female ');
INSERT into test1 values (' test2 ', ' female ');
INSERT into test1 values (' test2 ', ' female ');
INSERT into test1 values (' test22 ', ' female ');
INSERT into test1 values (' test222 ', ' female ');
-------------------------------------------
SELECT * from Test1 minus select * from Test2;
Results:
NAME SEX
---------- ----------
Test1 Female
test11 Female
test111 Female
-----------------------------------------------------------
SELECT * from Test2 minus select * from Test1;
Results:
NAME SEX
---------- ----------
Test2 Female
test22 Female
test222 Female
Conclusion: Minus returns always the data from the left table, which returns the difference set. Note: Minus has a shaving effect
==========================================================
Here is my experiment, it is clear that minus efficiency, made_order a total of 230,000 records, charge_detail a total of 170,000 records
Performance comparison:
SELECT order_id from Made_order
Minus
SELECT order_id from Charge_detail
1.14 sec
SELECT a.order_id from Made_order a
WHERE NOT EXISTS (
SELECT order_id
From Charge_detail
WHERE order_id = a.order_id
)
18.19 sec
SELECT order_id from Made_order
WHERE order_id Not IN (
SELECT order_id
From Charge_detail
)
Oracle minus Keywords