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