SELECT t1.supplier_id, t1.ky_count, t1.ky_amount, IFNULL(t2.ky_refund_count,0) as ky_refund_count, IFNULL(t2.ky_refund_amount,0) as ky_refund_amount FROM ( SELECT a.supplier_id, count( DISTINCT c.order_no ) AS ky_count, SUM( IFNULL( c.record_amount, 0 )) / 100 AS ky_amount FROM settle_order a INNER JOIN settle_order_receipt b ON a.order_id = b.order_id INNER JOIN cash_withdrawal_record c ON b.third_trade_no = c.order_no WHERE a.is_del = 0 AND a.order_time >= '2023-05-28 00:00:00' AND a.order_time < '2023-05-29 00:00:00' AND a.order_type in (70,75) AND a.supplier_id IN (78,63,58,57,64,72,71,74,83,77,70,69,67,82,65,87,73,59,66,60,86,85,79,80,84,90) AND b.channel_code = 61 AND c.con_bank_account_no IN ( 247, 325 ) AND c.record_status = 1 AND c.record_time > '2023-05-01 00:00:00' GROUP BY a.supplier_id ) t1 LEFT JOIN ( SELECT a.supplier_id, count( DISTINCT b.order_no ) AS ky_refund_count, SUM( IFNULL( b.record_amount, 0 )) / 100 AS ky_refund_amount FROM settle_order_refund a INNER JOIN cash_withdrawal_record b ON a.third_refund_id = b.order_no WHERE a.is_del = 0 AND a.order_type in (70,75) AND a.apply_time >= '2023-05-28 00:00:00' AND a.apply_time < '2023-05-29 00:00:00' AND a.supplier_id IN (78,63,58,57,64,72,71,74,83,77,70,69,67,82,65,87,73,59,66,60,86,85,79,80,84,90) AND a.channel_code = 61 AND b.con_bank_account_no IN ( 247, 325 ) AND b.record_status = 5 AND b.record_time > '2023-05-01 00:00:00' GROUP BY a.supplier_id ) t2 ON t1.supplier_id = t2.supplier_id order by t1.supplier_id asc
时间: 2024-03-20 14:44:55 浏览: 114
以下是对该 SQL 语句的一些优化建议:
1. 使用 EXISTS 替代 DISTINCT + JOIN
在子查询中,使用了 DISTINCT 关键词去重,然后使用 JOIN 连接表。这样的方式效率较低,可以考虑使用 EXISTS 替代。具体来说,应该将连接条件改为 EXISTS 子查询的 WHERE 子句,如下所示:
```
SELECT
a.supplier_id,
COUNT(DISTINCT c.order_no) AS ky_count,
SUM(IFNULL(c.record_amount, 0)) / 100 AS ky_amount
FROM
settle_order AS a
INNER JOIN settle_order_receipt AS b ON a.order_id = b.order_id
INNER JOIN cash_withdrawal_record AS c ON EXISTS (
SELECT 1
FROM cash_withdrawal_record AS d
WHERE b.third_trade_no = d.order_no
AND d.con_bank_account_no IN (247, 325)
AND d.record_status = 1
AND d.record_time > '2023-05-01 00:00:00'
)
WHERE
a.is_del = 0
AND a.order_time >= '2023-05-28 00:00:00'
AND a.order_time < '2023-05-29 00:00:00'
AND a.order_type IN (70, 75)
AND a.supplier_id IN (78, 63, 58, 57, 64, 72, 71, 74, 83, 77, 70, 69, 67, 82, 65, 87, 73, 59, 66, 60, 86, 85, 79, 80, 84, 90)
AND b.channel_code = 61
GROUP BY
a.supplier_id
```
同样的,对于另一个子查询也可以使用 EXISTS 进行优化。
2. 使用 UNION ALL 代替 LEFT JOIN
在原 SQL 中,使用了 LEFT JOIN 来将两个子查询合并,可以考虑使用 UNION ALL 代替。使用 UNION ALL 的好处是它不会去重,而我们已经在子查询中使用了 GROUP BY 进行了去重,因此可以放心使用 UNION ALL。
```
SELECT
t.supplier_id,
t.ky_count,
t.ky_amount,
t.ky_refund_count,
t.ky_refund_amount
FROM (
SELECT
a.supplier_id,
COUNT(DISTINCT c.order_no) AS ky_count,
SUM(IFNULL(c.record_amount, 0)) / 100 AS ky_amount,
0 AS ky_refund_count,
0 AS ky_refund_amount
FROM
settle_order AS a
INNER JOIN settle_order_receipt AS b ON a.order_id = b.order_id
INNER JOIN cash_withdrawal_record AS c ON EXISTS (
SELECT 1
FROM cash_withdrawal_record AS d
WHERE b.third_trade_no = d.order_no
AND d.con_bank_account_no IN (247, 325)
AND d.record_status = 1
AND d.record_time > '2023-05-01 00:00:00'
)
WHERE
a.is_del = 0
AND a.order_time >= '2023-05-28 00:00:00'
AND a.order_time < '2023-05-29 00:00:00'
AND a.order_type IN (70, 75)
AND a.supplier_id IN (78, 63, 58, 57, 64, 72, 71, 74, 83, 77, 70, 69, 67, 82, 65, 87, 73, 59, 66, 60, 86, 85, 79, 80, 84, 90)
AND b.channel_code = 61
GROUP BY
a.supplier_id
UNION ALL
SELECT
a.supplier_id,
0 AS ky_count,
0 AS ky_amount,
COUNT(DISTINCT b.order_no) AS ky_refund_count,
SUM(IFNULL(b.record_amount, 0)) / 100 AS ky_refund_amount
FROM
settle_order_refund AS a
INNER JOIN cash_withdrawal_record AS b ON EXISTS (
SELECT 1
FROM settle_order_refund AS c
WHERE c.third_refund_id = b.order_no
AND c.is_del = 0
AND c.order_type IN (70, 75)
AND c.apply_time >= '2023-05-28 00:00:00'
AND c.apply_time < '2023-05-29 00:00:00'
AND c.supplier_id IN (78, 63, 58, 57, 64, 72, 71, 74, 83, 77, 70, 69, 67, 82, 65, 87, 73, 59, 66, 60, 86, 85, 79, 80, 84, 90)
AND c.channel_code = 61
)
WHERE
b.con_bank_account_no IN (247, 325)
AND b.record_status = 5
AND b.record_time > '2023-05-01 00:00:00'
GROUP BY
a.supplier_id
) AS t
ORDER BY
t.supplier_id ASC
```
3. 使用索引优化查询
在子查询中,涉及到多个表的连表查询,需要使用到合适的索引才能提高查询效率。具体来说,可以考虑在以下字段上建立索引:
- settle_order.is_del
- settle_order.order_time
- settle_order.order_type
- settle_order.supplier_id
- settle_order_receipt.order_id
- settle_order_receipt.channel_code
- settle_order_receipt.third_trade_no
- cash_withdrawal_record.order_no
- cash_withdrawal_record.con_bank_account_no
- cash_withdrawal_record.record_status
- cash_withdrawal_record.record_time
- settle_order_refund.is_del
- settle_order_refund.order_type
- settle_order_refund.apply_time
- settle_order_refund.supplier_id
- settle_order_refund.channel_code
- cash_withdrawal_record.order_no
- cash_withdrawal_record.con_bank_account_no
- cash_withdrawal_record.record_status
- cash_withdrawal_record.record_time
以上是一些可能的优化建议,具体的优化方案需要根据实际情况进行调整。
阅读全文