使用UNION,多列的性能问题

时间:2017-07-06 09:11:24

标签: sql oracle oracle11g oracle12c

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

使用UNION的性能问题

    SELECT
                    store.store_name name,
                    store.block_id block_id,
                    store.marker flag,
                    substr('A',1,1) store_type,
                    substr(area.area_code,1,5) area_code,
                    substr(area.area_name,1,5) area_name
    FROM  store , area
            where store.store_id = area.store_id
            and store.block_id = area.block_id
    UNION
     SELECT
                    store.store_name name,
                    store.block_id block_id,
                    store.marker flag,
                    substr('A',1,1) store_type,
                    substr(market.market_code,1,5) area_code,
                    substr(market.market_area,1,5) area_name
    FROM  store , market
            where store.store_id = market.store_id 
            and store.block_id =market.block_id;                                

                 | 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 :(得分:0)

通常,UNION不会导致FTS。如果优化器认为它可以更好地扫描所有行而不是索引访问,然后是表访问,则由优化器决定。所以我看到了这个问题的一些问题:

  1. 没有数据子集,这意味着您需要所有行 无论如何,如果是这样,那么FTS很可能会成为 成本最低的选择。

  2. 没有数据子集,是索引的唯一可能方式 如果“连接列”是至少表中的索引,则使用 跟随FTS(在某些情况下,您可能会寻找较小的表格 成为FTS,然后基于索引访问更大的表 连接条件列是索引)。

  3. 您没有指明表格大小或索引是什么; 对基数等知识对于SQL调优至关重要。

  4. 此外,如果您拥有Diagnostics + Tuning Pack许可,则可以通过SQL Tuning Advisor运行SQL,并查看此工具提供的调优建议。