SQL - 大型XML文件在导入时最大化TEMPDB

时间:2016-10-04 14:54:11

标签: sql xml cross-apply

我有一个查询,可以很好地将XML数据引入SQL表(通常)。我当前的问题是我尝试将此查询与更大的XML文件一起使用,并且它最大化了我的tempdb(和磁盘空间)并且失败了。有人可以帮忙吗?

    ;WITH XMLNAMESPACES(DEFAULT 'http://www.adc.ca/ETS/v3')
    INSERT INTO [CC-DB].[dbo].[TourTimeLogs] ([Rig],[Job],[Date],[Sheet],[Rev],[TourID],[StartTime],[EndTime],[Mins],[Code],[Detail])
    SELECT 
        RIG = XC.value('(../../../../../../Rig/RigId)[1]', 'varchar(100)'),
        JOB = XC.value('(../../../../../../JobNo)[1]', 'varchar(100)'),
        DATE = XC.value('(../../../../Date)[1]', 'date'),
        SHEET = XC.value('(../../../../SheetNo)[1]', 'varchar(100)'),
        REV = XC.value('(../../../../Revision)[1]', 'varchar(100)'),
        TOURID = XC.value('(../../@tourId)[1]', 'varchar(100)'),
        STARTTIME = XC.value('(FromTime)[1]', 'datetime'),  
        ENDTIME = XC.value('(ToTime)[1]', 'datetime'),
        MINS = datediff(minute, XC.value('(FromTime)[1]', 'datetime'), XC.value('(ToTime)[1]', 'datetime')),
        TIMECODE = XC.value('(TimeCodeNo)[1]', 'varchar(100)'),
        DETAIL = XC.value('(Detail)[1]', 'varchar(100)')    
    FROM 
         [CC-DB].[dbo].[XmlSourceTable] SRC
    CROSS APPLY
        SRC.XmlData.nodes('/ETS/WellTours/WellTour/DayTours/DayTour/Tours/Tour/TimeLogs/TimeLog') AS XT(XC)

1 个答案:

答案 0 :(得分:1)

如果没有实际的XML,那就是阅读魔术玻璃灯泡,但我会尝试一下:

致电

.nodes('/ETS/WellTours/WellTour/DayTours/DayTour/Tours/Tour/TimeLogs/TimeLog')

告诉我,有一个XML具有根节点 ETS 和嵌套的 1:n-structure WellTours DayTours Tours TimeLogs

您可以尝试这样的事情:

;WITH XMLNAMESPACES(DEFAULT 'http://www.adc.ca/ETS/v3')
INSERT INTO [CC-DB].[dbo].[TourTimeLogs] ([Rig],[Job],[Date],[Sheet],[Rev],[TourID],[StartTime],[EndTime],[Mins],[Code],[Detail])
SELECT 
    RIG = WT.value('(Rig/RigId)[1]', 'varchar(100)'),
    JOB = WT.value('(JobNo)[1]', 'varchar(100)'),
    [DATE] = DT.value('(Date)[1]', 'date'),
    SHEET = DT.value('(SheetNo)[1]', 'varchar(100)'),
    REV = DT.value('(Revision)[1]', 'varchar(100)'),
    TOURID = T.value('@tourId', 'varchar(100)'),
    STARTTIME = TL.value('(FromTime)[1]', 'datetime'),  
    ENDTIME = TL.value('(ToTime)[1]', 'datetime'),
    MINS = datediff(minute, TL.value('(FromTime)[1]', 'datetime'), TL.value('(ToTime)[1]', 'datetime')),
    TIMECODE = TL.value('(TimeCodeNo)[1]', 'varchar(100)'),
    DETAIL = TL.value('(Detail)[1]', 'varchar(100)')    
FROM 
     [CC-DB].[dbo].[XmlSourceTable] SRC
CROSS APPLY
    SRC.XmlData.nodes('/ETS/WellTours/WellTour') AS A(WT)
CROSS APPLY
    WT.nodes('DayTours/DayTour') AS B(DT)
CROSS APPLY
    DT.nodes('Tours/Tour') AS C(T)
CROSS APPLY
    T.nodes('TimeLogs/TimeLog') AS D(TL)

关注如果没有返回任何内容,请尝试使用OUTER APPLY。没有真正的XML来测试它,它是盲目飞行 ......

如此阅读:

  • 查找所有<WellTour> - 元素
  • 查找所有<DayTour> - 每个<WellTour>下面的元素
  • 查找所有<Tour> - 每个<DayTour>下面的元素
  • 查找所有<TimeLog> - 每个<Tour>下面的元素

从实际级别读取数据。不要向后跳。 XML最好是仅向前(如果可能)。