如何使此 xml 查询更快

How to make this xml query faster

提问人:user2275634 提问时间:10/23/2023 最后编辑:Yitzhak Khabinskyuser2275634 更新时间:10/23/2023 访问量:40

问:

我有这个疑问:

DECLARE @DatatypenNEN3610 NVARCHAR(256)
DECLARE @Objecten NVARCHAR(256)

SET @DatatypenNEN3610 = '''http://www.kadaster.nl/schemas/lvbag/imbag/datatypennen3610/v20200601'' as DatatypenNEN361'
SET @Objecten = ''' http://www.kadaster.nl/schemas/lvbag/imbag/objecten/v20200601'' as Objecten'

DECLARE @Filepath varchar(100)

SET @Filepath = 'C:\Test\Test.xml'

DECLARE @sql NVARCHAR(MAX);

SET @sql = '
;WITH XMLNAMESPACES ('+ @DatatypenNEN3610 +' , '+ @Objecten +')

select
    r.x.value(''*:identificatie[1]'', ''Nvarchar(50)'') AS Identifictie,
    r.x.value(''*:oorspronkelijkBouwjaar[1]'',''Nvarchar(10)'') As Bouwjaar
FROM (  
        SELECT CAST(MY_XML AS xml)  
        FROM OPENROWSET(BULK ''' + @Filepath + ''', SINGLE_BLOB) AS T(MY_XML)  
    ) AS T(MY_XML)

CROSS APPLY t.MY_XML.nodes(''/*:bagStand/*:standBestand/*:stand/*:bagObject/*:Pand'') r ( x )
';

exec sp_executesql @sql;

它工作正常。但它非常慢。有人可以帮我骗人让它更快吗?

sql-server xml 性能 xquery

评论

0赞 jarlh 10/23/2023
您使用的是哪些 dbms?(上面的代码非常特定于产品。
0赞 SelVazi 10/23/2023
请编辑您的问题并标记您正在使用的 RDBMS
0赞 Yitzhak Khabinsky 10/23/2023
请编辑您的问题,并提供 XML 文件的示例。另外,XML文件的大小是多少?
0赞 user2275634 10/23/2023
我正在使用 SQL Server Express 2022

答:

0赞 Yitzhak Khabinsky 10/23/2023 #1

了解如何有效地粉碎 XML。

SQL算法

/*
Step #1: Load XML file into single row db table
*/
DECLARE @tbl TABLE (XMLColumn XML);
DECLARE @Filepath VARCHAR(100) = 'C:\Test\Test.xml';

DECLARE @sql NVARCHAR(MAX) = 
N'SELECT BulkColumn 
   FROM Openrowset(Bulk ' + QUOTENAME(@Filepath, NCHAR(39)) + ', Single_Blob) x;';

PRINT @sql;

INSERT INTO @tbl (XMLColumn)
EXEC sys.sp_executesql @sql;

-- just to see
SELECT * FROM @tbl;

/*
Step #2: Shred XML without dynamic SQL
*/
WITH XMLNAMESPACES (/* we need to see your XML sample to declare and use XML namespaces*/)
SELECT c.value('(*:identificatie/text())[1]', 'NVARCHAR(50)') AS Identifictie,
    c.value('(*:oorspronkelijkBouwjaar/text())[1]', 'NVARCHAR(10)') As Bouwjaar
FROM @tbl
CROSS APPLY XMLColumn.nodes('/*:bagStand/*:standBestand/*:stand/*:bagObject/*:Pand') AS t(c);

评论

0赞 user2275634 10/23/2023
谢谢。它@Yitzhak哈宾斯基的工作速度非常非常快