Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding explain plan through xmltype

I had a performance issue caused by a wrong xpath ('@' missing in attribute predicate) in a query like this:

select extractvalue(field, '//item[attr="value"]') from table where field1 = :1;

I expected an exception but seems that Oracle accept this particular xpath, has a meaning?

I tried to perform an explain plan against that query but the result is pretty strange, someone can help me to understand it?

I used this code to reproduce the environment

SELECT * FROM V$VERSION;
/*
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
"CORE   11.2.0.3.0  Production"
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
*/

create table TMP_TEST_XML(
  id number,
 content_xml xmltype 
);
/
create unique index IDX_TMP_TEST_XML on TMP_TEST_XML(id);
/
declare
  xml xmltype := xmltype('<root>
    <a key="A">Aaa</a>
    <b key="B">Bbb</b>
    <c key="C">Ccc</c>
    <d key="D">Ddd</d>
    <e key="E">Eee</e>
    <f key="F">Fff</f>
    <g key="G">Ggg</g>
    <h key="H">Hhh</h>
    <i key="I">Iii</i>
    <l key="L">Lll</l>
</root>');
begin

  for idx in 1..10000 
  loop
    insert into TMP_TEST_XML values (idx, xml);
  end loop;

  commit;

end;
/
--explain plan xpath without '@' (wrong)
EXPLAIN PLAN SET statement_id = 'planXml1' FOR
select extractvalue(content_xml, '/root/g[key="G"]') from TMP_TEST_XML where id between 120 and 130;
/    
select plan_table_output
from table(dbms_xplan.display('plan_table',null,'advanced'));
/
/*
------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                  |    24 | 48360 |     4   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE             |                  |     1 |     4 |            |          |
|   2 |   NESTED LOOPS SEMI         |                  |   667K|  2606K|   223K  (1)| 00:44:37 |
|   3 |    XPATH EVALUATION         |                  |       |       |            |          |
|*  4 |    XPATH EVALUATION         |                  |       |       |            |          |
|   5 |  TABLE ACCESS BY INDEX ROWID| TMP_TEST_XML     |    24 | 48360 |     4   (0)| 00:00:01 |
|*  6 |   INDEX RANGE SCAN          | IDX_TMP_TEST_XML |    43 |       |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------------------------
*/
/
-- explain plan xpath with '@' (correct)
EXPLAIN PLAN SET statement_id = 'planXml1' FOR
select extractvalue(content_xml, '/root/g[@key="G"]') from TMP_TEST_XML where id between 120 and 130;
/
select plan_table_output
from table(dbms_xplan.display('plan_table',null,'advanced'));
/
/*
------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                  |    24 | 48360 |     4   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE             |                  |     1 |     4 |            |          |
|*  2 |   XPATH EVALUATION          |                  |       |       |            |          |
|   3 |  TABLE ACCESS BY INDEX ROWID| TMP_TEST_XML     |    24 | 48360 |     4   (0)| 00:00:01 |
|*  4 |   INDEX RANGE SCAN          | IDX_TMP_TEST_XML |    43 |       |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------------------------
*/

In the first explain there is a 'nested loops' (row 2) with cardinality 667K that disappeared in the second one. Inserting more records in the same table and performing a new explain plain (without '@') that value is always 667K.

What does it represent that value?

like image 366
Francesco Genta Avatar asked Nov 10 '22 14:11

Francesco Genta


1 Answers

I expected an exception but seems that Oracle accept this particular xpath, has a meaning?

Well, yes. In itself, the xpath /root/g[key="G"] fetches the node which has a child with tag "key" and value "G". So, even though the extractvalue would fail (more than one node is returned), this would work:

select extract(xmltype('<root>
<a key="A">Aaa</a>
<g key="G"><key>G</key>Ggg</g>
<h key="H">Hhh</h></root>'),'/root/g[key="G"]').getStringVal() from dual;

It returns <g key="G"><key>G</key>Ggg</g>

The high cost could be justified in this kind of search, because attributes are probably more optimized and searchable than other kind of sub-nodes (it can suffice to say that there can be only one with a particular name for each tag, while tags can be repeated multiple times).

like image 73
Matteo Steccolini Avatar answered Nov 15 '22 06:11

Matteo Steccolini