简体   繁体   English

有人可以帮我优化下面的 mysql 存储过程,它使我的服务器崩溃

[英]Can someone please help me out in optimizing the below mysql store procedure which is crashing my server

/* Below will fetch all completed task.*/ /* 下面将获取所有已完成的任务。*/

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;

/* Below will fetch all aborted task.*/ /* 下面将获取所有中止的任务。*/

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;

Some of these indexes may help:其中一些索引可能会有所帮助:

NodeInstanceLog:  INDEX(processInstanceId)
NodeInstanceLog:  INDEX(nodeinstanceid,  nodeName, processInstanceId)
VariableInstanceLog:  INDEX(processInstanceId,  value, variableId)

When adding a composite index, DROP index(es) with the same leading columns.添加复合索引时,删除具有相同前导列的索引。 That is, when you have both INDEX(a) and INDEX(a,b), toss the former.也就是说,当你同时拥有 INDEX(a) 和 INDEX(a,b) 时,扔掉前者。

max(convert(nodeinstanceid,signed)) -- Does this mean that nodeinstanceid is a VARCHAR , but needs to be compare as a number? max(convert(nodeinstanceid,signed)) -- 这是否意味着nodeinstanceidVARCHAR ,但需要作为数字进行比较? I recommend you find a way to store it in an INT or other type of numeric column;我建议您找到一种方法将其存储在INT或其他类型的数字列中; this may allow the query to run much faster.这可能使查询运行得更快。 Also, is that column in NodeInstanceLog ?另外,该列是否在NodeInstanceLog中? Is it the PRIMARY KEY ?它是PRIMARY KEY吗? What indexes (including the PK) exists now for the tables?这些表现在存在哪些索引(包括 PK)?

To help readers understand the query, please use ON for specifying how the tables relate, use WHERE for filtering.为了帮助读者理解查询,请使用ON指定表的关联方式,使用WHERE进行过滤。

Please qualify all columns with their table alias -- I can't tell, for example, which table type and nodeType are in. Hence, the INDEX recommendations above may not be complete.请用它们的表别名限定所有列——例如,我不知道哪个表typenodeType类型在其中。因此,上面的INDEX建议可能不完整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM