Differences between in and exists keywords in a database

Source: Internet
Author: User

  1. Differences between in and exists keywords in a database

    In is the appearance and the inner table as a hash join, and exists is the external loop, each loop and then query the internal table.

    The assertion that exists is more efficient than in is inaccurate.
    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 associated subquery with in is redundant because the IN clause and the function of the related operation in the subquery are the same. Such as:
    Select Staff_name from Staff_member where staff_id in
    (select staff_id from Staff_func where staff_member.staff_id=staff_func.staff_id);

    It is not appropriate to specify a EXISTS clause for a non-associative subquery, because this produces a Cartesian product. Such as:
    Select Staff_name from Staff_member where staff_id
    Exists (select staff_id from Staff_func);

    Not in and not exists


    If the query statement uses not in so the appearance of the full table scan, not used index;
    The index on the table can still be used by the subquery of not extsts.
    So no matter which table is large, using not exists is faster than not.

    Try not to use the NOT in clause. Using the minus clause is faster than the NOT in clause, although you use the minus clause to make two queries:
    Select Staff_name from Staff_member where staff_id on (select staff_id from Staff_member minus select staff_id from staff_ Func where func_id like ' 81% ');

    The difference between in and "="

    Select name from student where name in (' Zhang ', ' Wang ', ' Li ', ' Zhao ');

    And

    Select name from student where Name= ' Zhang ' or name= ' li ' or name= ' Wang ' or name= ' Zhao '

    The result is the same.

Differences between in and exists keywords in a database

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.