是否可以通过 linq 进行 XQuery?

发布于 2024-11-29 04:25:39 字数 192 浏览 3 评论 0原文

我有一个 XML 字段,每条记录大约有 5MB 的数据,但有时,我只需要读取 XML 字段的一小部分。您可以想象,如果我读取整个 XML 字段,然后使用 Linq-to-XML 解析 XML 文件并收集值,那么速度会太慢且成本高昂。所以我想知道,是否可以直接使用 Linq 获取值而不是读取整个 XML 字段?

我的数据库是SQL Server 2008

I have an XML field which has about 5MB of data every record, but sometimes, I only need to read a small part of the XML field. As you can imagine, if I read the whole XML field and then using Linq-to-XML to parse the XML file and gather the value, it would be too slow and expensive. So I want know, is it possible to get a value directly using Linq instead of read the whole XML field?

My DB is SQL Server 2008

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2024-12-06 04:25:39

根据目前提供的信息,我认为最好的解决方案是在 SQL Server 中使用 XML 索引。

XML 索引有四种类型:

  1. 主要
  2. 辅助 PATH
  3. 辅助 PROPERTY
  4. 辅助 VALUE

在您的情况下,您似乎知道所需数据的路径,自然辅助 PATH 索引似乎是最合适的。

按照以下步骤创建此索引:

创建主索引

create primary xml index XIX_XmlColumnName on XmlTable(XmlColumnName)
go

这将为您的 xml 列创建“基本”索引,基本上这意味着 xml 将被分解到一个隐藏表中并存储每个值元素变成一行。

创建辅助路径索引

create xml index XIX_XmlColumnName_Path on XmlTable(XmlColumnName)
using xml index XIX_XmlColumnName for path
go

这将使用主索引(我们现在知道是一个表)中的路径列创建辅助索引。

最后,在过程中运行一个(sql)查询,并从您的应用程序中调用它:

select XmlColumnName.query('/path/to/element')
from XmlTable

当然,这不是 linq 查询/解决方案,但在我看来,最好使用适合的工具,而不是尝试强制执行。

有关 xml 索引的更深入信息,请参阅这篇 msdn 文章

With the current information provided I think the best solution is to use an XML index in SQL Server.

There are four types of XML indexes:

  1. Primary
  2. Secondary for PATH
  3. Secondary for PROPERTY
  4. Secondary for VALUE

In your case it appears you know the path to the data you want, naturally a secondary PATH index seems to be the best fit.

Follow these steps to create this index:

Create primary index

create primary xml index XIX_XmlColumnName on XmlTable(XmlColumnName)
go

This will create the "base" index for your xml column, basically this means that the xml will be shredded to a hidden table and stored with values where every element is turned into one row.

Create secondary path index

create xml index XIX_XmlColumnName_Path on XmlTable(XmlColumnName)
using xml index XIX_XmlColumnName for path
go

This will create a secondary index using the path-column in the primary index (which we now know is a table).

Finally, run a (sql) query such as this in a procedure and call that from your application:

select XmlColumnName.query('/path/to/element')
from XmlTable

Granted, this is not a linq-query/solution, but imo it's always best to use a tool that fits, and not try to force it.

For more in-depth information about xml indexes, see this msdn-article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文