为XML内联函数优化此SQL查询

时间:2014-10-12 11:47:45

标签: sql tsql

这是查询。

Select MAX(DatasourceConnectionID) as DatasourceConnectionID, ConnectionDescription
from
    (

    SELECT meta.DatasourceConnection.DatasourceConnectionID 
    ,CASE WHEN (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails))='SQL'
        THEN (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' TO [' + (Select top 1 ServerName from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + '].[' + (Select top 1 DatabaseName from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ']'
     WHEN (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails))='EXCEL'
        THEN (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' (last workshet ' + (Select top 1 WorksheetName from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ')'
     ELSE 
        (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' ' + isnull((Select top 1 FileName from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)),'')
     END AS ConnectionDescription                                                                   
    from meta.DatasetLoad inner join meta.DatasourceConnection 
            on meta.DatasetLoad.DatasourceConnectionID = meta.DatasourceConnection.DatasourceConnectionID inner join
            meta.DataSource on meta.DatasourceConnection.DataSourceID = meta.DataSource.DataSourceID inner join
            meta.Connection on meta.DatasourceConnection.ConnectionID = meta.Connection.ConnectionID
    where DatasetID = 1156 and DatasetLoadStatusID = 5 and NOT meta.DatasourceConnection.DatasourceConnectionID in
     (Select Distinct DatasourceConnectionID from meta.DatasetLoadSchedule where DatasetID = 1156))Data
    group by ConnectionDescription  

    UNION ALL
    SELECT meta.DatasourceConnection.DatasourceConnectionID 
    ,CASE WHEN (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails))='SQL'
        THEN (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' TO [' + (Select top 1 ServerName from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + '].[' + (Select top 1 DatabaseName from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ']'
     WHEN (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails))='EXCEL'
        THEN (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' (last workshet ' + (Select top 1 WorksheetName from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ')'
     ELSE 
        (Select top 1 ConnectionType from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' ' + isnull((Select top 1 FileName from meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)),'')
     END AS ConnectionDescription                                                                   
    from meta.DatasetLoadSchedule inner join meta.DatasourceConnection 
            on meta.DatasetLoadSchedule.DatasourceConnectionID = meta.DatasourceConnection.DatasourceConnectionID inner join
            meta.DataSource on meta.DatasourceConnection.DataSourceID = meta.DataSource.DataSourceID inner join
            meta.Connection on meta.DatasourceConnection.ConnectionID = meta.Connection.ConnectionID
    where DatasetID = 1156

函数GetConnectionFromXML从xml字符串返回一个表。

FUNCTION [meta].[GetConnectionFromXML] 
(   
    @ConnectionXML xml
)
RETURNS @Connection table
(
  ConnectionType nvarchar(20),
  ServerName nvarchar(100),
  DatabaseName nvarchar(100),
  ConnectionString nvarchar(500),
  Query nvarchar(max),
  FileName nvarchar(1000),
  WorksheetName nvarchar(50),
  Delimiter nvarchar(5),
  HeaderRow tinyint,
  TextQualifier nchar(1),
  TextQualifierIsOptional bit,
  RequiresCleaning bit
)
AS
BEGIN
    INSERT INTO @Connection
    SELECT
        cn.c.value('ConnectionType[1]', 'nvarchar(20)') AS ConnectionType,
        cn.c.value('ServerName[1]', 'nvarchar(100)') AS ServerName,
        cn.c.value('DatabaseName[1]', 'nvarchar(100)') AS DatabaseName,
        cn.c.value('ConnectionString[1]', 'nvarchar(500)') AS ConnectionString,
        cn.c.value('Query[1]', 'nvarchar(max)') AS Query,
        cn.c.value('FileName[1]', 'nvarchar(1000)') AS FileName,
        cn.c.value('WorksheetName[1]', 'nvarchar(50)') AS WorksheetName,
        cn.c.value('Delimiter[1]', 'nvarchar(5)') AS Delimiter,                     
        cn.c.value('HeaderRow[1]', 'tinyint') AS HeaderRow,                     
        cn.c.value('TextQualifier[1]', 'nchar(1)') AS TextQualifier,                        
        cn.c.value('TextQualifierIsOptional[1]', 'bit') AS TextQualifierIsOptional,
        cn.c.value('RequiresCleaning[1]', 'bit') AS RequiresCleaning
    FROM 
        @ConnectionXML.nodes('//Connection') As cn(c) 

    RETURN
END

1 个答案:

答案 0 :(得分:0)

我刚使用子查询来最小化连接记录。这使得时间从1分钟减少到仅仅4秒。