如何使SQL更快地解析此XML

时间:2018-09-19 06:30:44

标签: sql xml

下面的代码可以工作,但是需要16分钟才能解析SQL中36,000多个行中的XML。有没有一种方法可以更改以下代码以使其更快地解析XML?我已经更新了下面的代码。

select c.claimid, c.adjuddate as lastadjudication, cast(claimedithistory as xml) as xmldata, ca.adjudicationattempts into #kb from claimedithistory ca (nolock) join claim c (nolock) on ca.claimid = c.claimid  where adjuddate > dateadd(d, 1, eomonth(getdate(), -3)) order by c.claimid

alter table #kb add primary key (claimid, adjudicationattempts)

create table #kb2 (claimid char(15) not null,lastadjudication Date not null,attempt int not null, id char(60) null, adjuddate date null)


Declare @claimid char(15)
Declare @lastadjudication as date
declare @xmldata as xml
declare @adjudicationattempts as int
declare @nextadjudication as int
set @nextadjudication = 1

SET NOCOUNT ON

DECLARE db_cursor CURSOR FOR 
SELECT claimid, lastadjudication, xmldata, adjudicationattempts 
FROM #kb order by claimid desc

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @claimid, @lastadjudication, @xmldata, @adjudicationattempts  

WHILE @@FETCH_STATUS = 0  
BEGIN  

    set @nextadjudication = 1
    WHILE @nextadjudication <= @adjudicationattempts
            BEGIN

                insert into #kb2 (claimid, lastadjudication, attempt, id, adjuddate)
                select claimid, lastadjudication,xmldata.value('(ClaimEditHistory[sql:variable("@nextadjudication")]/@attempt)[1]', 'int')  as attempt, 
                xmldata.value('(ClaimEditHistory[sql:variable("@nextadjudication")]/@userid)[1]', 'varchar(15)')  as id,
                xmldata.value('(ClaimEditHistory[sql:variable("@nextadjudication")]/@datetime)[1]', 'datetime')  as adjuddate 
                from #kb 
                where claimid = @claimid
                    and xmldata.value('(ClaimEditHistory[sql:variable("@nextadjudication")]/@datetime)[1]', 'datetime') > dateadd(d, 1, eomonth(getdate(), -3))
                    and xmldata.value('(ClaimEditHistory[sql:variable("@nextadjudication")]/@userid)[1]', 'varchar(15)') <> 'hsnprbatch'
                    and xmldata.value('(ClaimEditHistory[sql:variable("@nextadjudication")]/@userid)[1]', 'varchar(15)') <> 'TorresM'
                set @nextadjudication = @nextadjudication + 1
            END

      FETCH NEXT FROM db_cursor INTO @claimid, @lastadjudication, @xmldata, @adjudicationattempts
END 

CLOSE db_cursor  
DEALLOCATE db_cursor

select * from #kb2 

1 个答案:

答案 0 :(得分:0)

为什么不创建一个SSIS包,在需要时以3-4个部分(而不是一个部分)运行它(而不是一个),并将其转储到SQL表中?然后只需调用该表,它将比尝试在这样的查询中完成所有操作要快得多。