MySQL Sub-query optimization

Source: Internet
Author: User

Today to query for seven days there are no installed stores information, the first thought of using not in, first installed the UserID check out, and then the ID not in, but this must use sub-query, the data volume is small can also, the data volume is large, the efficiency is particularly low, Because MySQL needs to establish a temporary table for the query results of the inner query statement. The outer query statement then queries the record in the temporary table. After the query is complete, MySQL needs to revoke these temporary tables. As a result, the speed of the subquery is affected somewhat. If the amount of data queried is large, the effect increases accordingly.

First Use:

SELECT * from V9_wba_account where levels = 3 and ID not in (SELECT distinct userid from V9_wba_dev where > 2015091 7)

Then try:

SELECT * from V9_wba_account as user left join V9_wba_dev as Dev on user.id=dev.userid where levels = 3

Because the left JOIN does not satisfy the User.id=dev.userid, it also queries the data in the table, but the fields in the right table in the result are null.

At last:

SELECT * from V9_wba_account as user-left join V9_wba_dev as Dev in user.id=dev.userid where levels = 3 and dev.id is NULL

Finally, it is correct to exclude the right table from being null.

SELECT * from V9_wba_account as user left join V9_wba_dev as Dev on user.id=dev.userid and Dev.days >= 201 50918 where levels = 3 and dev.id is NULL;

On is followed by the query criteria for the right table

MySQL Sub-query optimization

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.