优化这个sql select FLOW_COMMON.c_business_id as business_id, (select max(f.end_time) from flow_hi_track f where f.business_id = FLOW_COMMON.c_business_id and f.action_type != 'CLAIM' ) as deal_time from template_flow_common FLOW_COMMON right join template_hollycrm1680160914000 hollycrm1680160914000 on FLOW_COMMON.c_business_id = hollycrm1680160914000.c_business_id where FLOW_COMMON.tenant_id = 'T000' and FLOW_COMMON.valid = 1 and lower(FLOW_COMMON.c_state) != 'draft' and ( ( FLOW_COMMON.c_flow_id in ('FLOW20230330152148238756') and (FLOW_COMMON.c_processing_group in ('1650685461842100265') ) or FLOW_COMMON.c_cur_assignee = '1639203916409208891' ) or FLOW_COMMON.c_creator = '1639203916409208891' or FLOW_COMMON.c_flow_id in ('FLOW20230330152148238756') or FLOW_COMMON.c_business_id in ('1650765461521956870', '1650765461521956870', '1650817085812506712', '1650831863482155082', '1654094763571281921', '1654001405104488514', '1654294361434751036', '1654445890410119245', '1654441313937915946', '1654433554383241232', '1653329109050196051', '1655380751421538376', '1655380751421538376', '1654732194700066894', '1654765190966673497', '1655862681678118938', '1654732194700066894', '1654732194700066894', '1654732194700066894', '1654441313937915946', '1656855682290286598', '1654732194700066894', '1654732194700066894', '1654732194700066894', '1656106327421747261') or (FLOW_COMMON.c_processing_group in ('1650685461842100265')) ) and FLOW_COMMON.c_workorder_type = 'C0018' and FLOW_COMMON.c_business_type = 'C00180008,C001800080001,C0018000800010001' LIMIT 572540,10;
时间: 2024-01-11 18:03:36 浏览: 137
可以尝试对该 SQL 查询进行优化,以下是优化的建议:
1. 使用连接查询代替子查询:
将子查询部分替换为连接查询,这样可以减少查询次数。将以下部分:
```
(select max(f.end_time) from flow_hi_track f where
f.business_id = FLOW_COMMON.c_business_id
and f.action_type != 'CLAIM' ) as deal_time
```
替换为:
```
LEFT JOIN (SELECT business_id, MAX(end_time) AS deal_time FROM flow_hi_track WHERE action_type != 'CLAIM' GROUP BY business_id) f
ON FLOW_COMMON.c_business_id = f.business_id
```
2. 使用 EXISTS 子查询代替 IN 子查询:
将以下部分中的 IN 子查询替换为 EXISTS 子查询,因为 EXISTS 子查询在匹配到第一个结果后可以立即停止查询,而不需要检查所有的匹配项。
```
FLOW_COMMON.c_flow_id in ('FLOW20230330152148238756')
or FLOW_COMMON.c_business_id in ('1650765461521956870', '1650765461521956870', '1650817085812506712', '1650831863482155082', '1654094763571281921', '1654001405104488514', '1654294361434751036', '1654445890410119245', '1654441313937915946', '1654433554383241232', '1653329109050196051', '1655380751421538376', '1655380751421538376', '1654732194700066894', '1654765190966673497', '1655862681678118938', '1654732194700066894', '1654732194700066894', '1654732194700066894', '1654441313937915946', '1656855682290286598', '1654732194700066894', '1654732194700066894', '1654732194700066894', '1656106327421747261')
```
替换为:
```
EXISTS (SELECT 1 FROM template_flow_common t WHERE t.c_flow_id = 'FLOW20230330152148238756' AND FLOW_COMMON.c_business_id = t.c_business_id)
OR EXISTS (SELECT 1 FROM template_flow_common t WHERE FLOW_COMMON.c_business_id = t.c_business_id AND t.c_business_id IN ('1650765461521956870', '1650817085812506712', '1650831863482155082', '1654094763571281921', '1654001405104488514', '1654294361434751036', '1654445890410119245', '1654441313937915946', '1654433554383241232', '1653329109050196051', '1655380751421538376', '1654732194700066894', '1654765190966673497', '1655862681678118938', '1656855682290286598', '1656106327421747261'))
```
3. 使用索引:
确保在 `template_flow_common` 表的 `c_business_id` 列上创建索引,以提高连接查询和子查询的性能。同时,也可以考虑在其他频繁使用的列上创建索引,例如 `c_flow_id`。
请注意,在进行任何优化之前,应先备份数据库以防止意外数据丢失,并在生产环境中进行测试和验证。
阅读全文