需要使用 sys_connect_by_path 函数进行索引吗?如何效仿呢?

发布于 2024-08-24 15:04:21 字数 916 浏览 11 评论 0原文

我在 Oracle 9i 中有一个自引用表,以及一个从中获取数据的视图:

CREATE OR REPLACE VIEW config AS
SELECT c.node_id,
       c.parent_node_id,
       c.config_key,
       c.config_value,
       (SELECT c2.config_key 
          FROM vera.config_tab c2 
         WHERE c2.node_id = c.parent_node_id) AS parent_config_key,
       sys_connect_by_path(config_key, '.') path,
       sys_connect_by_path(config_key, '->') php_notation
  FROM config_tab c
CONNECT BY c.parent_node_id = PRIOR c.node_id
 START WITH c.parent_node_id IS NULL
 ORDER BY LEVEL DESC

该表存储 PHP 应用程序的配置。现在我需要在 oracle 视图中使用相同的配置。

我想通过路径从视图中选择一些值,但不幸的是这需要 0.15 秒,所以这是不可接受的成本。

SELECT * FROM some_table
 WHERE some_column IN (
   SELECT config_value FROM config_tab WHERE path = 'a.path.to.config'
 )

起初我想到了 sys_connect_by_path 上的函数索引,但这是不可能的,因为它还需要 CONNECT BY 子句。

有什么建议如何从“配置”视图模拟路径列上的索引吗?

I have a self referencing table in Oracle 9i, and a view that gets data from it:

CREATE OR REPLACE VIEW config AS
SELECT c.node_id,
       c.parent_node_id,
       c.config_key,
       c.config_value,
       (SELECT c2.config_key 
          FROM vera.config_tab c2 
         WHERE c2.node_id = c.parent_node_id) AS parent_config_key,
       sys_connect_by_path(config_key, '.') path,
       sys_connect_by_path(config_key, '->') php_notation
  FROM config_tab c
CONNECT BY c.parent_node_id = PRIOR c.node_id
 START WITH c.parent_node_id IS NULL
 ORDER BY LEVEL DESC

The table stores configuration for PHP application. Now I need to use same config in oracle view.

I would like to select some values from the view by path, but unfortunately this takes 0,15s so it's unacceptable cost.

SELECT * FROM some_table
 WHERE some_column IN (
   SELECT config_value FROM config_tab WHERE path = 'a.path.to.config'
 )

At first I thought of a function index on sys_connect_by_path, but it is impossible, as it needs also CONNECT BY clause.

Any suggestions how can I emulate an index on the path column from the 'config' view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-08-31 15:04:21

如果 config_tab 中的数据不经常更改,您可以使用 具体化视图与您的视图具有相同的查询。然后,您可以为物化视图的 path 列建立索引。

CREATE MATERIALIZED VIEW config
   REFRESH COMPLETE ON DEMAND 
   AS <your_query>;

CREATE INDEX ix_config_path ON config (path);

由于这是一个复杂的查询,您需要执行 完全刷新物化视图,以便 MV 中的数据不会过时。

更新

  • 您的列path 将被定义为VARCHAR2(4000)。您可以限制该列的大小以便对其建立索引。例如,在查询中,将 sys_connect_by_path(...) 替换为 SUBSTR(sys_connect_by_path(..., 1, 1000)
  • 您将无法使用 REFRESH ON COMMIT 对复杂的 MV 不起作用,您必须修改更新基表的代码以包含刷新,我不知道这在您的环境中是否实用
  • 。提交将刷新 MV 的作业的触发器。该作业将在您提交后执行(这是 dbms_job 的一项功能),因为您必须检查是否只触发该作业。每个事务执行一次作业(例如使用包变量),这仅在您不经常更新基表时才实用。

If your data doesn't change frequently in the config_tab, you could use a materialized view with the same query as your view. You could then index the path column of your materialized view.

CREATE MATERIALIZED VIEW config
   REFRESH COMPLETE ON DEMAND 
   AS <your_query>;

CREATE INDEX ix_config_path ON config (path);

Since this is a complex query, you would need to do a full refresh of your materialized view every time the base table is updated so that the data in the MV doesn't become stale.

Update

  • Your column path will be defined as a VARCHAR2(4000). You could limit the size of this column in order to index it. In your query, replace sys_connect_by_path(...) by SUBSTR(sys_connect_by_path(..., 1, 1000) for example.
  • You won't be able to use REFRESH ON COMMIT on a complex MV. A simple trigger won't work. You will have to modify the code that updates your base table to include a refresh somehow, I don't know if this is practical in your environment.
  • You could also use a trigger that submits a job that will refresh the MV. The job will execute once you commit (this is a feature of dbms_job). This is more complex since you will have to check that you only trigger the job once per transaction (using a package variable for example). Again, this is only practical if you don't update the base table frequently.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文