如何提高Oracle中的连接查询的性能

时间:2017-04-28 12:46:05

标签: oracle query-performance connect-by

我有以下连接查询,在my_table_name中记录数为500,大约需要50到60秒。

您是否可以建议更好的方法来编写此查询以提高性能。

SELECT DISTINCT REGEXP_SUBSTR(STD_DEP,'[^,]+', 1, LEVEL) AS DEP_STD_ID
FROM my_table_name
WHERE std_id = 242
  CONNECT BY REGEXP_SUBSTR(STD_DEP,'[^,]+', 1, LEVEL) IS NOT NULL;

在上面的查询中:STD_DEP是一个逗号分隔的字段,它将包含所有依赖的std_id。

1 个答案:

答案 0 :(得分:0)

在Oracle中有many, many ways分隔分隔字符串。

不使用正则表达式的是:

WITH bounds ( id, list, start_pos, end_pos, lvl ) AS (
  SELECT id, list, 1, INSTR( list, ',' ), 1
  FROM   my_table_name
  WHERE  std_id = 242
UNION ALL
  SELECT id,
         list,
         end_pos + 1,
         INSTR( list, ',', end_pos + 1 ),
         lvl + 1
  FROM   bounds
  WHERE  end_pos > 0
)
SELECT id,
       SUBSTR(
         list,
         start_pos,
         DECODE( end_pos, 0, LENGTH( list ) + 1, end_pos ) - start_pos
       ) AS item,
      lvl
FROM   bounds
ORDER BY id, lvl;