ITPub博客

首页 > 数据库 > Oracle > xmltype数据类演练及读取效率测算

xmltype数据类演练及读取效率测算

原创 Oracle 作者:ocpDBAboy 时间:2015-06-17 01:03:01 0 删除 编辑

xmltype 预定义的对象类型

--1.演练部分
--用于保存和操作xml数据
--创建一个可以装xml的表
create table fallsxml(
fall_id number,
fall_xml xmltype
);

--使用静态方法CreateXML 插入第1条xml记录
INSERT INTO fallsxml
VALUES
  (1,
   xmltype.CreateXML('
      
          Munising Falls
          Alger
          MI
         
             [url]http://michiganwaterfalls.com/munising_falls/munising_falls.html[/url]
         

      
'));
      
--插入第2条xml记录        
INSERT INTO fallsxml
VALUES
  (2,
   XMLType.CreateXML('
 
    Au Train Falls
    Alger
    MI
   
       [url]http://michiganwaterfalls.com/autrain_falls/autrain_falls.html[/url]
   

 
'));

--插入第3条xml记录
INSERT INTO fallsxml
VALUES
  (3,
   XMLType.CreateXML('
        
            Laughing Whitefish Falls
            Alger
            MI
        
'));
--查看插入的记录,使用existsNode()方法检索节点
select t.*,rowid from fallsxml t;
select fall_id from fallsxml f where f.fall_xml.existsNode('/fall/url')>0;--存在fall/url    
select fall_id from fallsxml where existsNode(fall_xml,'/fall/url')>0;--fall列中存在fall下面有url的

--抽取xml的文本并输出(获得指定字符,采用静态方法extract )
--在plqldev中运行报错(去掉2个demo_block即可),在sqlplus中正确
<>
declare
  v_xml xmltype;
  url   varchar2(500);
begin
  --查询xml记录into给变量v_xml
  select t.fall_xml into demo_block.v_xml from fallsxml t where t.fall_id = 1;
  --从取到的xml记录中,抽取extract 节点值
  url := v_xml.extract('/fall/url/text()').getStringVal();
  dbms_output.put_line(url);
end;
/
--输出 [url]http://michiganwaterfalls.com/munising_falls/munising_falls.html[/url]
注:v_xml.extract('/fall/url/text(  )').getStringVal 调用静态方法extract提取相应数据,也就是提供根目录下的url子目录下的文本,text()指url的体部文本。geteStringVal是文本的值。
select Upper(Xmltype.getStringVal(xmltype.extract(fall_xml,'/fall/url/text()'))) from fallsxml
        where existsNode(fall_xml,'/fall/url')>0;
上面的是将查询出来的html地址变为大写     


2.效率测算部分

---测试代码:
--xmltype()执行效率测算
select f_gettime(fall_xml, '/fall/url', 10000)
  from fallsxml
 where rownum = 1;
--返回值:
+000000000 00:00:00.485000000||
               [url]http://michiganwaterfalls.com/munising_falls/munising_falls.html[/url]
           
测试函数代码:
create or replace function f_gettime(sb in xmltype,path in varchar2,a in number:=10000) return varchar2 is
  cc xmltype;
  v_time timestamp;
begin
  v_time:=current_timestamp;
  for i in 1..a loop
   cc:=sb.extract(path);
    end loop;
  return to_char(current_timestamp-v_time) || '||' ||cc.getStringVal();
end f_gettime;
select xmltype(ts_clob).extract('/ROWDATA/ROW/ID2/text()') from ts_clob where rownum=1;--xmltype()执行效率
select extract(xmltype(ts_clob),'/ROWDATA/ROW/ID2/text()') from ts_clob where rownum=1;


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29267792/viewspace-1702052/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: oracle碎片清理
请登录后发表评论 登录
全部评论
积极,热爱生活!

注册时间:2014-01-15

  • 博文量
    32
  • 访问量
    112280