---
title: "sql优化"
author: "soft_xiaohui"
date: "2012-12-19T19:46:59Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2012-12-19T19:46:59Z"
id: 1750612
url: "https://soft-xiaohui.iteye.com/blog/1750612"
---

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 &lt;= 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 &lt;= 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

两个表均是百万级数据量