低效的SQL搜索查询 - Oracle DB

时间:2015-03-24 06:59:14

标签: sql oracle performance search query-optimization

我在这个查询中的逻辑是正确的(我80%确定它是)。但它已经运行了2小时23分钟仍在继续,我想知道是否有人可以帮助我提高运行效率,因为我不认为这是一个强烈的查询

SELECT b.bridge_no, COUNT(*) AS comment_cnt
  FROM iacd_asset b INNER JOIN iacd_note c
    ON REGEXP_LIKE(c.comments, '(^|\W)BN' || b.bridge_no || '(\W|$)', 'i')
inner join ncr_note e on c.note_id=e.note_id
inner join ncr f on e.ncr_id=f.ncr_id
inner join ncr_iac g on f.ncr_id=g.ncr_id
 WHERE c.create_dt >= date'2015-01-01'
   AND c.create_dt < date'2015-03-12' 
   AND length(b.bridge_no) > 1
   AND g.scheme in (1, 3, 5, 6, 7, 8, 9, 9, and about 10 more values)
 GROUP BY b.bridge_no
 ORDER BY comment_cnt;

简而言之,查询应该是一堆连接,然后通过scheme(g.scheme in ....)过滤连接表,然后解析notes字段中包含BN的任何内容。

PLAN TABLE,好吧,我之前从未使用过,但我相信这是计划表

+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| OPERATION        | OPTIONS        | OBJECT_OWNER | OBJECT_NAME      | OBJECT_ALIAS | OBJECT_INSTANCE | OBJECT_TYPE    | OPTIMIZER | ID | PARENT_ID | DEPTH | POSITION | COST    | CARDINALITY | BYTES       | CPU_COST        | IO_COST | TEMP_SPACE | ACCESS_PREDICATES           | FILTER_PREDICATES                                                               | PROJECTION                                                                              | TIME | QBLOCK_NAME  |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| SELECT STATEMENT |                |              |                  |              |                 |                | ALL_ROWS  | 0  |           | 0     | 281,503  | 281,503 | 40          | 4,480       | 148,378,917,975 | 215,677 |            |                             |                                                                                 |                                                                                         | 458  |              |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| SORT             | ORDER BY       |              |                  |              |                 |                |           | 1  | 0         | 1     | 1        | 281,503 | 40          | 4,480       | 148,378,917,975 | 215,677 |            |                             |                                                                                 | (#keys=1) COUNT(*)[22], "B"."BRIDGE_NO"[NUMBER,22]                                      | 458  | SEL$81719215 |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| HASH             | GROUP BY       |              |                  |              |                 |                |           | 2  | 1         | 2     | 1        | 281,503 | 40          | 4,480       | 148,378,917,975 | 215,677 |            |                             |                                                                                 | (#keys=1) "B"."BRIDGE_NO"[NUMBER,22], COUNT(*)[22]                                      | 458  |              |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| HASH JOIN        |                |              |                  |              |                 |                |           | 3  | 2         | 3     | 1        | 281,497 | 16,084      | 1,801,408   | 148,366,537,976 | 215,677 | 24,126,000 | "G"."NCR_ID"="F"."NCR_ID"   |                                                                                 | (#keys=1) "B"."BRIDGE_NO"[NUMBER,22]                                                    | 458  |              |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| HASH JOIN        |                |              |                  |              |                 |                |           | 4  | 3         | 4     | 1        | 96,996  | 209,778     | 21,607,134  | 13,549,630,814  | 90,985  | 22,725,000 | "E"."NCR_ID"="F"."NCR_ID"   |                                                                                 | (#keys=1) "F"."NCR_ID"[NUMBER,22], "B"."BRIDGE_NO"[NUMBER,22]                           | 158  |              |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| HASH JOIN        |                |              |                  |              |                 |                |           | 5  | 4         | 5     | 1        | 42,595  | 208,419     | 20,216,643  | 5,484,063,163   | 40,162  | 9,839,000  | "C"."NOTE_ID"="E"."NOTE_ID" | REGEXP_LIKE ("C"."COMMENTS",'(^|\W)BN'||TO_CHAR("B"."BRIDGE_NO")||'(\W|$)','i') | (#keys=1) "B"."BRIDGE_NO"[NUMBER,22], "E"."NCR_ID"[NUMBER,22]                           | 70   |              |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| PARTITION RANGE  | SINGLE         |              |                  |              |                 |                |           | 6  | 5         | 6     | 1        | 1,039   | 104,603     | 8,577,446   | 62,280,224      | 1,011   |            |                             |                                                                                 | "C"."NOTE_ID"[NUMBER,22], "C"."COMMENTS"[VARCHAR2,4000]                                 | 2    |              |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| TABLE ACCESS     | FULL           | IACDB        | IACD_NOTE        | C@SEL$1      | 2               | TABLE          | ANALYZED  | 7  | 6         | 7     | 1        | 1,039   | 104,603     | 8,577,446   | 62,280,224      | 1,011   |            |                             | "C"."CREATE_DATE"<TO_DATE(' 2014-12-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss')     | "C"."NOTE_ID"[NUMBER,22], "C"."COMMENTS"[VARCHAR2,4000]                                 | 2    | SEL$81719215 |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| MERGE JOIN       | CARTESIAN      |              |                  |              |                 |                |           | 8  | 5         | 6     | 2        | 24,267  | 12,268,270  | 184,024,050 | 2,780,501,758   | 23,033  |            |                             |                                                                                 | (#keys=0) "B"."BRIDGE_NO"[NUMBER,22], "E"."NCR_ID"[NUMBER,22], "E"."NOTE_ID"[NUMBER,22] | 40   |              |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| TABLE ACCESS     | FULL           | IACDB        | IACD_ASSET       | B@SEL$1      | 1               | TABLE          | ANALYZED  | 9  | 8         | 7     | 1        | 7       | 40          | 160         | 560,542         | 7       |            |                             | LENGTH(TO_CHAR("B"."BRIDGE_NO"))>1                                              | "B"."BRIDGE_NO"[NUMBER,22]                                                              | 1    | SEL$81719215 |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| BUFFER           | SORT           |              |                  |              |                 |                |           | 10 | 8         | 7     | 2        | 24,259  | 308,248     | 3,390,728   | 2,779,941,216   | 23,026  |            |                             |                                                                                 | (#keys=0) "E"."NCR_ID"[NUMBER,22], "E"."NOTE_ID"[NUMBER,22]                             | 40   |              |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| TABLE ACCESS     | FULL           | IACDB        | IACD_NCR_NOTE    | E@SEL$2      | 4               | TABLE          | ANALYZED  | 11 | 10        | 8     | 1        | 606     | 308,248     | 3,390,728   | 69,498,530      | 576     |            |                             |                                                                                 | "E"."NCR_ID"[NUMBER,22], "E"."NOTE_ID"[NUMBER,22]                                       | 1    | SEL$81719215 |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| INDEX            | FAST FULL SCAN | IACDB        | PK_IACDNCR_NCRID | F@SEL$3      |                 | INDEX (UNIQUE) | ANALYZED  | 12 | 4         | 5     | 2        | 31,763  | 22,838,996  | 137,033,976 | 3,248,120,913   | 30,322  |            |                             |                                                                                 | "F"."NCR_ID"[NUMBER,22]                                                                 | 52   | SEL$81719215 |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+
| TABLE ACCESS     | FULL           | IACDB        | IACD_NCR_IAC     | G@SEL$4      | 8               | TABLE          | ANALYZED  | 13 | 3         | 4     | 2        | 181,461 | 1,731,062   | 15,579,558  | 134,407,812,606 | 121,833 |            |                             | ALL THE SCHEMES CHCECKS                                                         | "G"."NCR_ID"[NUMBER,22]                                                                 | 295  | SEL$81719215 |
+------------------+----------------+--------------+------------------+--------------+-----------------+----------------+-----------+----+-----------+-------+----------+---------+-------------+-------------+-----------------+---------+------------+-----------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+------+--------------+

希望这足够清晰

索引的内容我假设只有被排序的字段是重要的 crate_dt未编入索引 scheme id已编入索引

也许我的查询顺序错了......

1 个答案:

答案 0 :(得分:2)

计划显示你正在进行IACD_NOTE和IACD_ASSET的全表扫描,然后对它们进行CARETESIAN连接,因为你提供了无标准,用于将IACD_ASSET中的一条记录链接到一组IACD_NOTE中的记录。

这不是我对非强烈查询的定义,而且CPU成本的令人eye目结舌的值也证明了这一点。

您需要替换此..,

FROM iacd_asset b INNER JOIN iacd_note c
ON REGEXP_LIKE(c.comments, '(^|\W)BN' || b.bridge_no || '(\W|$)', 'i')

...在索引列上实际连接。如果Notes通过BRIDGE_NO或类似的外键链接到Assets会很有帮助。我不知道你的数据模型。然后,您可以将该正则表达式用作WHERE子句中的附加过滤器。

此外,您还要加入另外三个表,以获得允许在SCHEME上添加其他过滤器的内容。同样,我不知道你的数据模型,但这似乎效率很低。

不幸的是,这种依赖于领域知识的调整。修复此查询需要了解数据 - 数据量,分布和偏差,数据模型本身以及查询实现的业务逻辑。这超出了我们在StackOverflow中提供的建议的范围。


有一点需要考虑,但是一个重要的决定是用自由文本索引索引注释。但是,这有很多分支(特别是空间和数据库管理员)。 Find out more