SQL in Anything, (all) difference

Source: Internet
Author: User

Differences between EXITS and in:


From the efficiency point of view:

1) SELECT * from T1 where exists (select 1 from T2 where t1.a=t2.a);

When the T1 data volume is small and the T2 data is very large, the query efficiency of T1<<T2 1) is high.

2) SELECT * from T1 where t1.a in (select t2.a from T2);

T1 data volume is very large and T2 data amount of hours, T1>>T2, 2) query efficiency is high.

In short, the general formula: the appearance is big, uses in, the inner table is big, uses the exists.


If the two table size of the query is equal, then the in and exists are not very different.

If one of the two tables is smaller and one is a large table, then the subquery table is large with exists, and the subquery table is small in:

Example: Table A (small table), table B (large table)
1:
SELECT * from A where CC in (select CC from B)
Inefficient, using the index of the CC column on table A;
SELECT * from A where exists (select cc from B where cc=a.cc)
High efficiency, using the index of the CC column on the B table.

The opposite
2:
SELECT * from B where cc on (select cc from A)
High efficiency, using the index of CC column on B table;
SELECT * from B where exists (select cc from A where cc=b.cc)
Inefficient, using the index of the CC column on table A.

The use of exists and in efficiency, usually using exists is higher than in efficiency, because in does not walk the index, but depends on the actual use of:
In the case of large appearance and small inner table, exists is suitable for small appearance and large inner table.


(4) Differences between < any and <all in SQL statements


1, look for age than 15, 16, 22, 21, 17, 18, 19, any one of the student record has the following code:
SELECT *
From student
where Age<any (15,16,22,21,17,18,19)

2, look for age than 15, 16, 22, 21, 17, 18, 19, any one of the large student record has the following code:
SELECT *
From student
where Age>any (15,16,22,21,17,18,19)

Look from above. The meaning of any is not the same as all.
Any is less than the one in the collection. That is, smaller than the smallest.
All is smaller than all. No, it's smaller than the smallest.

What is the difference between the two? Where is it going to be different?

SQL in Anything, (all) difference

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.