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 Tabletest1 (Namevarchar(Ten), Sexvarchar(Ten),);Insert intoTest1Values('Test','female');Insert intoTest1Values('test1','female');Insert intoTest1Values('test1','female');Insert intoTest1Values('test11','female');Insert intoTest1Values('test111','female');
CREATE TABLE 2
Create Tabletest2 (Namevarchar(Ten), Sexvarchar(Ten),);Insert intoTest1Values('Test','female');Insert intoTest1Values('test2','female');Insert intoTest1Values('test2','female');Insert intoTest1Values('test22','female');Insert intoTest1Values('test222','female');
-------------------------------------------
Select * fromTest1 minusSelect * fromtest2; Result: NAME SEX---------- ---------- test1 Female test11 female test111 female-----------------------------------------------------------Select * fromTest2 minusSelect * fromtest1; Result: 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:
SELECTorder_id fromMade_order minusSELECTorder_id fromCharge_detail1.14secSELECTa.order_id fromMade_order aWHERE not exists(SELECTorder_id fromCharge_detailWHEREorder_id=a.order_id)18.19secSELECTorder_id fromMade_orderWHEREorder_id not inch(SELECTorder_id fromcharge_detail)20.05Sec
There are other key words:
INTERSECT (intersection)
UNION ALL and set
Oracle Collection of difference values