Oracle minus differential Set

Source: Internet
Author: User

Oracle Minuskeyword
Minuskeyword in SQL
There is a minuskeyword 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 there is, then this record is removed, not in the final result. Assuming that the result of the second SQL statement does not exist in the result of the first SQL statement, the data is discarded and its syntax is as follows:
[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

==========================================================
The following is my experiment, it is very obvious 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
)
20.05 sec
  
And a little bit more keyword:
INTERSECT (intersection)
UNION ALL and set

Related Article

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.