优化这条sql: SELECT /*aggregation_path_type=single_agg*/ `company_code`, `company_name`, `sale_id`, `sale_name`, round(sum(`checkin_uv`), 2) AS peopleSum, round(sum(`cash_pay`), 2) AS cashPay, round(sum(`account_pay`), 2) AS accountPay, round(sum(`salecard_pay`), 2) AS saleCardPay, round(sum(`online_pay`), 2) AS onlinePay, round(sum(`pay_amount_final`), 2) AS payAmount, round(sum(`total_original_price`), 2) AS originalPrice, CASE WHEN sum(`total_original_price`) = 0 THEN 0 ELSE Round( sum(`pay_amount_final`) / sum(`total_original_price`) * 100, 2 ) END AS discount, CASE WHEN sum(`checkin_uv`) = 0 THEN 0 ELSE Round(sum(`pay_amount_final`) / sum(`checkin_uv`), 2) END AS priceAvg, round(SUM(`settlement_pice_exclude_add`), 2) AS saleExcludeAdd, round(SUM(`original_pice_exclude_add`), 2) AS originalExcludeAdd, CASE WHEN sum(`total_original_price`) = 0 THEN 0 ELSE Round( ( SUM(`settlement_pice_exclude_add`) / sum(`total_original_price`) * 100 ), 2 ) END AS excludeAddDis, CASE WHEN sum(`checkin_uv`) = 0 THEN 0 ELSE Round( SUM(`settlement_pice_exclude_add`) / sum(`checkin_uv`), 2 ) END AS excludeAddPriceAvg FROM `salesman_confim_revenue_v2` WHERE sale_region_code = '8' GROUP BY `company_code`, `sale_id` ORDER BY `company_code` DESC
时间: 2024-01-15 22:02:48 浏览: 117
针对这条SQL语句,可以进行以下优化:
1. 优化索引:根据该SQL语句的查询条件和分组条件,可以建立sale_region_code、company_code、sale_id三个字段的联合索引,可以加快查询速度。
2. 避免使用函数:在SQL语句中使用函数会影响查询效率,可以尝试避免使用函数,比如将round函数转移到应用程序中进行处理。
3. 减少重复计算:该SQL语句中有一些重复计算的部分,比如sum(`total_original_price`)、sum(`checkin_uv`)可以在with语句中先计算出来,避免重复计算。
4. 分批查询:如果数据量较大,可以采用分批查询的方式,将数据分成多个批次进行查询,避免一次查询数据量过大。
优化后的SQL语句如下所示:
```
WITH temp AS (
SELECT
`company_code`,
`sale_id`,
SUM(`checkin_uv`) AS checkin_uv_sum,
SUM(`cash_pay`) AS cash_pay_sum,
SUM(`account_pay`) AS account_pay_sum,
SUM(`salecard_pay`) AS salecard_pay_sum,
SUM(`online_pay`) AS online_pay_sum,
SUM(`pay_amount_final`) AS pay_amount_final_sum,
SUM(`total_original_price`) AS total_original_price_sum,
SUM(`settlement_pice_exclude_add`) AS settlement_pice_exclude_add_sum,
SUM(`original_pice_exclude_add`) AS original_pice_exclude_add_sum
FROM
`salesman_confim_revenue_v2`
WHERE
sale_region_code = '8'
GROUP BY
`company_code`,
`sale_id`
), result AS (
SELECT
`company_code`,
`company_name`,
`sale_id`,
`sale_name`,
ROUND(temp.checkin_uv_sum, 2) AS peopleSum,
ROUND(temp.cash_pay_sum, 2) AS cashPay,
ROUND(temp.account_pay_sum, 2) AS accountPay,
ROUND(temp.salecard_pay_sum, 2) AS saleCardPay,
ROUND(temp.online_pay_sum, 2) AS onlinePay,
ROUND(temp.pay_amount_final_sum, 2) AS payAmount,
ROUND(temp.total_original_price_sum, 2) AS originalPrice,
CASE
WHEN temp.total_original_price_sum = 0 THEN 0
ELSE ROUND(temp.pay_amount_final_sum / temp.total_original_price_sum * 100, 2)
END AS discount,
CASE
WHEN temp.checkin_uv_sum = 0 THEN 0
ELSE ROUND(temp.pay_amount_final_sum / temp.checkin_uv_sum, 2)
END AS priceAvg,
ROUND(temp.settlement_pice_exclude_add_sum, 2) AS saleExcludeAdd,
ROUND(temp.original_pice_exclude_add_sum, 2) AS originalExcludeAdd,
CASE
WHEN temp.total_original_price_sum = 0 THEN 0
ELSE ROUND((temp.settlement_pice_exclude_add_sum / temp.total_original_price_sum * 100), 2)
END AS excludeAddDis,
CASE
WHEN temp.checkin_uv_sum = 0 THEN 0
ELSE ROUND(temp.settlement_pice_exclude_add_sum / temp.checkin_uv_sum, 2)
END AS excludeAddPriceAvg
FROM
temp
LEFT JOIN `company_info` ON temp.company_code = `company_info`.company_code
LEFT JOIN `salesman_info` ON temp.sale_id = `salesman_info`.sale_id
ORDER BY
`company_code` DESC
)
SELECT * FROM result;
```
以上SQL语句中,使用了with语句将重复计算的部分先计算出来,然后再进行查询。同时,使用了联合索引和避免函数的优化方式,可以加快查询速度。
阅读全文