Case study of table join query not equal to Optimization

Source: Internet
Author: User

The original SQL statement is as follows:

SELECT            o.order_id            FROM orders o            JOIN shipping_orders so ON so.order_id = o.order_id            JOIN shippings s ON s.shipping_no = so.shipping_no            WHERE o.payment_method = 'COD'AND o.status IN(3, 4, 6, 11, 12)AND s.logistic_track_no <> ''AND o.delivery_time < unix_timestamp('2013-09-01')AND o.order_id NOT IN (SELECT order_id FROM cod_order_extinfos);


shipping_ordersTable andshippingsTable Association to retrieve records not equal to null, because s. logistic_track_no <>''If the index is not used, it will scan the entire table. Therefore, the purpose of optimization is to retrieve the index first.shippingsIf the table is empty, the result set is very small and the join operation is much faster.


Rewrite as follows:

SELECT            o.order_id            FROM orders o            JOIN shipping_orders so ON so.order_id = o.order_id            LEFT JOIN (select shipping_no,logistic_track_no from shippings where logistic_track_no = '') s            ON s.shipping_no = so.shipping_no            WHERE o.payment_method = 'COD'AND o.status IN(3, 4, 6, 11, 12)AND s.logistic_track_no is nullAND o.delivery_time < unix_timestamp('2010-12-31')AND  NOT EXISTS ( SELECT order_id FROM cod_order_extinfos coe where coe.order_id=o.order_id);


Execution time before optimization: 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/2059341318-0.jpg "title =" optimization before .jpg "alt =" 161750987.jpg"/>


Execution time after optimization:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/2059346495-1.jpg "title =" after optimization .jpg "alt =" 161828823.jpg"/>

This article is from the "hechun's technical column" blog, please be sure to keep this source http://hcymysql.blog.51cto.com/5223301/1300162

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.