请高手解决... sql 优化问题。
SELECT formResourceName,t.id, p.workflow_name workflowName,p.workflow_type workflowType,p.process_definition_id processDefinitionId ,i.process_instance_id processInstanceId,i.create_time processInstanceCreateTime,i.orderId,i.author,i.author_name authorName,i.flag processInstanceFlag ,t.task_id taskId,t.handle_time handleTime, (case when handle_result='0' then '不通过' when handle_result='1' then '通过' when handle_result='2' then '创建' when handle_result='3' then '撤回' else '通过' end) as handleResult, handle_comments handleComment ,t.handler ,t.handler_name handlerName from workflow_trans t left join workflow_instance i on t.process_instance_id=i.process_instance_id left join workflow_template p on i.process_definition_id=p.process_definition_id WHERE i.status<>'DELETE' AND t.id IN (SELECT max(id) FROM workflow_trans GROUP BY process_instance_id,formResourceName) -- 这里排除重复数据
用id in 数据很慢。 如何优化?
inner join (select max(id) from ...) as ids on t.id=ids.id
试试看。
我目前处理SQL中in的方法就是 将in换成EXISTS
例如
select c from T1 where id1 In( select id2 from T2 )
修改为
select c from T1 where id1 exists (select 1 from t2 where id1=id2)
可以再join一次嘛。把in 改为join
SQL文本身优化来看,有两点优化:
1.使用 exists 替换 in
2.workflow_instance 表对status创建索引
原因:
in 及 <> 都属于Scan扫描,这样假设数据量较大时,执行效率很低。