简体   繁体   English

SQL Server中的XML数据列

[英]XML Data Column in SQL Server

Using SQL Server 2008 R2 使用SQL Server 2008 R2

I'd like to create a table with the following columns 我想用以下各列创建一个表

[id] INT IDENTITY(1,1) NOT NULL,
[user_id] INT NOT NULL,
[date] DATE NOT NULL,
[timestamp] DATETIME NOT NULL,
[xml_data] XML NOT NULL

with the primary key on the identity column and a non-clustered index on user_id and date that covers xml_data and timestamp. 在标识列上具有主键,在user_id上具有非聚集索引,并且日期涵盖xml_data和时间戳。

However, I notice that I can't add xml_data to the INCLUDE statement in the index. 但是,我注意到我无法将xml_data添加到索引中的INCLUDE语句中。 Sad face, since that's going to result in an RID lookup when a user searches on user_id and date. 悲伤的表情,因为当用户搜索user_id和date时,这将导致RID查找。

What's the best way to store xml data that will be queried? 存储要查询的xml数据的最佳方法是什么?

I figure my choices are 我认为我的选择是

  1. Stick with xml and have well-formatted data but take the query hit 坚持使用xml并拥有格式正确的数据,但要查询命中
  2. Use a VARCHAR(MAX) with unknown pros/cons 使用具有未知优缺点的VARCHAR(MAX)
  3. Use a VARBINARY(MAX) with unknown pros/cons 使用具有未知优缺点的VARBINARY(MAX)

Note: I doubt I'll be able to restrict the length of the string to even something like 8000. 注意:我怀疑是否可以将字符串的长度限制为甚至8000。

If you have XML - store it as XML , for two main reasons: 如果您有XML-将其存储为XML ,原因有两个:

  • it's optimized for XML storage - it's not stored as just plain text, it's actually tokenized and stored more efficiently than plain text 它针对XML存储进行了优化-它不仅存储为纯文本,而且实际上是标记化的,比纯文本更有效地存储

  • you can actually query the XML when it's stored as type XML 当将XML存储为XML类型时,您实际上可以查询 XML

But: you cannot just index a XML column like that. 但是:您不能仅像这样索引XML列。 Any index in SQL Server can be a maximum of 900 bytes long - an XML column could be up to 2 GB in size. SQL Server中的任何索引最大长度为900字节-XML列的大小最大为2 GB。

If you want to index your XML column, have a look at XML Indexes in SQL Server 2005 - it's a separate type of index designed to handle queries into XML very efficiently. 如果要对XML列建立索引,请查看SQL Server 2005中的XML索引 -它是一种单独的索引类型,旨在非常高效地处理对XML的查询。

Another way to speed up your XML queries could be to "surface" certain pieces of your XML that you query on often onto the parent table, by means of a stored function that extract that piece of information from the XML, and stores it as a computed persisted column on the parent table. 加快XML查询速度的另一种方法是,通过存储函数从XML中提取信息并将其存储为父表,从而将您经常查询的XML某些部分“浮现”到父表上。计算父表上的持久化列。 Once it's stored there, you can query it just like any other column, and you can index it, too! 一旦将其存储在此处,您就可以像查询其他任何列一样对其进行查询,也可以对其进行索引! It only works for single pieces of information, however (eg the OrderNumber from your order - you only ever have one of those) - it can't be applied to collections of data. 但是,它仅适用于条信息(例如,订单中的OrderNumber您只能拥有其中之一)-不能将其应用于数据集合。

You can use XQuery to querying xml fields. 您可以使用XQuery查询xml字段。 See here . 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM