【问题标题】:Use XMLIndex on column queries of a XMLTable对 XMLTable 的列查询使用 XMLIndex
【发布时间】:2018-11-20 13:06:56
【问题描述】:

我想知道当属性被解析为表的列时,是否有办法让 Oracle 在属性上使用 XMLIndex。例如,以下代码将使用我指定的XMLIndex,但如果我指定列名,它不会这样做:

SELECT *
FROM mytable, XMLTABLE(
  '/foo' PASSING PAYLOAD COLUMNS
  res PATH './bar/text()'
) 
-- where res = 'abc';
where XMLExists('/foo/bar/text()="abc"]' PASSING PAYLOAD)

我想定义这样的表的视图,其中包含许多应该易于查询的列。目前我通过直接使用物化视图和索引列来解决问题,但我更喜欢使用XMLIndex 的解决方案。

【问题讨论】:

    标签: sql xml oracle indexing


    【解决方案1】:

    如需了解更多详情,请在谷歌搜索Structured Component to an XMLIndex Index。 还有这里的小例子。

    1)准备测试结构。

    create table xindex_example(a number, b xmltype);
    
    insert into xindex_example values(1,xmltype('<root><x>
    <a>1</a>
    <b>2</b>
    </x>
    </root>'));
    

    2) 使用参数创建 xmlindex。

    CREATE INDEX xindex_example_idx ON xindex_example (b) INDEXTYPE IS XDB.XMLIndex
      PARAMETERS (q'~XMLTable my_test_group_tab
                     '/root/x'
                     columns a_val number path './a' ,  b_val number path './b'~');
    
      ;
    

    Oracale 会自动创建表 my_test_group_tab,但您不能直接从该表进行查询。 (ORA-30967: operation directly on the Path Table is disallowed)

    您可以执行 desc my_test_group_tab 来查看结构。

    3) 在create table上创建b+tree索引

    create index my_test_group_tab_idx on my_test_group_tab(a_val);
    

    4) 并测试它。

      select * from (
      select x.* from xindex_example,xmltable('/root/x' passing b columns a_val number path './a' ,  b_val number path './b') x)
      where a_val=1;
    

    5) 执行计划:

    -----------------------------------------------------------------------------------------------------
    | Id  | Operation                    | Name                  | Rows  | Bytes | Cost (%CPU)| Time     |
    ------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT             |                       |     1 |    50 |     3   (0)| 00:00:01 |
    |   1 |  NESTED LOOPS                |                       |     1 |    50 |     3   (0)| 00:00:01 |
    |   2 |   TABLE ACCESS BY INDEX ROWID| MY_TEST_GROUP_TAB     |     1 |    38 |     2   (0)| 00:00:01 |
    |*  3 |    INDEX RANGE SCAN          | MY_TEST_GROUP_TAB_IDX |     1 |       |     1   (0)| 00:00:01 |
    |   4 |   TABLE ACCESS BY USER ROWID | XINDEX_EXAMPLE        |     1 |    12 |     1   (0)| 00:00:01 |
    ------------------------------------------------------------------------------------------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多