Oracle Collection of difference values

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.