Introduction to the optimization examples of MYSQL in and EXISTS _mysql

Source: Internet
Author: User
Tags mysql in

Optimization principle: Small table driving large table, that is, small data sets drive large datasets.

############# principle (RBO) #####################

SELECT * from "A where ID" (select ID from b) is
equivalent to: For
select IDs from B for
select * from a WHERE a.id = b.ID

When the dataset of table B must be less than the dataset of table A, use in is better than exists.

SELECT * from a where exists (select 1 from b where b.id = a.id) is
equivalent to for
select * from a for
select * from B W Here b.ID = a.ID

When the data set of table A is less than the data set of Table B, the exists is better than in.

Note: The ID field for table A and table B should be indexed.

For example:

/** execution Time: 0.313s **/
Select Sql_no_cache * from Rocky_member m where EXISTS (select 1 from Rocky_vip_appro a where m.i D = a.user_id and a.passed = 1);
/** execution Time: 0.160s **/
Select Sql_no_cache * from Rocky_member m WHERE m.id in (SELECT ID from Rocky_vip_appro WHERE Pass ed = 1);

Not in and not exists usages are similar.

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.