Small table drive Large table, concurrently on exists and in

Source: Internet
Author: User

Given the amount of data for two tables, A and b,a and B,

When a is less than B, use Existsselect * from A whereexists(SELECT * from B where a.id=b.id) is the implementation of exists, which is equivalent to the outer loop, each time the internal table is queried?
 for inch A      for inch B         if j.id = = I.id then ....
Conversely, if a is greater than B, then use Inselect * from A where ID in (select ID from B) This is logically similar to
 for inch B      for inch A         if j.id = = I.id then ....

Then MySQL implements in, using hash join (hash join). I guess it's not the same as a logical two-tier loop that works. After my actual test select * from the user u where exists (select 0 from plan p where p.user_id=u.user_id) 0.188 sec/6.234 sec SELECT * From the user U where user_id in (SELECT user_id from plan) 0.015 sec/0.000 sec using in is instantaneous, where plan table has 3300 rows of records, the user table has 3800, the amount of data is precisely So close. So the pseudo-code (two-layer loop can only help to understand), and the actual situation is very big gap. Otherwise they should be very close to each other.
Duration Fetch Rows Sql
Exists 0.17 6.2 924 SELECT * from user u where exists (select 0 from plan p where p.user_id=u.user_id)
Inch 0.01 0.0 924 SELECT * from user U where user_id in (select user_id from plan)
Although the amount of data is small, it can be imagined that if we use exists, it can be very bad, no wonder it is recommended in. (not finished) Hash Join not in

Small table drive Large table, concurrently on exists and in

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.