请问 大佬们这个段 sql 怎么优化?急

2020-12-31 15:15:53 +08:00
 hemingyang

select distinct RE.* from ( SELECT R.ID_ id, to_number(T.PROC_INST_ID_) processInstanceId, Q.TEXT_ busiType, P.NAME_ definitionName, TO_DATE(TO_CHAR(T.START_TIME_, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') createTime, DECODE(NVL(L.PROC_INST_ID_, 0), 0, TO_DATE(null, 'YYYY-MM-DD'), TO_DATE(TO_CHAR(T.END_TIME_, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS')) endTime, R.NAME_ curName, ( SELECT USERNAME || '(' || REALNAME || ')' FROM t_System_User WHERE id = T.START_USER_ID_ ) AS startUserId, (SELECT ur.phone from T_SYSTEM_USER ur where ur.id=T.START_USER_ID_) as sqrlxdh, ( SELECT u1.DEPARTMENT FROM t_System_User u1 WHERE id = T.START_USER_ID_ ) AS startUserXx, (SELECT TK.ASSIGNEE_ FROM ACT_RU_TASK TK WHERE TK.PROC_INST_ID_ = T.PROC_INST_ID_ AND TK.ASSIGNEE_ IS NOT NULL) assignee, (SELECT UR.REALNAME FROM T_SYSTEM_USER UR WHERE UR.ID = (SELECT TK.ASSIGNEE_ FROM ACT_RU_TASK TK WHERE TK.PROC_INST_ID_ = T.PROC_INST_ID_ AND TK.ASSIGNEE_ IS NOT NULL)) assigneeName, (SELECT ur.phone from T_SYSTEM_USER ur where ur.id=(SELECT TK.ASSIGNEE_ FROM ACT_RU_TASK TK WHERE TK.PROC_INST_ID_ = T.PROC_INST_ID_ AND TK.ASSIGNEE_ IS NOT NULL)) as jsrlxdh FROM ACT_HI_PROCINST T, ACT_HI_VARINST Q, ACT_RE_PROCDEF P, ACT_HI_ACTINST L, (SELECT T1.*, ROW_NUMBER() OVER(PARTITION BY T1.PROC_INST_ID_ ORDER BY T1.START_TIME_ DESC NULLS LAST) RN FROM ACT_HI_TASKINST T1) R, T_SYSTEM_USER U WHERE T.PROC_INST_ID_ = R.PROC_INST_ID_ AND T.PROC_INST_ID_ = Q.PROC_INST_ID_ AND Q.NAME_ = 'busiType' AND P.ID_ = T.PROC_DEF_ID_ AND T.PROC_INST_ID_ = L.PROC_INST_ID_(+) AND T.START_USER_ID_ = U.ID AND L.ACT_TYPE_(+) = 'endEvent' AND R.RN = '1' AND EXISTS (SELECT 1 FROM ACT_HI_TASKINST Q WHERE Q.PROC_INST_ID_ = T.PROC_INST_ID_ AND Q.ASSIGNEE_ = 'abebf5330c71440ab6a4719e535b8129') and not exists ( select 1 from ACT_RU_TASK RES LEFT JOIN ACT_RU_IDENTITYLINK I ON I.TASK_ID_ = RES.ID_ LEFT JOIN ACT_RU_EXECUTION REX ON REX.PROC_INST_ID_ = RES.PROC_INST_ID_ WHERE RES.SUSPENSION_STATE_ = 1 AND REX.PARENT_ID_ IS NULL and res.ID_ = r.ID_ AND ( RES.ASSIGNEE_ = 'abebf5330c71440ab6a4719e535b8129' OR ( RES.ASSIGNEE_ IS NULL AND I.TYPE_ = 'candidate' AND ( I.USER_ID_ = 'abebf5330c71440ab6a4719e535b8129' OR I.GROUP_ID_ IN ( SELECT g.ID_ FROM ACT_ID_GROUP g, ACT_ID_MEMBERSHIP membership WHERE g.ID_ = membership.GROUP_ID_ AND membership.USER_ID_ = 'abebf5330c71440ab6a4719e535b8129' )) ) ) )

 ) RE
	 ORDER BY endTime DESC, createTime DESC
1223 次点击
所在节点    问与答
9 条回复
l00t
2020-12-31 15:53:08 +08:00
你这发给谁看?为什么要优化?要优化成什么样?
npe
2020-12-31 15:59:22 +08:00
truncate table
YYYYMMDDHHSS
2020-12-31 16:34:27 +08:00
根本不是会写 sql 的人写得出来的样子,sqlboy 如是说
hemingyang
2020-12-31 16:36:39 +08:00
@YYYYMMDDHHSS 大佬有方案吗
hemingyang
2020-12-31 16:37:21 +08:00
<h1>

select distinct RE.* from (
SELECT R.ID_ id,
to_number(T.PROC_INST_ID_) processInstanceId,
Q.TEXT_ busiType,
P.NAME_ definitionName,
TO_DATE(TO_CHAR(T.START_TIME_, 'YYYY-MM-DD HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS') createTime,
DECODE(NVL(L.PROC_INST_ID_, 0),
0,
TO_DATE(null, 'YYYY-MM-DD'),
TO_DATE(TO_CHAR(T.END_TIME_, 'YYYY-MM-DD HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS')) endTime,
R.NAME_ curName,
( SELECT USERNAME || '(' || REALNAME || ')' FROM t_System_User WHERE id = T.START_USER_ID_ ) AS startUserId,
(SELECT ur.phone from T_SYSTEM_USER ur where ur.id=T.START_USER_ID_) as sqrlxdh,
( SELECT u1.DEPARTMENT FROM t_System_User u1 WHERE id = T.START_USER_ID_ ) AS startUserXx,
(SELECT TK.ASSIGNEE_
FROM ACT_RU_TASK TK
WHERE TK.PROC_INST_ID_ = T.PROC_INST_ID_
AND TK.ASSIGNEE_ IS NOT NULL) assignee,
(SELECT UR.REALNAME
FROM T_SYSTEM_USER UR
WHERE UR.ID =
(SELECT TK.ASSIGNEE_
FROM ACT_RU_TASK TK
WHERE TK.PROC_INST_ID_ = T.PROC_INST_ID_
AND TK.ASSIGNEE_ IS NOT NULL)) assigneeName,
(SELECT ur.phone from T_SYSTEM_USER ur where ur.id=(SELECT TK.ASSIGNEE_
FROM ACT_RU_TASK TK
WHERE TK.PROC_INST_ID_ = T.PROC_INST_ID_
AND TK.ASSIGNEE_ IS NOT NULL)) as jsrlxdh
FROM ACT_HI_PROCINST T,
ACT_HI_VARINST Q,
ACT_RE_PROCDEF P,
ACT_HI_ACTINST L,
(SELECT T1.*,
ROW_NUMBER() OVER(PARTITION BY T1.PROC_INST_ID_ ORDER BY T1.START_TIME_ DESC NULLS LAST) RN
FROM ACT_HI_TASKINST T1) R,
(SELECT u.id FROM ACT_HI_PROCINST T JOIN T_SYSTEM_USER U ON T.START_USER_ID_ = U.Id ) tr
WHERE T.PROC_INST_ID_ = R.PROC_INST_ID_
AND T.PROC_INST_ID_ = Q.PROC_INST_ID_
AND Q.NAME_ = 'busiType'
AND P.ID_ = T.PROC_DEF_ID_
AND T.PROC_INST_ID_ = L.PROC_INST_ID_(+)
AND L.ACT_TYPE_(+) = 'endEvent'
AND T.START_USER_ID_ = tr.Id
AND R.RN = '1'
AND EXISTS (SELECT 1
FROM ACT_HI_TASKINST Q
WHERE Q.PROC_INST_ID_ = T.PROC_INST_ID_
AND Q.ASSIGNEE_ = 'abebf5330c71440ab6a4719e535b8129')
and not exists (
select 1 from
ACT_RU_TASK RES
LEFT JOIN ACT_RU_IDENTITYLINK I ON I.TASK_ID_ = RES.ID_
LEFT JOIN ACT_RU_EXECUTION REX ON REX.PROC_INST_ID_ = RES.PROC_INST_ID_
WHERE RES.SUSPENSION_STATE_ = 1
AND REX.PARENT_ID_ IS NULL
and res.ID_ = r.ID_
AND (
RES.ASSIGNEE_ = 'abebf5330c71440ab6a4719e535b8129'
OR (
RES.ASSIGNEE_ IS NULL
AND I.TYPE_ = 'candidate'
AND ( I.USER_ID_ = 'abebf5330c71440ab6a4719e535b8129' OR I.GROUP_ID_ IN ( SELECT g.ID_ FROM ACT_ID_GROUP g, ACT_ID_MEMBERSHIP membership WHERE g.ID_ = membership.GROUP_ID_ AND membership.USER_ID_ = 'abebf5330c71440ab6a4719e535b8129' ))
)
)
)



) RE
ORDER BY endTime DESC, createTime DESC

</h1>
totoro52
2020-12-31 16:39:33 +08:00
直接甩一条 SQL 出来让人怎么优化。。。
hemingyang
2020-12-31 16:42:10 +08:00
@totoro52 就是 T_SYSTEM_USER 表数据很大 需要关联 ACT_HI_PROCINST 就会很慢
NotFoundEgg
2020-12-31 16:50:50 +08:00
一看到 activiti 的引擎表就头疼

也不说这是干啥的
jtwor
2020-12-31 17:19:08 +08:00
子查询太多了 可以关联的 不要用 in 用 exists

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/740658

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX