惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
A
Arctic Wolf
S
Security Affairs
O
OpenAI News
SecWiki News
SecWiki News
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
T
Threat Research - Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
N
News | PayPal Newsroom
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
The Hacker News
The Hacker News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
T
Tenable Blog
T
The Exploit Database - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
人人都是产品经理
人人都是产品经理
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
The Cloudflare Blog
N
News and Events Feed by Topic
量子位
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hacker News - Newest:
Hacker News - Newest: "LLM"
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Webroot Blog
Webroot Blog

博客园 - 挑战

从存储过程中读取相关信息 Blend Step by Step书籍笔记(第一章) WPF非轮询方式实时更新数据库变化SqlDependency 动态解析XAML文本构建WPF的UI 解决为'*********' 的游标已存在问题 数据表死锁查询和处理 SQL Server操作XML(六)XML FLOWR SQL Server操作XML(四)XML数据类型 SQL Server操作XML(三)OPENXML函数功能 SQL Server操作XML(二)XML子句实例 SQL Server操作XML(一)XML子句 数据绑定 最为详尽的WPF类继承关系 LinQ数据访问 WPF Diagram Designer Part 3:连接Item 照猫画虎WPF之二数据绑定 照猫画虎WPF之一:命名空间 解决WPF部署后客户端访问安全性问题 C#读取文本播放相应语音
SQL Server操作XML(五)XML Query-XQuery
挑战 · 2012-06-13 · via 博客园 - 挑战

(1)索引

XML字段最大支持2G,如果不建立索引,遍历数据查询,性能会很差。

  • Primary Index             必须有主键列,且为聚集索引。该索引中存放XML数据中Tag、Value和Path等信息
  • 辅助Path Index            用于基于Path查询,即XPath查询
  • 辅助Property Index      用于基于节点的查询
  • 辅助Value Index          用于XML值的查询

XQuery即查询XML语言,包括基于XPath、Element和Attribute

语法包括FLOWR

For                遍历满足条件节点的内容

Let           

Order By

Where

Return

(2)索引查询

原始数据

CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
 Customer VARCHAR(20),
 SaleDate Date,
 OrderDetail xml)

insert orders values('Tom','2009-09-13','
<OrderDetails>
  <row>
     <Product_ID>2</Product_ID>
     <Quantity>200</Quantity>
  </row>
  <row>
     <Product_ID>3</Product_ID>
     <Quantity>300</Quantity>
  </row>
  <row>
     <Product_ID>1</Product_ID>
     <Quantity>400</Quantity>
  </row>
</OrderDetails>
')

  • Primary Index

create primary xml index xidx_details on orders(OrderDetail)

显示错误:表 'orders' 需要具有一个包含的列数小于 16 的聚集主键,才能为其创建主 XML 索引。

 原因是缺少聚集的主键列

alter table orders
add constraint pk_orders_orderid
primary key clustered(orderid)

  • Path Index

--在primary index基础上,创建path index
create xml index xidx_details_path on orders(OrderDetail) using xml index xidx_details for path

 基于Path Index查询,按照相对路径//OrderDetails/row查询

select OrderID,OrderDetail.query('//OrderDetails/row') from orders

显示结果

<row>
  <Product_ID>2</Product_ID>
  <Quantity>200</Quantity>
</row>
<row>
  <Product_ID>3</Product_ID>
  <Quantity>300</Quantity>
</row>
<row>
  <Product_ID>1</Product_ID>
  <Quantity>400</Quantity>
</row>

  •  Property Index

--在primary index基础上,创建property index
create xml index xidx_details_property on orders(OrderDetail) using xml index xidx_details for property

基于Property Index查询,按照相对路径//OrderDetails/row查找相同深度的第二个元素

select OrderID,OrderDetail.query('//OrderDetails/row[Product_ID][2]') from orders

显示结果:

<row>
  <Product_ID>L02</Product_ID>
  <Quantity>300</Quantity>
</row>

 
  •  Value Index

--在primary index基础上,创建value index
create xml index xidx_details_value on orders(OrderDetail) using xml index xidx_details for value

--基于value index查询,按照相对路径//OrderDetails/row查找Product_ID="L01"的元素
select OrderID,OrderDetail.query('//OrderDetails/row[Product_ID="L01"]') from orders

显示结果:

<row>
  <Product_ID>L01</Product_ID>
  <Quantity>200</Quantity>
</row>

 详细介绍如下:

declare @mydoc xml

set @mydoc='
<ROOT>
<AAA>
  <BBB ID="1">二层第一个B</BBB>
  <BBB ID="2">二层第二个B</BBB>
  <CCC>
    <DDD>  
    <BBB ID="3">四层第一个B</BBB>
    </DDD>
    <BBB ID="4">三层第一个B</BBB>
    <BBB ID="5">三层第二个B</BBB>
   </CCC>
   <DDD>100</DDD>
   <DDD>200</DDD>
</AAA>

<AAA>
   <DDD>300</DDD>
   <DDD>400</DDD>
</AAA>
</ROOT>

'

--绝对路径,按照ROOT/AAA/BBB路径查找
select @mydoc.query('/ROOT/AAA/BBB')

 显示结果

<BBB ID="1">二层第一个B</BBB>
<BBB ID="2">二层第二个B</BBB>

--相对路径,按照所有BBB路径查找
select @mydoc.query('//BBB')

 显示结果

<BBB ID="1">二层第一个B</BBB>
<BBB ID="2">二层第二个B</BBB>
<BBB ID="3">四层第一个B</BBB>
<BBB ID="4">三层第一个B</BBB>
<BBB ID="5">三层第二个B</BBB>

 

--相同深度的第一个BBB
select @mydoc.query('//BBB[1]') 

 显示结果 

<BBB ID="1">二层第一个B</BBB>
<BBB ID="3">四层第一个B</BBB>
<BBB ID="4">三层第一个B</BBB>

--绝对路径,按照ROOT/AAA/BBB路径查找的第一个BBB
select @mydoc.query('/ROOT/AAA/BBB[1]')

显示结果:

<BBB ID="1">二层第一个B</BBB>

--绝对路径,按照ROOT/AAA/BBB路径查找的最后一个BBB
select @mydoc.query('/ROOT/AAA/BBB[last()]') 

显示结果:

<BBB ID="2">二层第二个B</BBB>

--相对路径,按照所有BBB路径查找ID=4的BBB(针对Attribute)
select @mydoc.query('//BBB[@ID="4"]')

显示结果:

<BBB ID="4">三层第一个B</BBB>

--绝对路径,按照所有BBB路径查找DDD=300的整棵树信息(针对Element)
select @mydoc.query('/ROOT/AAA[DDD=300]')

 显示结果:

<AAA>
  <DDD>300</DDD>
  <DDD>400</DDD>
</AAA>

其中,/表示绝对路径,/AAA/BBB可以找到,但是路径不完整,则找不到例如/BBB

       //表示相对路径,会自动补全所有上级路径,例如//BBB可以找到所有****/BBB路径