select t.id ,t.parent, t.name ,t.begin ,t.end , t.ACTUAL_START , t.ACTUAL_FINISH, t.TASK_UNIQUE_NO, t.NO, t.SUMMARY, t.DEPENDENCE, t.PRIORITY, t.EXEC_STAT, t.DURATION, t.COMP_PCT,ASSIGNER,POSITION,PRINCIPAL,PRINCIPAL_NAME,ORG_NAME,MGR_LINE,ERJIGUANXIAN,SFSJYS,SFNDJH, t.CRITICAL,t.PROJ_NO,t.SRC_TID,t.ASSIGNER_AUTH,t.POSITION_NAME,t.ASSIGNER_NAME,t.PRIN_ORG,t.ORG,t.SRC_SYS,t.CREATE_USER, t.TASK_NO,tp.id as typ,tp.name as typname,t.SETTLETYPECODE as SETTLETYPECODE,'' as remark,'' as type,t.OATASKID as OATASKID,t.QIQU,t.DESCRIBE, S.DESCRIPTION,ontheway_type,T.SHIFOUXXJDHB,up.update_date optime,submit,t.AUDITOR,t.AUDITOR_NO,t.proj_name,tp.filter_type,t.plan,t.MATTER_SRC,t.Prepose_task,t.shixiangbiaoqian, t.SETTLE_LEVEL,tp.specail_name,t.audit_unit,t.rectify_question_type,t.project_data_processor,t.project_data_processor_name, CASE WHEN t.EXEC_STAT not IN ('30','31','32','33','34','41','55') and ( up.STAT = 'OP' or up.STAT = 'I') then 'relay' when t.EXEC_STAT not IN ('30','31','32','33','34','41','55') and up.STAT = 'U' and up.FINISH ='Y' and up.rel = '1' AND up.RID is not null then 'finish' when t.EXEC_STAT not IN ('30','31','32','33','34','41','55') and up.STAT = 'U' and up.DELAY ='Y' and up.rel = '1' AND up.RID is not null then 'delay' when t.EXEC_STAT not IN ('30','31','32','33','34','41','55') and up.STAT = 'U' and up.TERMINATE ='P' and up.rel = '1' AND up.RID is not null then 'terminate' else null end as approve_type, up.user_id as approver from t_master_task t left join t_task_2_task_typ p on t.id = p.task left join t_task_typ tp on tp.id = p.typ left join t_slave_task s on s.id=t.id left join (select * from (select a.*,row_number() over(partition by task order by update_date desc) rm from t_user_task_upd_stat a) where rm = 1) up on t.id = up.task; 这sql怎么优化
时间: 2023-02-09 15:09:09 浏览: 49
这是一个很复杂的SQL语句,有很多的表连接和子查询,所以要优化的话需要从很多方面入手。
首先,对于表的连接,可以使用索引来提高查询效率,具体可以在被连接的字段上建立索引。
其次,对于子查询,可以考虑将其改写为关联查询的形式,这样可以避免子查询带来的性能开销。
此外,在查询中使用了很多的函数,例如row_number()和over(),这些函数的计算也会消耗查询性能。如果可以的话,可以尽量避免使用这些函数。
总的来说,优化SQL语句需要从多方面入手,还需要根据具体情况进行分析和优化。
相关问题
帮我优化这段sql:SELECT asewo.code_s as code,asewo.order_type_s as orderType,asewo.equipment_name_s as equipmentName,asewo.executor_s as executor, asewo.plan_start_time_T as planStartTime,asewo.plan_finish_time_T as planFinishTime,asewo.tpm_key_s as tpmKey,asewo.shop_s as shop, asewo.line_S as line,asewo.actual_start_time_t as startTime,asewo.actual_finish_time_t as finishTime,asewo.executor_account_s as executorAccounts, asewo.source_type_s as sourceType,asewo.SOURCE_ORDER_S as sourceOrder,asewo.CREATION_TIME as creationTime FROM AT_AS_EM_Work_Order asewo WHERE to_char(asewo.plan_start_time_T,'yyyy_mm-dd hh24:mi') <= to_char(plan_finish_time_T,'yyyy_mm_dd hh24:mi') AND asewo.ORDER_STATUS_S='创建' AND asewo.executor_s='H007063' AND asewo.EQUIPMENT_NAME_S like '%机%' GROUP BY asewo.shop_s, asewo.line_S
可以尝试以下几种优化方式:
1. 建立索引:为了让查询更快速,可以在 `AT_AS_EM_Work_Order` 表中建立索引,包括 `plan_start_time_T`、`plan_finish_time_T`、`ORDER_STATUS_S`、`executor_s` 和 `EQUIPMENT_NAME_S` 字段。
2. 修改查询条件:可以将日期格式化的操作移动到查询条件外部,在查询条件内直接比较日期类型的字段。同时可以将 `GROUP BY` 字段减少到仅保留 `shop_s` 字段,这样可以让查询更快速。
修改后的 SQL 可以是这样的:
```
SELECT asewo.code_s as code,
asewo.order_type_s as orderType,
asewo.equipment_name_s as equipmentName,
asewo.executor_s as executor,
asewo.plan_start_time_T as planStartTime,
asewo.plan_finish_time_T as planFinishTime,
asewo.tpm_key_s as tpmKey,
asewo.shop_s as shop,
asewo.line_S as line,
asewo.actual_start_time_t as startTime,
asewo.actual_finish_time_t as finishTime,
asewo.executor_account_s as executorAccounts,
asewo.source_type_s as sourceType,
asewo.SOURCE_ORDER_S as sourceOrder,
asewo.CREATION_TIME as creationTime
FROM AT_AS_EM_Work_Order asewo
WHERE asewo.plan_start_time_T <= asewo.plan_finish_time_T
AND asewo.ORDER_STATUS_S = '创建'
AND asewo.executor_s = 'H007063'
AND asewo.EQUIPMENT_NAME_S LIKE '%机%'
GROUP BY asewo.shop_s
```
3. 分批查询:如果数据量非常大,可以将查询分批进行,每次查询一部分数据,然后将结果合并。这样可以减少单次查询的数据量,从而提高查询效率。
super().__setattr__
`super().__setattr__()` is a built-in Python function that can be used to call the `__setattr__()` method of the parent class in a subclass. It is commonly used when you want to add some additional functionality to the `__setattr__()` method of the parent class without completely overriding it.
Here's an example:
```python
class MyClass:
def __setattr__(self, name, value):
print(f"Setting {name} to {value}")
super().__setattr__(name, value)
class MySubclass(MyClass):
def __setattr__(self, name, value):
print(f"Adding extra functionality for {name} = {value}")
super().__setattr__(name, value.upper())
```
In this example, `MySubclass` is a subclass of `MyClass`. It overrides the `__setattr__()` method of its parent class to add some extra functionality. However, it still wants to call the `__setattr__()` method of the parent class to handle the actual setting of the attribute.
To do this, it uses `super().__setattr__(name, value.upper())` to call the `__setattr__()` method of the parent class and pass in the modified value. This allows it to add its own functionality while still preserving the functionality of the parent class.
相关推荐














