In MySQL, and use Exists

Exists Keywords:

exists indicates that there is on the appearance of doing loop cycle , each loop recycling internal query tables (subqueries), then the index because of internal lookup table used (high efficiency in the table, it can be used large table), and the appearance of most need to traverse multiple inevitably (as far as possible with a small table), it is within a large table using EXISTS , may accelerate efficiency; and it is often used in conjunction with sub-queries, for example, the following SQL statement

SELECT * FROM `user` WHERE exists (SELECT * FROM `order` WHERE user.id = order.user_id)

exists for checking whether the subquery returns at least one row of data, the subquery does not actually return any data, but returns the value True or False.

When the sub-query returns true, then the outer query will be queried.

When the sub-query returns false, the outer query will not query or query the absence of any record

In Keywords:

in that the outer and inner tables do hash connection, first check the table, and then in the results table appearance matches (the appearance of high efficiency, large tables available) use the index to look, and how much needs to query in the table, it is inevitable, therefore, large external use in, can accelerate efficiency.

SELECT * FROM `user` WHERE id in (SELECT user_id FROM `order`)

in () statement will only happen once, check it out the order of the table all the user_id field and cached, then, id check whether the user table and order the table user_id rather, if they are equal then the result of adding, until traversed user all records.

If not in, the are within the look full table scan, no index, low efficiency, may be considered not exists, may also be used A left join B on A.id = B.id where B.id is null optimized.

 Summary :

Large data in the table use exists, large external data use in;

reference:

MySQL, and exists in the difference and usage scenarios

https://www.cnblogs.com/xiaoxiong-kankan/p/7928153.html

Guess you like

Origin www.cnblogs.com/liaowenhui/p/12345625.html
Recommended