MySQL Query ~ data that exists in one table and not in another

Source: Internet
Author: User
Tags mysql query one table

A, b two tables, find the ID field, there is a table, but there is no data in table B. A total of 13w data, after the deduplication of about 3W data, b table has 2W data, and B table ID field is indexed.

Method One

Easy to understand with not in, low efficiency ~ Execution Time: 1.395 seconds ~

1 SELECT distinct a.id from  A where a.id not in (select ID from B)
Method Two

Use left Join...on ..., "b.id isnull" indicates a record of NULL in the b.ID field after you have connected-the execution time: 0.739 seconds ~

1 Select a.id from A left join B in a.id=b.id where b.id is null

Graphic

Method Three

Logic is relatively complex, but fastest ~ execution time: 0.570 seconds ~

1  SELECT * from  B 2     where (select COUNT (1) as Num from A where a.id = b.id) = 0

MySQL Query ~ data that exists in one table and not in another

Related Article

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.