使用UNION的性能问题

时间:2017-07-06 08:43:29

标签: sql oracle plsql oracle11g oracle12c

oracle查询中的联合导致了很多FTS(全表扫描)和高行数,我可以更好地重写这个查询吗?

            SELECT
                            tab1.a1,
                            tab1.a2 ,
                            tab2.b1,
                            tab2.b2


            FROM tab1 ,tab2
                    where tab1.aid = tab2.aid
                    and tab1.bid = tab2.bid
            UNION 
            SELECT
                            tab1.a1,
                            tab1.a2,
                            tab3.c1,
                            tab3.c2
            FROM tab1 ,tab3
                    where tab1.cid = tab3.cid
                    and tab1.bid =tab3.bid;

解释计划看起来很像......

                 | Id  | Operation                     | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
                 ---------------------------------------------------------------------------------------
                 |   0 | SELECT STATEMENT              |  1845K|    61M|       |   120K  (1)| 00:00:05 |
                 |   1 |  UNION-ALL                    |       |       |       |            |          |
                 |   2 |   MERGE JOIN                  |  1719K|    57M|       | 98522   (2)| 00:00:04 |
                 |   3 |    SORT JOIN                  |  1761K|    25M|    94M| 30984   (1)| 00:00:02 |
                 |   4 |     TABLE ACCESS FULL         |  1761K|    25M|       | 21911   (1)| 00:00:01 |
                 |*  5 |    SORT JOIN                  |  1882K|    35M|   115M| 67538   (2)| 00:00:03 |
                 |   6 |     TABLE ACCESS FULL         |  1882K|    35M|       | 56061   (2)| 00:00:03 |
                 |   7 |   NESTED LOOPS                |   126K|  3699K|       | 22186   (1)| 00:00:01 |
                 |   8 |    NESTED LOOPS               |   126K|  3699K|       | 22186   (1)| 00:00:01 |
                 |   9 |     TABLE ACCESS FULL         |   126K|  1726K|       |  3232   (2)| 00:00:01 |

索引已开启(tab1(援助,出价),tab2(援助,出价),tab3(cid,bid))

1 个答案:

答案 0 :(得分:2)

SELECT t.a1, t.a2  
FROM tab1 t
WHERE EXISTS (SELECT 1
              FROM tab2 t2
              WHERE t.aid = t2.aid
              AND t.bid = t2.bid)
OR EXISTS (SELECT 1
           FROM tab3 t3
           WHERE t.cid = t3.cid
           AND t.bid =t3.bid)