MySQL multi-table query instance results and analysis-2017.12.14

Source: Internet
Author: User

Today to help run the data found that SQL execution is very slow, well, according to the previous writing habits, and later with the architect to analyze the discussion, improved, the current record analysis is as follows

First, the test table, SQL case

set  @bgDate  =  ' 20171213 ';set  @edDate  =  ' 20171213 ';/* Number ①*/select ta_id,sum ( Tb.amount), SUM (Tb.fee)  from (select order_no,ta_id from table_a  where ta_ status = 100 and ta_channel_merch_no =  ' 201712130001 ' AND SETTLE_DATE  <=  @endDate  AND SETTLE_DATE >=  @begDate  ) Taleft join (select  Order_no,amount,fee from table_b)  tbon ta. ORDER_NO&NBSP;=&NBSP;TB. order_nogroup by ta.ta_id;/* number ②*/select ta. ' ta_id '  as ta_id , sum (TA. ') AMOUNT ')   as sumamount , sum (TB. ' Fee ')    as summerfee from   ' table_a '  TA LEFT JOIN  ' table_b '  tb on ta. ' Order_no ' &NBSP;=&NBSP;TB. ' Order_no '   where  ta. ' settle_date '  >=  @begDate  and ta. ' Settle_date '  <=  @endDate   and ta.' Ta_channel_merch_no ' = ' 201712130001 '    and ta. ' Ta_status ' =100   group  By ta. ' ta_id '  /* index of table below */table_a  index: ta_id, Order_no, settle_datetable_b  index: order_no/* The following is the data volume of the table */table_a:10w+ record table_b:10w+ record

Second, the test results

①90s only the result;

②1.5s is the result;


Third, analysis

1. navicat-Explanation

The TA with the number ① is scanned by the whole table;


2. Independent analysis

Ref. ①SQL

(1) Although the table data volume has been reduced, but because of the newly created temporary table TA, TB, are not indexed, in the left join the slow speed;

(2) At the end of group by, TA.TA_ID also has no index, resulting in a reduction in speed again;


Ref. ②SQL

(1) Although TA, TB direct full table left JOIN, but the connected order_no has been indexed, the speed can be guaranteed;

(2) Some of the fields involved in where and group by are indexed.



MySQL multi-table query instance results and analysis-2017.12.14

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.