sql优化

本文介绍了一个具体的SQL查询优化案例,对比了优化前后的SQL语句,并详细解释了如何通过重构查询逻辑来减少全表扫描,提高查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sql优化前:

SELECT DISTINCT a.acti_prce_id prce_id, a.acti_id, a.aggregate_code code

  FROM wf_activity_instances a,

       wf_activity_instances_pati f, --这个表全表扫描了

       (SELECT pati_id_arg2 column_value

          FROM wf_participant_relations a,

               (SELECT pati_id_arg1 user_id

                  FROM wf_participant_relations p

                 WHERE p.pati_id_arg2 = 11521

                   AND p.relation_type = 'GRANT_H'

                UNION

                SELECT 11521 user_id FROM dual) h

         WHERE a.pati_id_arg1 = h.user_id

           AND a.relation_type IN ('MEMBER OF', 'GRANT')

        UNION

        SELECT 11521 column_value

          FROM dual

        UNION

        SELECT 5 column_value

          FROM dual

        UNION

        SELECT pati_id_arg1 column_value

          FROM wf_participant_relations p

         WHERE p.pati_id_arg2 = 11521

           AND p.relation_type = 'GRANT_H') g

 WHERE 11521 != 1

   AND a.state = 'NOTRUNNING'

   AND a.negation_ind = 'N'

   AND a.date_created <= SYSDATE

   AND (a.id = f.acin_id AND g.column_value = f.pati_id);

 

优化后:

select distinct a.acti_prce_id   prce_id,

                a.acti_id,

                a.aggregate_code code

  from (select x.*

          from wf_activity_instances x

         where x.state = 'NOTRUNNING'

           and x.negation_ind = 'N'

           and x.date_created <= sysdate) a

 where 11521 != 1

   and exists (select 1

          from wf_activity_instances_pati zz

         where zz.acin_id = a.id

           and exists (select 1

                  from (select pati_id_arg2 column_value

                          from wf_participant_relations a,

                               (select pati_id_arg1 user_id

                                  from wf_participant_relations p

                                 where p.pati_id_arg2 = 11521

                                   and p.relation_type = 'GRANT_H'

                                union

                                select 11521 user_id from dual) h

                         where a.pati_id_arg1 = h.user_id

                           and a.relation_type in ('MEMBER OF', 'GRANT')

                        union

                        select 11521 column_value

                          from dual

                        union

                        select 5 column_value

                          from dual

                        union

                        select pati_id_arg1 column_value

                          from wf_participant_relations p

                         where p.pati_id_arg2 = 11521

                           and p.relation_type = 'GRANT_H') g

                 where g.column_value = zz.pati_id))

 

wf_activity_instances,wf_activity_instances_pati

两个表均是百万级数据量

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值