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