优化oracle db上的子查询

时间:2014-10-13 15:55:55

标签: sql oracle oracle11g

我只对oracle db 11拥有阅读权限。我的问题是我现在开发了以下查询,但是它非常慢。执行此查询最多可能需要5 min。此外,我想在将来为我的查询添加更多标签,我想这次会以指数方式增加。

SELECT distinct *
FROM (
  SELECT sqOne.value_number AS Interest_Rate, sqTwo.value_number AS Equity, u.update_as_of AS REFERENZ_DATUM, u.update_dat_ins AS INSERTION_DATE, p.cust_id, p.project_name
  FROM update u
   JOIN project p ON p.project_id= u.project_id
   JOIN customer cust ON p.CUST_ID = cust.CUST_ID
   JOIN value v ON u.update_id = v.update_id 
   JOIN tag t ON t.tag_id = v.tag_id
-- ###############################################
-- Interest Rate
-- ###############################################
   LEFT OUTER JOIN 
      (
          SELECT va.value_number, pr.project_id
          FROM update up
          JOIN project pr ON pr.project_id= up.project_id
          JOIN value va ON up.update_id = va.update_id 
          JOIN tag ta ON ta.tag_id = va.tag_id
          WHERE ta.tag_desc LIKE 'IR%' AND va.value_number > 0 AND va.value_number IS NOT NULL
      ) sqOne 
  ON sqOne.project_id= p.project_id
-- ###############################################
-- Equity
-- ###############################################
   LEFT OUTER JOIN 
      (
          SELECT va.value_number, pr.project_id--return the tic which is the latest!!! TODO
          FROM update up
          JOIN project pr ON pr.project_id= up.project_id
          JOIN value va ON up.update_id = va.update_id 
          JOIN tag ta ON ta.tag_id = va.tag_id
          WHERE ta.tag_desc LIKE 'EQUITY%' AND va.value_number > 0 AND va.value_number IS NOT NULL
      ) sqTwo 
  ON sqTwo.project_id= p.project_id
 WHERE p.project_stat = 'A'
 ORDER BY p.project_id
);

在这里,我尝试使用select *查询重建表定义:

  • select * from update;
+--------------------------------------------------+
| UPDATE_ID UPDATE_AS_OF UPDATE_DAT_INS PROJECT_ID |
+--------------------------------------------------+
| 1         17-DEC-12    17-DEC-12        34       |
| 2         17-DEC-12    17-DEC-12        31       |
| 14        17-DEC-12    17-DEC-12        29       |
| 21        17-DEC-12    18-DEC-12        2        |
| 22        17-DEC-12    18-DEC-12        2        |
| 25        18-DEC-12    18-DEC-12        66       |
+--------------------------------------------------+
  • select * from project;
+----------------------------------------------------------------------+
| PROJECT_NAME     PROJECT_DESC           CUST_ID           PROJECT_ID |
+----------------------------------------------------------------------+
| Test                null                654321                21     |
| test1               null                654321                39     |
| test2               null                123456                76     |
| test                null                123456                90     |
| Usability           null                323423                158    |
| 5. Test             null                654321                181    |
| fsf                 null                123456                361    |
| Test 100            null                123456                425    |
+----------------------------------------------------------------------+
  • select * from customer;
+-----------------------------------------------------+
|  CUST_ID   CUST_DAT_INS  CUST_DAT_UPD     CUST_NAME |
+-----------------------------------------------------+
| 1            13-DEC-12     01-MAR-14     abc1       |
| 2            13-DEC-12     30-OCT-13     abc2       |
| 3            13-DEC-12     30-OCT-13     abds       |
| 4            13-DEC-12     30-OCT-13     test1      |
| 5            13-DEC-12     30-OCT-13     test23     |
+-----------------------------------------------------+
  • select * from value;
+---------------------------------------------------------------------------------+
| VALUE_ID   VALUE_NUMBER  VALUE_DAT_INS    TAG_ID  PROJECT_ID          UPDATE_ID |
+---------------------------------------------------------------------------------+
| 1          null          17-DEC-12          118          34           1         |
| 2          5000          17-DEC-12          48           34           1         |
| 3          8             17-DEC-12          73           34           1         |
| 4          null          17-DEC-12          187          34           1         |
| 5          null          17-DEC-12          123          34           1         |
+---------------------------------------------------------------------------------+
  • select * from tag;
+------------------------------------------------------------+
| TAG_ID      TAG_CODE         TAG_DESC          TAG_DAT_INS |
+------------------------------------------------------------+
| 1             EQU             EQUITY             13-DEC-12 |
| 2             IR              Interest Rate      13-DEC-12 |
+------------------------------------------------------------+

感谢您就如何优化我的查询以加快加载速度提供答案!

更新

这是explain for查询的更新:

---------------------------------------------------------------------------------------
| Id  | Operation                           | Name            | Rows  | Bytes | Cost  |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |                 |  2600 |   162K|  2747 |
|   1 |  HASH UNIQUE                        |                 |  2600 |   162K|  2747 |
|   2 |   HASH JOIN                         |                 |  2600 |   162K|  2746 |
|   3 |    VIEW                             | VW_DTP_104D9B21 |  4191 | 20955 |   623 |
|   4 |     HASH UNIQUE                     |                 |  4191 | 20955 |   623 |
|   5 |      INDEX FAST FULL SCAN           | VALUE_IDX4      |   531K|  2594K|   590 |
|   6 |    HASH JOIN                        |                 |  2600 |   149K|  2123 |
|   7 |     HASH JOIN RIGHT OUTER           |                 |   495 | 16830 |  2109 |
|   8 |      VIEW                           |                 |   483 |  2898 |  1050 |
|   9 |       NESTED LOOPS                  |                 |   483 | 19320 |  1050 |
|  10 |        HASH JOIN                    |                 |   765 | 27540 |  1050 |
|  11 |         NESTED LOOPS                |                 |  4191 |   102K|    14 |
|  12 |          TABLE ACCESS BY INDEX ROWID|    TAG          |     1 |    16 |     1 |
|  13 |           INDEX UNIQUE SCAN         |    TAG_UDX1     |     1 |       |     0 |
|  14 |          TABLE ACCESS FULL          |    UPDATE       |  4191 | 37719 |    13 |
|  15 |         TABLE ACCESS FULL           |    VALUE        |   123K|  1331K|  1035 |
|  16 |        INDEX UNIQUE SCAN            |    PROJECT_PK   |     1 |     4 |     0 |
|  17 |      HASH JOIN RIGHT OUTER          |                 |   495 | 13860 |  1058 |
|  18 |       VIEW                          |                 |   242 |  1452 |  1052 |
|  19 |        NESTED LOOPS                 |                 |   242 | 12826 |  1052 |
|  20 |         HASH JOIN                   |                 |   383 | 18767 |  1052 |
|  21 |          MERGE JOIN CARTESIAN       |                 |  2096 | 79648 |    16 |
|  22 |           TABLE ACCESS FULL         |    TAG          |     1 |    29 |     3 |
|  23 |           BUFFER SORT               |                 |  4191 | 37719 |    13 |
|  24 |            TABLE ACCESS FULL        |    UPDATE       |  4191 | 37719 |    13 |
|  25 |          TABLE ACCESS FULL          |    VALUE        |   123K|  1331K|  1035 |
|  26 |         INDEX UNIQUE SCAN           |    PROJECT_PK   |     1 |     4 |     0 |
|  27 |       TABLE ACCESS FULL             |    PROJECT      |   495 | 10890 |     5 |
|  28 |     TABLE ACCESS FULL               |    UPDATE       |  4191 |   102K|    13 |
---------------------------------------------------------------------------------------

注意

  • ' PLAN_TABLE'是旧版本

2 个答案:

答案 0 :(得分:2)

从我坐的地方看来,这个问题看起来很简单。以下事情似乎是无用的代码:

  • 在"等级2"中加入customer query ...在该查询级别的任何地方都没有使用customer表中的数据。
  • 在"等级2"中加入value query ...在该查询级别的任何地方都没有使用value表中的数据。
  • 在"等级2"中加入tag query ...在该查询级别的任何地方都没有使用tag表中的数据。
  • 加入projectsqOne中的sqTwo ... pr.project_id都可以选为up.project_id
  • va.value_number IS NOT NULLsqOne中的sqTwo谓词...谓词va.value_number > 0自动隐含两者。
  • " 2级查询中的
  • order by子句" ......由于distinct在"等级1"。
  • ,完全没有意义
  • 很可能在摆脱valuetag联接之后,select distinct ...不需要因为我怀疑那两个联接(沿着我怀疑sqOne / sqTwo仅在project_id而不是update_id上加入SELECT distinct * FROM ( SELECT sqOne.value_number AS Interest_Rate, sqTwo.value_number AS Equity, u.update_as_of AS REFERENZ_DATUM, u.update_dat_ins AS INSERTION_DATE, p.cust_id, p.project_name FROM update u JOIN project p ON p.project_id = u.project_id -- Interest Rate LEFT OUTER JOIN ( SELECT va.value_number, up.project_id FROM update up JOIN value va ON up.update_id = va.update_id JOIN tag ta ON ta.tag_id = va.tag_id WHERE ta.tag_desc LIKE 'IR%' AND va.value_number > 0 ) sqOne ON sqOne.project_id = p.project_id -- Equity LEFT OUTER JOIN ( SELECT va.value_number, up.project_id --return the tic which is the latest!!! TODO FROM update up JOIN value va ON up.update_id = va.update_id JOIN tag ta ON ta.tag_id = va.tag_id WHERE ta.tag_desc LIKE 'EQUITY%' AND va.value_number > 0 ) sqTwo ON sqTwo.project_id = p.project_id WHERE p.project_stat = 'A' -- the following supplements the original inner joins with `tag` and `value`; those two originally left the result with only those `product` and `update` rows which had at least some tags and values AND (sqOne.project_id is not null or sqTwo.project_id is not null) ); / {{1}}的错误加入了"等级2& #34;查询。但我可能也错了,你必须自己测试一下。

因此,在此基本代码清理之后,查询可能如下所示:

{{1}}

请尝试,如果它甚至运行(我可能已经删除了一些重要的东西),如果它做了你需要它做的事情,如果它运行得更快或者没有,如果没有那么发布经过的时间它的执行和新的执行计划。

答案 1 :(得分:0)

您可以尝试重新设计查询。

1)您不需要2个子查询。由于连接条件在主查询和子查询中是相同的,我们可以在那里计算兴趣和权益,例如感兴趣的是在基本查询本身中使用以下语句

(CASE WHEN t.tag_desc LIKE 'IR%' AND v.value_number > 0 AND v.value_number IS NOT NULL THEN v.value_number ELSE NULL END) AS Interest_Rate

同样写入权益并删除2个子查询。

2)只需检查是否需要使用DISTINCT

希望它有所帮助。