SELECT a.supplier_id, count(DISTINCT a.order_id) AS totalcount, sum(IFNULL(b.receipt_amount, 0)) / 100 as total_amount, sum(IFNULL(b.sell_commission, 0)) / 100 as total_sellcommAmount, count(DISTINCT IF(b.channel_code = 61, a.order_id, NULL)) AS channelcode61_count, sum( IF(b.channel_code = 61, IFNULL(b.receipt_amount, 0), 0) ) / 100 as channelcode61_amount, sum( IF(b.channel_code = 61, IFNULL(b.sell_commission, 0), 0) ) / 100 as channelcode61_sellcommAmount FROM settle_order a INNER JOIN settle_order_receipt b ON a.order_id = b.order_id WHERE a.is_del = 0 AND a.order_type IN (70, 75) AND a.order_time >= '2023-05-28 00:00:00' AND a.order_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 ) GROUP BY a.supplier_id order by a.supplier_id asc
时间: 2024-03-20 22:43:23 浏览: 102
感谢提供SQL语句。根据该SQL语句,可能可以通过以下几个方面进行优化:
1. 调整JOIN关系
可以尝试调整JOIN关系,优先选择小表作为驱动表(即在ON子句中使用小表的主键进行连接)。此外,如果可能,也可以考虑使用子查询或LEFT JOIN代替INNER JOIN。
2. 减少子查询
可以尝试使用JOIN或LEFT JOIN来代替子查询,以避免多次查询数据库。
3. 避免使用函数
在SELECT语句中使用函数会增加查询的开销,因此应尽量避免使用函数。如果可以,可以考虑在代码中使用变量进行计算。
4. 使用索引
可以根据查询条件设置索引,以加快查询速度。例如,在该SQL中可以设置a.is_del、a.order_type、a.order_time和a.supplier_id的索引。
综上,以下是一个可能的优化SQL示例:
```
SELECT
a.supplier_id,
COUNT(DISTINCT a.order_id) AS totalcount,
SUM(IFNULL(b.receipt_amount, 0)) / 100 AS total_amount,
SUM(IFNULL(b.sell_commission, 0)) / 100 AS total_sellcommAmount,
COUNT(DISTINCT IF(b.channel_code = 61, a.order_id, NULL)) AS channelcode61_count,
SUM(IF(b.channel_code = 61, IFNULL(b.receipt_amount, 0), 0)) / 100 AS channelcode61_amount,
SUM(IF(b.channel_code = 61, IFNULL(b.sell_commission, 0), 0)) / 100 AS channelcode61_sellcommAmount
FROM
(SELECT * FROM settle_order WHERE is_del = 0 AND order_type IN (70, 75) AND order_time >= '2023-05-28 00:00:00' AND order_time < '2023-05-29 00:00:00' AND 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)) a
LEFT JOIN (SELECT * FROM settle_order_receipt) b ON a.order_id = b.order_id
GROUP BY
a.supplier_id
ORDER BY
a.supplier_id ASC
```
需要注意的是,优化SQL语句需要结合具体的数据库环境和数据情况进行,以上只是一个可能的示例,具体优化效果需要测试验证。
阅读全文