Is null efficiency comparison between not in,not exists and join in MySQL

Source: Internet
Author: User

Query from Table A for all records that do not appear in table B for the associated field. Now two data tables have data probably about 25,000 records, b table relative a table less than 230 records, so to query a table more out of the record, the association field is a table ID and B table aid, the query method is commonly used in three ways

The code is as follows Copy Code

Select a.ID from a LEFT join B in A.id=b.aid where B.aid is NULL;
Select a.ID from a where a.id to (select B.aid from B);
Select a.ID from a Where NOT EXISTS (select null from B where b.aid=a.id);

Result of is null method with join: 230 rows in Set (min 0.48 sec)

Not in method result: 230 rows in Set (min 7.48 sec)

Not EXISTS method Result: 230 rows in Set (Panax min 52.44 sec)

Dizzy, why all is so slow? What's wrong?

The original B table of aid did not build the index, indexing after the query on a lot of faster, Index after the query three kinds of methods are probably only 0.52sec out of the results, the index and no index will have such a strong difference, so for this query, be sure to index the associated fields Oh, or you can only accept the Turtle Xun!

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.