Sql Server Xml 列最佳实践
使用 Sql server Xml 列以确保快速性能和轻松报告的最佳实践是什么?
专栏如何设置? 你把它保留为无类型吗? 或将其与模式相关联?
将 xml 列与架构关联是否可以提高查询性能?
我们对xml列的使用如下:
A.>在每个客户的基础上,我们可以定义其数据的灵活存储,而无需彻底修改我们的数据库。
B.>我们需要为每个客户构建报告视图,将其数据返回为一个简单的表格(对于水晶报告或 Sql Server Reporting Services)。
我们目前用来查询的语法如下:
SELECT
Id,
doc.value('@associatedId','nvarchar(40)') as AssocId,
doc.value('@name1', 'nvarchar(255)') as Name1,
doc.value('@name2', 'nvarchar(255)') as Name2,
doc.value('@name3', 'nvarchar(255)') as Name3,
doc.value('@number', 'nvarchar(255)') as Number
From OrderDetails
CROSS APPLY OrderDetails.XmlData.nodes('//root/reviewers/reviewer') as XmlTable(doc)
有没有更快的方法来做到这一点?对于我们来说,这个查询在一个有 100 万条记录的表中运行得很慢,但目前只有 800 条记录有 xml 数据!
谢谢皮特
What are the best practices for working with Sql server Xml columns to ensure quick performance and also ease of reporting?
How do you set up the column?
do you leave it as untyped?
or associate it with a schema?
Does associating the xml column with a schema improve query performance?
Our use of xml columns is as follows:
A.> On a PER customer basis we can define flexible storage of their data without overhauling our db.
B.> We need to build reporting views for each customer which returns their data as if it was a simple table (for crystal reports or Sql Server Reporting Services).
The syntax we currently use to query is as follows:
SELECT
Id,
doc.value('@associatedId','nvarchar(40)') as AssocId,
doc.value('@name1', 'nvarchar(255)') as Name1,
doc.value('@name2', 'nvarchar(255)') as Name2,
doc.value('@name3', 'nvarchar(255)') as Name3,
doc.value('@number', 'nvarchar(255)') as Number
From OrderDetails
CROSS APPLY OrderDetails.XmlData.nodes('//root/reviewers/reviewer') as XmlTable(doc)
Is there a quicker way to do this? this query runs slowly for us in a table with 1million records, but only 800 currently have xml data!
Thanks
Pete
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自Microsoft SQL Server 2005 的 XML 最佳实践 :
使用类型化或非类型化 XML?
将 xml 列与架构关联是否可以提高查询性能?请参阅上面的观点:如果您想利用基于类型信息的查询优化,请使用类型化 XML。
对于 XML 索引的优点也有很长的讨论:
最重要的是,适合您使用的辅助 XML 索引类型:
From XML Best Practices for Microsoft SQL Server 2005:
Use a typed or untyped XML?
Does associating the xml column with a schema improve query performance? See above point: use typed XML if you want to take advantage of query optimizations based on type information.
There is also a lengthy discussion over the benefits of XML indexes:
And most importantly, the appropriate type of secondary XML index for your usage:
如果像上面的示例一样,您使用 XML 来存储各种字符串列,那么我认为您不会真正从类型化 XML 中受益,除非您需要服务器来验证数据。就性能而言,我怀疑非类型化会更快。
对于这些类型的查询,您绝对需要有适当的 XML 索引,它们对于 XML 查询的良好性能至关重要。如果没有索引,XML 列将存储为 blob,因此为了查询它们,SQL 需要首先将 blob 分解为 XML,然后执行您请求的任何操作。主 XML 索引将分解的 XML 存储在数据库中,因此不需要即时完成。您需要首先创建主 XML 索引,然后可以创建辅助 XML 索引来支持您的查询。
二级 XML 索引有 3 种类型:PATH、VALUE 和 PROPERTY。您需要哪些二级索引取决于您要执行的查询类型,因此我鼓励您查看联机丛书中的二级 XML 索引主题,以确定哪些索引对您有用:
http://msdn.microsoft.com/en-us /library/bb522562(SQL.100).aspx
If as in the above example you are using the XML to store various string columns, I don't think you would really benefit from typed XML unless you have a need for the server to validate the data. Performance-wise, I suspect it would be faster untyped.
For these kinds of queries you absolutely need to have XML indexes in place, they are essential for good performance of XML queries. Without indexes, XML columns are stored as blobs so in order to query them, SQL needs to shred the blob into XML first, then do whatever operations you are requesting. A primary XML index stores the shredded XML in the database so it doesn't need to be done on the fly. You need to create a primary XML index first, then secondary XML indexes can be created to support your queries.
There are 3 types of secondary XML indexes: PATH, VALUE and PROPERTY. Which secondary indexes you need depends on the type of queries you're going to be doing, so I would encourage you to review the Secondary XML Indexes topic in Books Online to decide which one(s) would be useful to you:
http://msdn.microsoft.com/en-us/library/bb522562(SQL.100).aspx