简体   繁体   English

SQL Server Xml列查询性能?

[英]Sql Server Xml Column query performance?

I'm trying to query an xml column in sql server. 我正在尝试查询sql server中的xml列。 I've created a primary index on the column and query it using: 我在该列上创建了一个主索引,并使用以下查询它:

SELECT *
FROM MyTable
where  Doc.exist('/xml/root/propertyx/text()[. = "something"]') = 1

In a table with 60 000 entries , this query takes some 100 ms on my local dev machine. 在具有60 000个条目的表中,此查询在我的本地dev机器上花费大约100毫秒。 Is it possible to optimize this somehow to increase performance of the query? 是否有可能以某种方式优化它以提高查询性能?

You can optimize for fast query times with a calculated column. 您可以使用计算所得的列来优化快速查询时间。 A calculated column can't use the XML functions directly, so you have to wrap them in a function: 计算列不能直接使用XML函数,因此必须将它们包装在函数中:

go
create function dbo.GetSomethingExists(
    @Doc xml)
returns bit
with schemabinding
as begin return (
     select  @Doc.exist('/xml/root/property/text()[. = "something"]')
) end
go
create table TestTable (
    Doc xml,
    SomethingExists as dbo.GetSomethingExists(Doc) persisted
)
go

If you declare the function with schemabinding , you can create an index on SomethingExists: 如果使用schemabinding声明函数, schemabinding可以在SomethingExists上创建索引:

create index IX_TestTable_SomethingExists on TestTable(SomethingExists)

This should make the query much faster. 这应该使查询更快。

创建Path类型的辅助XML索引可能会为您加快速度。

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

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