优化以下sql语句:SELECT prpss.nowNodeCode, prpss.isEnd, prpss.isValid, prpss.state, prpss.stateDesc, prpss.createTime, prpss.stateTime, frpndd.* FROM flow_run_process prpss LEFT JOIN ( SELECT frpnd.processCode, MAX( CASE frpnd.dataKey WHEN 'orderDes' THEN frpnd.dataValue ELSE NULL END ) AS 'orderDes', MAX( CASE frpnd.dataKey WHEN 'subordinateSystem' THEN frpnd.dataValue ELSE NULL END ) AS 'subordinateSystem', MAX( CASE frpnd.dataKey WHEN 'subordinateSystemNum' THEN frpnd.dataValue ELSE NULL END ) AS 'subordinateSystemNum', MAX( CASE frpnd.dataKey WHEN 'faultClassify' THEN frpnd.dataValue ELSE NULL END ) AS 'faultClassify', MAX( CASE frpnd.dataKey WHEN 'location' THEN frpnd.dataValue ELSE NULL END ) AS 'location', MAX( CASE frpnd.dataKey WHEN 'structureName' THEN frpnd.dataValue ELSE NULL END ) AS 'structureName', MAX( CASE frpnd.dataKey WHEN 'orderOrigin' THEN frpnd.dataValue ELSE NULL END ) AS 'orderOrigin', MAX( CASE frpnd.dataKey WHEN 'submissionTime' THEN frpnd.dataValue ELSE NULL END ) AS 'submissionTime', MAX( CASE frpnd.dataKey WHEN 'whetherCollection' THEN frpnd.dataValue ELSE NULL END ) AS 'whetherCollection', MAX( CASE frpnd.dataKey WHEN 'orderLevel' THEN frpnd.dataValue ELSE NULL END ) AS 'orderLevel', MAX( CASE frpnd.dataKey WHEN 'repairPer' THEN frpnd.dataValue ELSE NULL END ) AS 'repairPer', MAX( CASE frpnd.dataKey WHEN 'repairDep' THEN frpnd.dataValue ELSE NULL END ) AS 'repairDep', MAX( CASE frpnd.dataKey WHEN 'repairPerTel' THEN frpnd.dataValue ELSE NULL END ) AS 'repairPerTel', MAX( CASE frpnd.dataKey WHEN 'eispatchPeople' THEN frpnd.dataValue ELSE NULL END ) AS 'eispatchPeople', MAX( CASE frpnd.dataKey WHEN 'eispatchDep' THEN frpnd.dataValue ELSE NULL END ) AS 'eispatchDep', MAX( CASE frpnd.dataKey WHEN 'mainExecutor' THEN frpnd.dataValue ELSE NULL END ) AS 'mainExecutor', MAX( CASE frpnd.dataKey WHEN 'auxilianName' THEN frpnd.dataValue ELSE NULL END ) AS 'auxilianName', MAX( CASE frpnd.dataKey WHEN 'dealGroupName' THEN frpnd.dataValue ELSE NULL END ) AS 'dealGroupName', MAX( CASE frpnd.dataKey WHEN 'acceptPer' THEN frpnd.dataValue ELSE NULL END ) AS 'acceptPer', MAX( CASE frpnd.dataKey WHEN 'acceptTime' THEN frpnd.dataValue ELSE NULL END ) AS 'acceptTime' FROM flow_run_process_node_data frpnd WHERE EXISTS ( SELECT processNodeId FROM flow_run_process_node frpn WHERE frpn.processNodeId = frpnd.processNodeId AND frpn.isNew = 1 ) GROUP BY frpnd.processCode ) frpndd ON prpss.processCode = frpndd.processCode WHERE prpss.isValid = 1 AND prpss.modelCode = 'P_GZBX_0001' AND prpss.orgId = '40288594670ba1e701670ba33fe40000' AND prpss.siteId = '2c908066675b278a01675e608a4cq11' AND prpss.isEnd = 1
时间: 2023-12-07 09:03:18 浏览: 57
wx111学生宿舍管理系统-nodejs+vue+uniapp-小程序.zip(可运行源码+sql文件+)
这个 SQL 查询语句似乎比较复杂,优化的思路可以从以下几个方面入手:
1. 减少 JOIN 操作的数量:可以尝试将子查询和主查询的关联条件调整为 INNER JOIN,这样可以减少 JOIN 的数量,提高查询的效率。
2. 避免使用函数:在 SELECT 语句中使用函数会降低查询的效率,可以尝试将 CASE WHEN 子句替换为普通的字段查询。
3. 增加索引:可以根据查询条件增加相应的索引,以提高查询的效率。
4. 避免使用 GROUP BY:在子查询中使用 GROUP BY 会降低查询的效率,可以尝试使用其他方式替代,比如使用子查询中的 LIMIT 1 来获取每个 processCode 对应的数据。
针对这个 SQL 查询语句,可以尝试进行以下优化:
```
SELECT
prpss.nowNodeCode,
prpss.isEnd,
prpss.isValid,
prpss.state,
prpss.stateDesc,
prpss.createTime,
prpss.stateTime,
frpndd.*
FROM
flow_run_process prpss
INNER JOIN (
SELECT
frpnd.processCode,
MAX(IF(frpnd.dataKey = 'orderDes', frpnd.dataValue, NULL)) AS orderDes,
MAX(IF(frpnd.dataKey = 'subordinateSystem', frpnd.dataValue, NULL)) AS subordinateSystem,
MAX(IF(frpnd.dataKey = 'subordinateSystemNum', frpnd.dataValue, NULL)) AS subordinateSystemNum,
MAX(IF(frpnd.dataKey = 'faultClassify', frpnd.dataValue, NULL)) AS faultClassify,
MAX(IF(frpnd.dataKey = 'location', frpnd.dataValue, NULL)) AS location,
MAX(IF(frpnd.dataKey = 'structureName', frpnd.dataValue, NULL)) AS structureName,
MAX(IF(frpnd.dataKey = 'orderOrigin', frpnd.dataValue, NULL)) AS orderOrigin,
MAX(IF(frpnd.dataKey = 'submissionTime', frpnd.dataValue, NULL)) AS submissionTime,
MAX(IF(frpnd.dataKey = 'whetherCollection', frpnd.dataValue, NULL)) AS whetherCollection,
MAX(IF(frpnd.dataKey = 'orderLevel', frpnd.dataValue, NULL)) AS orderLevel,
MAX(IF(frpnd.dataKey = 'repairPer', frpnd.dataValue, NULL)) AS repairPer,
MAX(IF(frpnd.dataKey = 'repairDep', frpnd.dataValue, NULL)) AS repairDep,
MAX(IF(frpnd.dataKey = 'repairPerTel', frpnd.dataValue, NULL)) AS repairPerTel,
MAX(IF(frpnd.dataKey = 'eispatchPeople', frpnd.dataValue, NULL)) AS eispatchPeople,
MAX(IF(frpnd.dataKey = 'eispatchDep', frpnd.dataValue, NULL)) AS eispatchDep,
MAX(IF(frpnd.dataKey = 'mainExecutor', frpnd.dataValue, NULL)) AS mainExecutor,
MAX(IF(frpnd.dataKey = 'auxilianName', frpnd.dataValue, NULL)) AS auxilianName,
MAX(IF(frpnd.dataKey = 'dealGroupName', frpnd.dataValue, NULL)) AS dealGroupName,
MAX(IF(frpnd.dataKey = 'acceptPer', frpnd.dataValue, NULL)) AS acceptPer,
MAX(IF(frpnd.dataKey = 'acceptTime', frpnd.dataValue, NULL)) AS acceptTime
FROM
flow_run_process_node_data frpnd
INNER JOIN flow_run_process_node frpn ON frpn.processNodeId = frpnd.processNodeId AND frpn.isNew = 1
WHERE
frpnd.processCode IN (
SELECT processCode FROM flow_run_process WHERE isValid = 1 AND modelCode = 'P_GZBX_0001' AND orgId = '40288594670ba1e701670ba33fe40000' AND siteId = '2c908066675b278a01675e608a4cq11' AND isEnd = 1
)
GROUP BY
frpnd.processCode
) frpndd ON prpss.processCode = frpndd.processCode
WHERE
prpss.isValid = 1
```
这个 SQL 查询语句中,我们将子查询和主查询的关联条件调整为 INNER JOIN。同时,在 SELECT 语句中,我们使用了 IF 函数替代了 CASE WHEN 子句,以提高查询的效率。最后,我们根据查询条件增加了索引,以进一步提高查询的效率。
阅读全文