需要使用 sys_connect_by_path 函数进行索引吗?如何效仿呢?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
config_tab
中的数据不经常更改,您可以使用 具体化视图与您的视图具有相同的查询。然后,您可以为物化视图的path
列建立索引。由于这是一个复杂的查询,您需要执行 完全刷新物化视图,以便 MV 中的数据不会过时。
更新
path
将被定义为VARCHAR2(4000)
。您可以限制该列的大小以便对其建立索引。例如,在查询中,将sys_connect_by_path(...)
替换为SUBSTR(sys_connect_by_path(..., 1, 1000)
。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 thepath
column of your materialized view.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
path
will be defined as aVARCHAR2(4000)
. You could limit the size of this column in order to index it. In your query, replacesys_connect_by_path(...)
bySUBSTR(sys_connect_by_path(..., 1, 1000)
for example.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.