在SQL Server 2016中导入非常慢的xml

时间:2017-10-03 18:10:06

标签: sql xml performance tsql sql-server-2016

我在优化以下更新查询时遇到问题

SQL查询

end

我正在尝试导入此XML文件(https://ufile.io/8d9of),但完成导入需要2-3分钟。我怎么能加快速度,因为服务器不是问题(SQL Server 2016,企业ssd,双Xeon cpu和256GB ddr4 ram)

1 个答案:

答案 0 :(得分:1)

XML的加载和解析相当适用...在0.108秒内完成585条记录。我怀疑更新导致了降级。

可能将XML Parse放在CTE中,然后通过JOIN执行更新。

注意我添加了两个字段kID和kFromDateLocal

示例

Declare @XmlFile4 xml
Select @XmlFile4 = BulkColumn FROM  OPENROWSET(BULK 'C:\Working\geobewegingen.xml', SINGLE_BLOB) x;

;WITH XMLNAMESPACES (DEFAULT 'http://www.geodynamics.be/webservices')
,cte as (
Select kID                = resource.value('(../../Vehicle/Id)[1]', 'varchar(255)'),
       kFromDateLocal     = resource.value('(FromDateLocal)[1]', 'varchar(255)'),
       voernaam           = resource.value('(../../Vehicle/Name)[1]', 'varchar(255)'),
       voercode           = resource.value('(../../Vehicle/Code)[1]', 'varchar(255)'),
       pervan             = resource.value('(../../FromDateLocal)[1]', 'varchar(255)'),
       pertot             = resource.value('(../../ToDateLocal)[1]', 'varchar(255)'),
       tripvan            = resource.value('(FromDateLocal)[1]', 'varchar(255)'),
       triptot            = resource.value('(ToDateLocal)[1]', 'varchar(255)'),
       tripvanlocstr      = resource.value('(FromLocation/Address/Street)[1]', 'varchar(255)'),
       tripvanlocnr       = resource.value('(FromLocation/Address/HouseNumber)[1]', 'varchar(255)'),
       tripvanlocpc       = resource.value('(FromLocation/Address/PostalCode)[1]', 'varchar(255)'),
       tripvanloccity     = resource.value('(FromLocation/Address/City)[1]', 'varchar(255)'),
       tripvanlocsub      = resource.value('(FromLocation/Address/Submunicipality)[1]', 'varchar(255)'),
       tripvanloccountry  = resource.value('(FromLocation/Address/Country)[1]', 'varchar(255)'),
       tripvanlong        = resource.value('(FromLocation/Longitude)[1]', 'varchar(255)'),
       tripvanlat         = resource.value('(FromLocation/Latitude)[1]', 'varchar(255)'),
       tripnaarlocstr     = resource.value('(ToLocation/Address/Street)[1]', 'varchar(255)'),
       tripnaarlocnr      = resource.value('(ToLocation/Address/HouseNumber)[1]', 'varchar(255)'),
       tripnaarlocpc      = resource.value('(ToLocation/Address/PostalCode)[1]', 'varchar(255)'),
       tripnaarloccity    = resource.value('(ToLocation/Address/City)[1]', 'varchar(255)'),
       tripnaarlocsub     = resource.value('(ToLocation/Address/Submunicipality)[1]', 'varchar(255)'),
       tripnaarloccountry = resource.value('(ToLocation/Address/Country)[1]', 'varchar(255)'),
       tripnaarlong       = resource.value('(ToLocation/Longitude)[1]', 'varchar(255)'),
       tripnaarlat        = resource.value('(ToLocation/Latitude)[1]', 'varchar(255)'),
       triptype           = resource.value('(Type)[1]', 'varchar(255)'),
       tripkmgereden      = resource.value('(MileageDriven)[1]', 'varchar(255)'),
       tripkmvogelvlucht  = resource.value('(MileageBirdFlight)[1]', 'varchar(255)'),
       tripuserid         = resource.value('(Users/UserEntity/Id)[1]', 'varchar(255)'),
       tripusername       = resource.value('(Users/UserEntity/Name)[1]', 'varchar(255)')
 From @XmlFile4.nodes('//TripOverview_GetByVehicleIdListDateResponse/TripOverview_GetByVehicleIdListDateResult/TripOverviewEntity/TripItemEntities/TripItemEntity') AS XTbl1(resource)
) 
Update A
   set voernaam           = B.voernaam
      ,voernaam           = B.voernaam
      ,voercode           = B.voercode
      ,pervan             = B.pervan
      ,pertot             = B.pertot
      ,tripvan            = B.tripvan
      ,triptot            = B.triptot
      ,tripvanlocstr      = B.tripvanlocstr
      ,tripvanlocnr       = B.tripvanlocnr
      ,tripvanlocpc       = B.tripvanlocpc
      ,tripvanloccity     = B.tripvanloccity
      ,tripvanlocsub      = B.tripvanlocsub
      ,tripvanloccountry  = B.tripvanloccountry
      ,tripvanlong        = B.tripvanlong
      ,tripvanlat         = B.tripvanlat
      ,tripnaarlocstr     = B.tripnaarlocstr
      ,tripnaarlocnr      = B.tripnaarlocnr
      ,tripnaarlocpc      = B.tripnaarlocpc
      ,tripnaarloccity    = B.tripnaarloccity
      ,tripnaarlocsub     = B.tripnaarlocsub
      ,tripnaarloccountry = B.tripnaarloccountry
      ,tripnaarlong       = B.tripnaarlong
      ,tripnaarlat        = B.tripnaarlat
      ,triptype           = B.triptype
      ,tripkmgereden      = B.tripkmgereden
      ,tripkmvogelvlucht  = B.tripkmvogelvlucht
      ,tripuserid         = B.tripuserid
      ,tripusername       = B.tripusername
 From  GeoBewegingen A
 Join  cte B on A.id = B.kID and A.tripvan = B.kFromDateLocal