ORACLE-SQL性能优化-排序取第一条数据

在数据40万左右,排序后25万左右测试如下:

方式一(快一点):
SELECT * FROM table  WHERE  (id,createtime)  IN (SELECT id,MAX(createtime)  createtime  FROM  table  GROUP  BY  id);--获取时间最新的多条数据

方式二partition by order by(慢一点)
select a.*  FROM (
     select *  from (
            select t.*,row_number() over(partition by t.id
order by t.createtime desc) rn   from table t
) c  where rn = 1) a
--性能好一点(3S)
select TASK_PROCESSINSTID,TASK_ACTIVITYDEFID,TASK_BEGINTIME,TASK_CONTROLSTATE from V_DW_BPM_TASK
WHERE (TASK_PROCESSINSTID,TASK_ACTIVITYDEFID,TASK_BEGINTIME) IN
(
 select TASK_PROCESSINSTID,TASK_ACTIVITYDEFID,MAX(TASK_BEGINTIME) from V_DW_BPM_TASK
 inner join (
     select PROCESSDEFINITID  --流程定义ID
           ,ACTIVITYDEFID     --节点定义ID
           ,VERSIONNUM        --流程版本号
     from BO_EU_NPD_PROCESS_NODE where IS_ANALYSIS='是'
   ) N on N.ACTIVITYDEFID = TASK_ACTIVITYDEFID AND N.PROCESSDEFINITID = TASK_PROCESSDEFID
 where TASK_ACTIVITYTYPE = 'userTask' --统计人工活动节点 
   AND TASK_OWNER <> 'admin02' --admin02是默认用于系统任务创建的帐号,要排除统计
   AND TASK_CONTROLSTATE <> 'delete'  --删除的节点实例不做统计           
 GROUP  BY
   TASK_PROCESSINSTID,TASK_ACTIVITYDEFID
)


--性能差一点(7S)
select  TASK_PROCESSINSTID, TASK_ACTIVITYDEFID, TASK_CONTROLSTATE, TASK_BEGINTIME, rn
  from (
       --取节点实例中最新一条状态
        select a.*, row_number() over(partition by TASK_PROCESSINSTID,TASK_ACTIVITYDEFID order by TASK_BEGINTIME desc) rn  
        from (select TASK_PROCESSINSTID,TASK_ACTIVITYDEFID,TASK_BEGINTIME,TASK_CONTROLSTATE from V_DW_BPM_TASK
        inner join (
            select PROCESSDEFINITID  --流程定义ID
                  ,ACTIVITYDEFID     --节点定义ID
                  ,VERSIONNUM        --流程版本号
            from BO_EU_NPD_PROCESS_NODE where IS_ANALYSIS='是'
          ) N on N.ACTIVITYDEFID = TASK_ACTIVITYDEFID AND N.PROCESSDEFINITID = TASK_PROCESSDEFID
        where TASK_ACTIVITYTYPE = 'userTask' --统计人工活动节点 
          AND TASK_OWNER <> 'admin02' --admin02是默认用于系统任务创建的帐号,要排除统计
          AND TASK_CONTROLSTATE <> 'delete'  --删除的节点实例不做统计
          --AND (to_char(TASK_BEGINTIME, 'YYYYMMDD') = to_char(sysdate, 'YYYYMMDD') or to_char(TASK_ENDTIME, 'YYYYMMDD') = to_char(sysdate, 'YYYYMMDD'))
        ) a
   ) t  where t.rn = 1 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值