[英]Can someone please help me out in optimizing the below mysql store procedure which is crashing my server
/* 下面將獲取所有已完成的任務。*/
insert ignore into NodeInstanceLog_Dump
select nil.id, nil.connection, nil.log_date, nil.externalId,
nil.nodeContainerId, nil.nodeId ,nil.nodeInstanceId,
coalesce(nil.nodename, nil3.name)nodename, nil.nodeType, nil.processId,
nil.processInstanceId , nil.referenceId, nil.slaCompliance, nil.sla_due_date,
nil.type, nil.workItemId, 0 as activeStatus
from bpm.NodeInstanceLog nil
inner join bpm.VariableInstanceLog vil
ON nil.processInstanceId=vil.processInstanceId
and vil.value='Success'
and vil.variableId in ('oltOrderStatus','orderStatus')
and nodeType='EndNode'
and type=0
left join
(
SELECT distinct nil2.*,nil1.nodeName name
from bpm.NodeInstanceLog nil1 inner join
(
SELECT max(convert(nodeinstanceid,signed))id,processInstanceId
from bpm.NodeInstanceLog
where nodetype='HumanTaskNode'group by processInstanceId
)nil2 ON nil1.nodeinstanceid=nil2.id
and nil1.processInstanceId=nil2.processInstanceId
)nil3 ON nil.processInstanceId=nil3.processInstanceId;
/* 下面將獲取所有中止的任務。*/
insert ignore into NodeInstanceLog_Dump
select nil.id, nil.connection, nil.log_date, nil.externalId,
nil.nodeContainerId, nil.nodeId ,nil.nodeInstanceId,
coalesce(nil.nodename, nil3.name)nodename, nil.nodeType, nil.processId,
nil.processInstanceId , nil.referenceId, nil.slaCompliance, nil.sla_due_date,
nil.type, nil.workItemId, 0 as activeStatus
from bpm.NodeInstanceLog nil
inner join bpm.VariableInstanceLog vil
ON nil.processInstanceId=vil.processInstanceId
and vil.value='Aborted'
and vil.variableId in ('oltOrderStatus','orderStatus')
and nodeType='EndNode'
and type=0
left join
(
SELECT distinct nil2.*,nil1.nodeName name
from bpm.NodeInstanceLog nil1 inner join
(
SELECT max(convert(nodeinstanceid,signed))id,processInstanceId
from bpm.NodeInstanceLog
where nodetype='HumanTaskNode'group by processInstanceId
)nil2 ON nil1.nodeinstanceid=nil2.id
and nil1.processInstanceId=nil2.processInstanceId
)nil3 ON nil.processInstanceId=nil3.processInstanceId;
其中一些索引可能會有所幫助:
NodeInstanceLog: INDEX(processInstanceId)
NodeInstanceLog: INDEX(nodeinstanceid, nodeName, processInstanceId)
VariableInstanceLog: INDEX(processInstanceId, value, variableId)
添加復合索引時,刪除具有相同前導列的索引。 也就是說,當你同時擁有 INDEX(a) 和 INDEX(a,b) 時,扔掉前者。
max(convert(nodeinstanceid,signed))
-- 這是否意味着nodeinstanceid
是VARCHAR
,但需要作為數字進行比較? 我建議您找到一種方法將其存儲在INT
或其他類型的數字列中; 這可能使查詢運行得更快。 另外,該列是否在NodeInstanceLog
中? 它是PRIMARY KEY
嗎? 這些表現在存在哪些索引(包括 PK)?
為了幫助讀者理解查詢,請使用ON
指定表的關聯方式,使用WHERE
進行過濾。
請用它們的表別名限定所有列——例如,我不知道哪個表type
和nodeType
類型在其中。因此,上面的INDEX
建議可能不完整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.