使用行号优化MYSQL查询

时间:2017-10-24 11:16:49

标签: mysql

SELECT   o.* FROM   (    SELECT
     @rownum := @rownum + 1 RN,
     i.*    FROM<br>
     (
       SELECT
         @rownum:=0
     )
     r,<br>
     (
       SELECT DISTINCT<br>
         CG_MGR_USR.MANAGER_NAME,<br>
         CG_MGR_USR.PARAM_NAME,<br>
         USR_TR_DATA.USER_ID,<br>
         CG_MGRS.FIRST_NAME,<br>
         CG_MGRS.LAST_NAME,<br>
         CC_GENOBJ.OBJNAME,<br>
         USR_TR_DATA.RISK_ID,<br>
         USR_TR_DATA.VERSION_ID,<br>
         T_RISK.RISK_DESCR,<br>
         CC_RISKT.RISK_ID CC_RISK,<br>
         CC_RISKT.DESCN,<br>
         USR_TR_DATA.SYSTEM_ID,<br>
         T_STATUS.STATUS,<br>
         USR_TR_DATA.COUNTS,<br>
         SYSTEMS.SYSTEM_NAME<br>
       FROM
         TABLE_USR_TR_DATA USR_TR_DATA<br>
       LEFT OUTER JOIN TABLE_CC_GENOBJ CC_GENOBJ<br>
       ON
         CC_GENOBJ.GENOBJTP     = 1
       AND CC_GENOBJ.SYSTEM_ID IN<br>
         (
           SELECT
             sys_id
           FROM
             TABLE_CS_SYSTEM
           WHERE
             cs_sys_id   =USR_TR_DATA.system_id
           AND is_primary='Y'
         )<br>
       AND CC_GENOBJ.GENOBJID = USR_TR_DATA.USER_ID,<br>
         TABLE_RISK T_RISK<br>
       LEFT OUTER JOIN TABLE_CC_RISKT CC_RISKT
       ON<br>
         CC_RISKT.LANG        = 'EN'
       AND T_RISK.CC_RISK_ID = CC_RISKT.RISK_ID,<br>
         TABLE_CG_MGR_USR CG_MGR_USR,<br>
         TABLE_CG_MGRS CG_MGRS,<br>
         TABLE_STATUS T_STATUS,<br>
         TABLE_CS_SYSTEM CS_SYSTEMS,<br>
         TABLE_SYSTEMS SYSTEMS<br>
       WHERE
         USR_TR_DATA.RISK_ID        = T_RISK.RISK_ID<br>
       AND USR_TR_DATA.VERSION_ID   = T_RISK.VERSION_ID<br>
       AND CG_MGR_USR.USER_NAME     = USR_TR_DATA.USER_ID<br>
       AND CG_MGR_USR.SYSTEM_ID     = CS_SYSTEMS.SYS_ID<br>
       AND CG_MGRS.MANAGER_NAME     = CG_MGR_USR.MANAGER_NAME<br>
       AND CG_MGRS.PARAM_NAME       =CG_MGR_USR.PARAM_NAME<br>
       AND CG_MGRS.SYSTEM_ID        = CG_MGR_USR.SYSTEM_ID<br>
       AND CS_SYSTEMS.CS_SYS_ID     = USR_TR_DATA.SYSTEM_ID<br>
       AND CS_SYSTEMS.IS_PRIMARY    = 'Y'<br>
       AND T_STATUS.SEQ_NO         = USR_TR_DATA.STATUS_ID<br>
       AND SYSTEMS.SYSTEMS_ID       = USR_TR_DATA.SYSTEM_ID<br>
       AND CG_MGR_USR.MANAGER_NAME IN( 'SAPUSER' )<br>
       AND T_STATUS.STATUS        IN( 'IN-PROCESS', 'OPEN' )<br>
       AND USR_TR_DATA.COUNTS      >= 1<br>
       ORDER BY
         USER_ID,
         SYSTEM_ID,
         RISK_ID,
         VERSION_ID,
         CC_RISK,
         STATUS,
         COUNTS ASC
     )
     i   )   o <br>WHERE   o.RN   >= 1 AND o.RN <= 10

获取仅10条记录需要花费太多时间。

0 个答案:

没有答案