Select aa.Id, RentOrderNo,RentDeviceNo,RentOrg, changepowers.powercount as changeBatteryCounts from ( select * from tyy_rent_orders where( OrderCreateDtime >= '2023-05-22 14:44:27' ) and ( OrderCreateDtime <= '2023-05-29 14:44:27') and IsDeleted = false ) aa LEFT JOIN ( select orderno,count(ChangeDtime) as powercount from tyy_changebattery_record LEFT JOIN ( select RentOrderNo from tyy_rent_orders where ( OrderCreateDtime >= '2023-05-22 14:44:27' ) and ( OrderCreateDtime <= '2023-05-29 14:44:27') and IsDeleted = false ) bb on bb.RentOrderNo = tyy_changebattery_record.orderno where IsDeleted ='0' group by orderno ) changepowers on aa.RentOrderNo = changepowers.orderno order by RentOrderNo desc ; 这个语句如何优化
时间: 2024-02-17 20:03:41 浏览: 130
Vue中通过<script></script>引入的Vue.js文件
5星 · 资源好评率100%
这个查询中包含了两个子查询和一个LEFT JOIN,这些操作都会对性能产生影响,特别是在大表上的时候,查询时间可能会非常长。以下是几种可能的优化方式:
1. 索引优化
确保涉及到WHERE条件和JOIN条件的列都建立了索引,这样可以加快查询速度。在这个查询中,可以考虑给tyy_rent_orders表中的OrderCreateDtime和IsDeleted列,以及tyy_changebattery_record表中的orderno和IsDeleted列添加索引。
2. 子查询优化
可以将子查询转换为JOIN子句,这样可以避免使用子查询时查询执行计划的优化问题。例如,第一个子查询可以改写为:
```
SELECT * FROM tyy_rent_orders WHERE OrderCreateDtime >= '2023-05-22 14:44:27' AND OrderCreateDtime <= '2023-05-29 14:44:27' AND IsDeleted = false
```
第二个子查询可以改写为:
```
SELECT orderno, COUNT(ChangeDtime) as powercount FROM tyy_changebattery_record cr
INNER JOIN (
SELECT RentOrderNo FROM tyy_rent_orders WHERE OrderCreateDtime >= '2023-05-22 14:44:27' AND OrderCreateDtime <= '2023-05-29 14:44:27' AND IsDeleted = false
) ro ON cr.orderno = ro.RentOrderNo
WHERE cr.IsDeleted = '0'
GROUP BY orderno
```
3. 避免使用SELECT *和LEFT JOIN
在SELECT语句中避免使用SELECT *,尽量只选择需要的列,可以减少查询的数据量。另外,LEFT JOIN可能会导致性能下降,如果可以使用INNER JOIN代替,则应该尽量使用INNER JOIN。
综上所述,可以将原查询改写为以下形式:
```
SELECT aa.Id, aa.RentOrderNo, aa.RentDeviceNo, aa.RentOrg, changepowers.powercount AS changeBatteryCounts
FROM (
SELECT Id, RentOrderNo, RentDeviceNo, RentOrg
FROM tyy_rent_orders
WHERE OrderCreateDtime >= '2023-05-22 14:44:27' AND OrderCreateDtime <= '2023-05-29 14:44:27' AND IsDeleted = false
) aa
LEFT JOIN (
SELECT cr.orderno, COUNT(cr.ChangeDtime) AS powercount
FROM tyy_changebattery_record cr
INNER JOIN (
SELECT RentOrderNo FROM tyy_rent_orders WHERE OrderCreateDtime >= '2023-05-22 14:44:27' AND OrderCreateDtime <= '2023-05-29 14:44:27' AND IsDeleted = false
) ro ON cr.orderno = ro.RentOrderNo
WHERE cr.IsDeleted = '0'
GROUP BY cr.orderno
) changepowers ON aa.RentOrderNo = changepowers.orderno
ORDER BY aa.RentOrderNo DESC
```
这个优化后的查询中,我们避免了使用SELECT *,而是只选择了需要的列。另外,我们将子查询转换为JOIN子句,避免使用LEFT JOIN,同时在WHERE和JOIN条件上都使用了索引。
阅读全文