Exists and inner join

Source: Internet
Author: User

Today, I helped developers optimize SQL and found a problem that I didn't care much about before.

             SELECT               bond.store_no             FROM                 bill_order_num_dtl bond              WHERE EXISTS               ( select 1                 from meetorder_staff_dtl b ,                    meetorder_staff a                  where b.operate_area = bond.store_no                   and a.seq_no = b.seq_no                    and  a.user_code = ‘lym‘                  ) ;

The query requires 2 s, bill_order_num_dtl has more than 2 W data records, and meetorder_staff_dtl has about 50 records. After replacing exists with inner join, it only takes 0.02 s, which is 100 times worse, when the hit rate of exists is high, the query speed is faster. For example, the amount of data in the table to be judged is large, and it is not suitable to use a small amount of data in the condition table. Inner join is relatively stable and does not affect performance as the hit rate changes. After it is changed to the following, it only takes 0.02 s

             SELECT  bond.store_no             FROM   bill_order_num_dtl bond                INNER JOIN meetorder_staff_dtl b on b.operate_area = bond.store_no               INNER JOIN  meetorder_staff a on a.seq_no = b.seq_no             WHERE                  a.user_code = ‘lym‘


Exists and inner join

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.