1

我的 XML 结构:

<Items>
  <Item>
    <guid>FC550573-7171-997F-752D-8D65590CBFD6</guid>
    <Objects>
       <Object>
         <type>0</type>
         <guid>E10D9DA9-2C8D-8024-2F07-DF21395811BF</guid>
       </Object>
       <Object>
         <type>0</type>
         <guid>D8338400-35C7-781E-A039-C0FDDF80714A</guid>
       </Object>
    </Objects>
  </Item>
</Items>

填写对象表时:

CREATE TABLE [dbo].[Objects](
    [item_guid] [varchar](36) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [type] [int] NOT NULL,
    [guid] [varchar](36) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

使用查询:

INSERT INTO [dbname].[dbo].[Objects]
           ([item_guid]
           ,[type]
           ,[guid])
SELECT
 X.source.query('../../guid').value('.','VARCHAR(36)') as item_guid,
 X.source.query('type').value('.','INT') as type,
 X.source.query('guid').value('.','VARCHAR(36)') as guid
FROM(
Select xmldata from XmlFiles where fullpath=@fp
) AS T(x)
CROSS APPLY x.nodes('Items/Item/Objects/Object') As X(source)

此行使查询非常慢:

X.source.query('../../guid').value('.','VARCHAR(36)') as item_guid

这里的正确方法是什么?

4

3 回答 3

8

使用/text()来获取值对无类型 XML 的性能有好处。使用父轴也可能不好../.. (正如@marc_s 建议的那样)。

这是一个带有额外交叉应用并/text()获取值的版本。

尝试这个:

select T2.N.value('(guid/text())[1]', 'uniqueidentifier') as item_guid,
       T3.N.value('(type/text())[1]', 'int') as type,
       T3.N.value('(guid/text())[1]', 'uniqueidentifier') as guid
from (SELECT xmldata FROM dbo.XmlFiles WHERE fullpath = @fp) as T1(N)
  cross apply T1.N.nodes('Items/Item') as T2(N)
  cross apply T2.N.nodes('Objects/Object') as T3(N)

您必须判断哪个查询对您来说最快。

于 2012-11-08T14:37:10.643 回答
3

我只想补充一点,以防其他人遇到这种情况,添加以下选项会产生巨大的影响。

OPTION (OPTIMIZE FOR (@testXml = NULL))

如果您想自己测试,这里有一个我正在运行的简短测试脚本。只需查看这些之间的估计子树成本。

declare @testXml xml set @testXml = '<filters><filter name="test name" type="GREATERTHAN">1</filter><filter name="CLAIMID" type="GREATERTHAN">1</filter></filters>'


select x.value('@name','nvarchar(100) ') filtername, 
x.value('.','nvarchar(200)')filtervalue, 
x.value('@type','nvarchar(50) ') filtertype 
from @testXml.nodes('/filters/filter') as ref(x)
--vs...
select x.value('@name','nvarchar(100) ') filtername,  
x.value('.','nvarchar(200)')filtervalue,  
x.value('@type','nvarchar(50) ') filtertype 
from @testXml.nodes('/filters/filter') as ref(x) 
OPTION (OPTIMIZE FOR (@testXml = NULL))
于 2015-05-29T16:37:39.130 回答
1

尝试这个,

我们将创建一个临时表变量来存储这个 xml 值并插入到相应的表对象中

//..Xml value to temp variable
Declare @x xml ='<Items><Item><guid>FC550573-7171-997F-752D-8D65590CBFD6</guid><Objects><Object>
                 <type>0</type><guid>E10D9DA9-2C8D-8024-2F07-DF21395811BF</guid></Object><Object>
                 <type>0</type><guid>D8338400-35C7-781E-A039-C0FDDF80714A</guid></Object></Objects>
                 </Item></Items>';

Declare @Temp_Tbl table (RowId int identity, item_guid nvarchar(36), [type] int, [guid] nvarchar(36));

Insert into @Temp_Tbl SELECT @x.value('(/Items/Item/guid)[1]', 'nvarchar(36)'),
   Cont.value('(type)[1]', 'int'),  Cont.value('(guid)[1]', 'nvarchar(36)')                                                                                     
   FROM @x.nodes('/Items/Item/Objects/Object') AS Obj(Cont);

INSERT INTO [dbo].[Objects] Select item_guid,[type],[guid] from @Temp_Tbl;
于 2012-11-08T14:13:51.993 回答