Sql 使用视图时减少运行时构造查询的执行时间

Sql 使用视图时减少运行时构造查询的执行时间,sql,sql-server,Sql,Sql Server,我使用了一个查询,我只知道在运行时检索哪些变量。我使用一个视图将所有变量拉入一个数据集中,然后对该数据集进行查询。它看起来像这样: All variables in the SQL db: Table A.1, Table A.2, Table A.3 Table B.1, Table B.2, Table B.3 Table C.1, Table C.2, Table C.3 在运行时,我知道我需要变量表A.1和表A.2,其中表A.1=SomeNumber。因此,我创建了一个视图: 视图1=

我使用了一个查询,我只知道在运行时检索哪些变量。我使用一个视图将所有变量拉入一个数据集中,然后对该数据集进行查询。它看起来像这样:

All variables in the SQL db:
Table A.1, Table A.2, Table A.3
Table B.1, Table B.2, Table B.3
Table C.1, Table C.2, Table C.3
在运行时,我知道我需要变量表A.1和表A.2,其中表A.1=SomeNumber。因此,我创建了一个视图:

视图1=表A.1、表B.1

然后我查询表A.1=SomeNumber的视图

这种方法似乎有效,但返回结果需要30秒的时间,因为我有20多个表和100多个变量。有2K+条记录要查询

关于如何提高此查询的响应时间,有什么想法吗

*******下面是实际的查询**********

USE [ays_restructuring_league_live];

GO

ALTER PROCEDURE [dbo].[sp_getVolunteerSummaryDetails]

(@LeagueId int, @p_SearchCriteria varchar(MAX), @p_DataflowId int=null)
WITH 
EXECUTE AS CALLER
AS
    Begin
    SET NOCOUNT ON;
    DECLARE @Statement AS varchar(MAX);
    Declare @p_DataFieldName as varchar(max);
    declare @p_Label as varchar(max);

if (CHARINDEX('DisplayId = ''1'' ',@p_SearchCriteria)>0)/* for multiple record*/  
    begin
            Select b.* into #finalTable from(
            SELECT distinct VSI.VolunteerSeasonalId,
                VI.VolunteerId,
                right(MUS.DetailIdURL_Structure + cast(convert(varchar(max),VI.VolunteerId) as varchar(max)),MUS.DetailIdURL_Length)  as NewVolunteerId,
                right(MUS.OtherURL_Structure + cast(convert(varchar(max),VSI.SeasonId) as varchar(max)),MUS.OtherURL_Length)  as NewSeasonId,            
                UI.FirstName as VolunteerFirstName,
                UI.LastName as VolunteerLastName,
                UI.LastName+ ', '+ UI.FirstName as VolunteerName,
                UI.Email as VolunteerEmail,
                VI.ShirtSizeId,
                MUSZ.Size as ShirtSize,
                UI.HomePhone as VolunteerHomePhone,
                UI.MobilePhone as VolunteerMobilePhone,
                UI.WorkPhone as VolunteerWorkPhone,
                --convert(varchar,UI.BirthDate,101) as VolunteerBirthDate,
                --UI.Address,
                --UI.StateId,
                --MST.Abbreviation as State,
                --UI.CityId,
                --MC.Abbreviation as City,
                --UI.Zip,
                --VI.DrivingLicenceNumber,
                --UI.Gender as VolunteerGender,
                --VSI.CreatedBy,
                --VSI.CreatedOn,
                --VSI.UpdatedBy,
                --VSI.UpdatedOn,
                VSI.StatusId,
                VSI.SeasonId,
                    (SELECT substring ( (SELECT ', ' + cast (b.[Day] as varchar)
                                                                            FROM (select MD.[Day] 
                                                                            from dbo.Master_Day MD where MD.DayID in (select items from dbo.udf_Split(PP.DaysCanNotPractice, ',')))
                                                                            b for xml path ('')),2,10000)) as DaysCanNotPractice,    
                    (SELECT substring ( (SELECT ', ' + cast (b.[Time] as varchar)
                                                                            FROM (select PT.[Time] 
                                                                                    from dbo.PreferedTime PT where PT.PreferedTimeId in (select items from dbo.udf_Split(PP.TimeCanNotPractice, ',')))
                                                                            b for xml path ('')),2,10000)) as TimeCanNotPractice,
                --PP.LocationId,
                --PP.LocationRankId,
                (select substring((select ', ' + cast (b.ShortName as varchar) 
                                            from (select ML.ShortName 
                                            from dbo.Master_Location ML 
                                            where ML.LocationID in (select items from dbo.udf_Split(PP.LocationId, ','))) 
                                    as b for xml path('')),2,1000000)) as ShortName,
                (SELECT MR.[Rank]
                    FROM dbo.Master_Rank MR
                    WHERE MR.RankID = PP.LocationRankId) as LocationRank,

                --PP.DayOfWeekId,
                --PP.DayOfWeekRankId,
                (select substring((select ', ' + cast (b.[Day] as varchar) 
                                                    from (select MD.[Day] from dbo.Master_Day MD
                                                    where MD.DayID in (select items from dbo.udf_Split(PP.DayOfWeekId,',')))        
                                                    as b for xml path('')),2,1000000)) as [DayOfWeek],
                (select MR.[Rank] 
                        from dbo.Master_Rank MR
                        where MR.RankID = PP.DayOfWeekRankId)AS DayOfWeekRank,

                --PP.TimeOfDayId,
                --PP.TimeOfDayRankId,   
                (select substring((select ', ' + cast (b.[Time] as varchar)
                                                    from (select [Time] 
                                                    from dbo.PreferedTime PT
                                                    where PT.PreferedTimeId in (select items from dbo.udf_Split(PP.TimeOfDayId,',')))
                                                    as b for xml path('')),2,1000000)) as TimeOfDay,
                (SELECT MR.[Rank]
                            FROM dbo.Master_Rank MR
                            WHERE MR.RankID = PP.TimeOfDayRankId) as TimeOfDayRank,

                --case when MVP.VolunteerPosition is null then '' else ( case when MD.Abbreviation is null then MVP.VolunteerPosition else '('+MD.Abbreviation+') '+MVP.VolunteerPosition end)end as VolunteeredPosition,
                --case when MVP.VolunteerPosition is null then '' else ( case when VRP.PositionId>5 then MVP.VolunteerPosition else '('+MD.Abbreviation+') '+MVP.VolunteerPosition end)end as VolunteeredPosition,
                case when MVP.VolunteerPosition is null then '' else 
            case when VRP.PositionId>5 then MVP.VolunteerPosition  else
                         case when MD.Abbreviation is null then MVP.VolunteerPosition else 
                              '('+MD.Abbreviation+') '+MVP.VolunteerPosition end
                         end
            end as VolunteeredPosition,

                case when VRP.PositionId is null then '' else convert(varchar(50),VRP.PositionId) end as VolunteeredPositionId,
                case when VRP.DivisionId is null then '' else convert(varchar(50),VRP.DivisionId) end as VolunteeredDivisionId,
                '' as AssignedPosition,
                '' as AssignedVolunteerPositionId,
                '' as AssignedDivisionId,


                    (SELECT substring ( (SELECT '; ' + cast (b.PlayerName AS varchar(max))
                                    FROM (SELECT DISTINCT ('('+MD.Abbreviation+') '+ PPI.PlayerLastName + ', '+PPI.PlayerFirstName+'$'+'001'+right(MUS.DetailIdURL_Structure + cast(convert(varchar(max),PPI.PlayerId) as varchar(max)),MUS.DetailIdURL_Length)) AS PlayerName
                                                    FROM dbo.Player_PermanentInfo PPI,dbo.Player_SeasonalInfo PSI,dbo.Master_Division MD
                                                    WHERE (PPI.ParentId1=UI.UserId or PPI.ParentId2=UI.UserId)
                                                    and PSI.PlayerId=PPI.PlayerId
                                                    and PSI.IsAvailable=1 and PSI.SeasonId=VSI.SeasonID
                                                    and PSI.DivisionId=MD.DivisionId
                                                    )b FOR XML PATH ( '' )),2,100000))AS PlayerName,

                /*
                  (SELECT substring ( (SELECT ';' + cast (b.PlayerLastName AS varchar(max))
                                                    FROM (SELECT DISTINCT PPI.PlayerLastName  
                                                                    FROM dbo.Player_PermanentInfo PPI,dbo.Player_SeasonalInfo PSI,dbo.Master_Division MD
                                                                    WHERE (PPI.ParentId1=UI.UserId or PPI.ParentId2=UI.UserId)
                                                                    and PSI.PlayerId=PPI.PlayerId
                                                                    and PSI.IsAvailable=1 and PSI.SeasonId=VSI.SeasonID
                                                                    and PSI.DivisionId=MD.DivisionId
                                                                    )
                                                                b
                                                FOR XML PATH ( '' )),
                                            2,
                                            100000
                                        ))
                                AS PlayerLastName,

                        (SELECT substring ( (SELECT ';' + cast (b.PlayerFirstName AS varchar(max))
                                                FROM (SELECT DISTINCT PPI.PlayerFirstName  
                                                                FROM dbo.Player_PermanentInfo PPI,dbo.Player_SeasonalInfo PSI,dbo.Master_Division MD
                                                                WHERE (PPI.ParentId1=UI.UserId or PPI.ParentId2=UI.UserId)
                                                                and PSI.PlayerId=PPI.PlayerId
                                                                and PSI.IsAvailable=1 and PSI.SeasonId=VSI.SeasonID
                                                                and PSI.DivisionId=MD.DivisionId
                                                                )
                                                            b
                                            FOR XML PATH ( '' )),
                                        2,
                                        100000
                                    ))
                            AS PlayerFirstName,*/

                /*(SELECT substring ((SELECT ','
                                                                    + cast (b.ColorID AS varchar)
                                                            FROM (SELECT DISTINCT (VBGD.ColorID)
                                                                            FROM dbo.VolunteerBackGroundDetail VBGD,dbo.Master_Color MCL
                                                                        WHERE MCL.ColorID= VBGD.ColorID
                                                                        and VBGD.VolunteerId = VSI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                                                                        )
                                                                    b
                                                        FOR XML PATH ( '' )),
                                                    2,
                                                    100000
                                            ))
                                    AS Result,*/

                (SELECT substring((SELECT ','
                                                                    + cast (b.CheckTypeID AS varchar)
                                                        FROM (SELECT DISTINCT (VBI.CheckTypeID)
                                                                        FROM dbo.Volunteer_BackgroundInfo VBI
                                                                        WHERE VBI.VolunteerId = VSI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                                                                        )
                                                                    b
                                                    FOR XML PATH ( '' )),
                                                2,
                                                100000
                                            ))
                                    AS BGCheckType,

                (SELECT substring ((SELECT ','
                                                                    + cast (b.ColorName AS varchar)
                                                            FROM (SELECT DISTINCT (MCL.ColorName)
                                                                            FROM dbo.Volunteer_BackgroundInfo VBI,dbo.Master_Color MCL
                                                                        WHERE MCL.ColorID= VBI.ColorID
                                                                                and VBI.VolunteerId = VI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                                                                                )
                                                                    b
                                                        FOR XML PATH ( '' )),
                                                    2,
                                                    100000
                                            ))
                                    AS BGCheckResult,

                /*(SELECT substring((SELECT ','
                                                                    + cast (b.CheckType AS varchar)
                                                        FROM (SELECT DISTINCT (MVCT.CheckType)
                                                                        FROM dbo.Volunteer_BackgroundInfo VBI,dbo.Master_VolunteerCheckType MVCT
                                                                        WHERE VBI.VolunteerId = VSI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                                                                        and VBI.CheckTypeID=MVCT.CheckTypeId)
                                                                    b
                                                    FOR XML PATH ( '' )),
                                                2,
                                                100000
                                            ))
                                    AS CheckType,*/

                (SELECT substring((SELECT ','
                + cast (b.DatePerformed AS varchar)
                    FROM (SELECT DISTINCT Convert(varchar,VBI.DatePerformed,101) as DatePerformed
                FROM dbo.Volunteer_BackgroundInfo VBI
                WHERE VBI.VolunteerId = VI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                )
                b FOR XML PATH ( '' )),2,100000))AS BGCheckDate,

                /*(SELECT substring((SELECT ','+ cast(b.EventId as varchar(max))
                                                                from(select distinct VCD.EventId
                                                                from CheckIn_VolunteerCheckInDetails VCD,CheckIn_CoachCheckInDetails CCD
                                                                where VCD.EventId=CCD.EventId
                                                                and (VCD.VolunteerSeasonalId=VSI.VolunteerSeasonalId or CCD.VolunteerSeasonalId=VSI.VolunteerSeasonalId))
                                    b for xml path('')),2,100000))  as CheckInEventID,*/
                    --'' as CheckInEventID,
                    /*(SELECT substring((SELECT ', '+ cast(b.EventName as varchar(max))
                                                                from(select distinct CEM.EventName
                                                                from CheckIn_EventMaster CEM, CheckIn_VolunteerCheckInDetails VCD,CheckIn_CoachCheckInDetails CCD
                                                                where CEM.CheckInEventId=VCD.EventId AND CEM.CheckInEventId=CCD.EventId
                                                                and (VCD.VolunteerSeasonalId=VSI.VolunteerSeasonalId or CCD.VolunteerSeasonalId=VSI.VolunteerSeasonalId))
                                    b for xml path('')),2,100000))  as CheckInEventName,*/

                /*(SELECT substring((SELECT ','+ cast(b.EventId as varchar(max))
                                                                from(select distinct VCD.EventId
                                                                from CheckIn_VolunteerCheckInDetails VCD
                                                                where VCD.VolunteerSeasonalId=VSI.VolunteerSeasonalId)
                                    b for xml path('')),2,100000))  as CheckInEventID,

                    (SELECT substring((SELECT ', '+ cast(b.EventName as varchar(max))
                                                                from(select distinct CEM.EventName
                                                                from CheckIn_EventMaster CEM, CheckIn_VolunteerCheckInDetails VCD
                                                                where CEM.CheckInEventId=VCD.EventId
                                                                and VCD.VolunteerSeasonalId=VSI.VolunteerSeasonalId)
                                    b for xml path('')),2,100000))  as CheckInEventName,*/

                /*(SELECT substring((SELECT ','
                                                                    + cast (b.DatePerformed AS varchar)
                                                        FROM (SELECT DISTINCT Convert(varchar,VBI.DatePerformed,101) as DatePerformed
                                                                        FROM dbo.Volunteer_BackgroundInfo VBI
                                                                        WHERE VBI.VolunteerId = VSI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                                                                        )
                                                                    b
                                                    FOR XML PATH ( '' )),
                                                2,
                                                100000
                                            ))
                                    AS DatePerformed,*/
                    /*case when exists(select * 
                    from dbo.VolunteerApproval 
                    where VolunteerApproval.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                    and VolunteerApproval.LeagueId = VSI.LeagueId
                    and VolunteerApproval.ApprovedStatus = 1) then 'i_tick.gif' else 'remove_Icon.png' end as IsApproved, */
                    VSI.VolunteerSeasonalStatusId as Approved,
                    case when VSI.VolunteerSeasonalStatusId = 1 then 'i_tick.gif' else 'remove_Icon.png' end as IsApproved ,
                                    --,MS.SeasonName
                    '1' as DisplayId,
                    case when VSI.SeasonId = 8 then '2014/15 Regular Season' else 
                 case when VSI.SeasonId = 7 then '2013/14 Regular Season' else '2014/15 FYBA Fall Academy' 
                     end 
            end as SeasonName,

            VSI.SchedulingPriority

    --into #finalTable
    FROM dbo.Master_Url_Setting MUS, dbo.Volunteer_Info VI
    inner join dbo.Volunteer_SeasonalInfo VSI on VSI.VolunteerId = VI.VolunteerId
    inner join User_Info UI on UI.UserId=VI.UserId
    inner JOIN dbo.Volunteer_Requested_Position VRP
    ON VRP.VolunteerSeasonalId = VSI.VolunteerSeasonalId
    LEFT OUTER JOIN dbo.Master_UniformSize MUSZ
    ON MUSZ.UniformSizeId = VI.ShirtSizeId
    left OUTER JOIN dbo.PracticePreference PP
    ON PP.VolunteerSeasonalId = VSI.VolunteerSeasonalId
    --left OUTER JOIN dbo.Volunteer_Requested_Position VRP
    --ON VRP.VolunteerSeasonalId = VSI.VolunteerSeasonalId
    left outer join dbo.Master_Division MD
    ON VRP.DivisionId = MD.DivisionId
    left outer join dbo.Master_VolunteerPosition MVP
    ON MVP.VolunteerPositionId = VRP.PositionId

    WHERE VSI.IsAvailable = 1
    AND VSI.LeagueId = @LeagueId
    and VSI.Seasonid in (7,8,9)
    --and VSI.VolunteerId=878

union

    SELECT distinct VSI.VolunteerSeasonalId,
                VI.VolunteerId,
                right(MUS.DetailIdURL_Structure + cast(convert(varchar(max),VI.VolunteerId) as varchar(max)),MUS.DetailIdURL_Length)  as NewVolunteerId,
                right(MUS.OtherURL_Structure + cast(convert(varchar(max),VSI.SeasonId) as varchar(max)),MUS.OtherURL_Length)  as NewSeasonId,            
                UI.FirstName as VolunteerFirstName,
                UI.LastName as VolunteerLastName,
                UI.LastName+ ', '+ UI.FirstName as VolunteerName,
                UI.Email as VolunteerEmail,
                VI.ShirtSizeId,
                MUSZ.Size as ShirtSize,
                UI.HomePhone as VolunteerHomePhone,
                UI.MobilePhone as VolunteerMobilePhone,
                UI.WorkPhone as VolunteerWorkPhone,
                --convert(varchar,UI.BirthDate,101) as VolunteerBirthDate,
                --UI.Address,
                --UI.StateId,
                --MST.Abbreviation as State,
                --UI.CityId,
                --MC.Abbreviation as City,
                --UI.Zip,
                --VI.DrivingLicenceNumber,
                --UI.Gender as VolunteerGender,
                --VSI.CreatedBy,
                --VSI.CreatedOn,
                --VSI.UpdatedBy,
                --VSI.UpdatedOn,
                VSI.StatusId,
                VSI.SeasonId,
                    (SELECT substring ( (SELECT ', ' + cast (b.[Day] as varchar)
                                                                            FROM (select MD.[Day] 
                                                                            from dbo.Master_Day MD where MD.DayID in (select items from dbo.udf_Split(PP.DaysCanNotPractice, ',')))
                                                                            b for xml path ('')),2,10000)) as DaysCanNotPractice,    
                    (SELECT substring ( (SELECT ', ' + cast (b.[Time] as varchar)
                                                                            FROM (select PT.[Time] 
                                                                                    from dbo.PreferedTime PT where PT.PreferedTimeId in (select items from dbo.udf_Split(PP.TimeCanNotPractice, ',')))
                                                                            b for xml path ('')),2,10000)) as TimeCanNotPractice,
                --PP.LocationId,
                --PP.LocationRankId,
                (select substring((select ', ' + cast (b.ShortName as varchar) 
                                            from (select ML.ShortName 
                                            from dbo.Master_Location ML 
                                            where ML.LocationID in (select items from dbo.udf_Split(PP.LocationId, ','))) 
                                    as b for xml path('')),2,1000000)) as ShortName,
                (SELECT MR.[Rank]
                    FROM dbo.Master_Rank MR
                    WHERE MR.RankID = PP.LocationRankId) as LocationRank,

                --PP.DayOfWeekId,
                --PP.DayOfWeekRankId,
                (select substring((select ', ' + cast (b.[Day] as varchar) 
                                                    from (select MD.[Day] from dbo.Master_Day MD
                                                    where MD.DayID in (select items from dbo.udf_Split(PP.DayOfWeekId,',')))        
                                                    as b for xml path('')),2,1000000)) as [DayOfWeek],
                (select MR.[Rank] 
                        from dbo.Master_Rank MR
                        where MR.RankID = PP.DayOfWeekRankId)AS DayOfWeekRank,

                --PP.TimeOfDayId,
                --PP.TimeOfDayRankId,   
                (select substring((select ', ' + cast (b.[Time] as varchar)
                                                    from (select [Time] 
                                                    from dbo.PreferedTime PT
                                                    where PT.PreferedTimeId in (select items from dbo.udf_Split(PP.TimeOfDayId,',')))
                                                    as b for xml path('')),2,1000000)) as TimeOfDay,
                (SELECT MR.[Rank]
                            FROM dbo.Master_Rank MR
                            WHERE MR.RankID = PP.TimeOfDayRankId) as TimeOfDayRank,

                '' as VolunteeredPosition,
                '' as VolunteeredPositionId,
                '' as VolunteeredDivisionId,
                case when AMVP.VolunteerPosition is null then '' else (case when TV.VolunteerPositionId>5 then AMVP.VolunteerPosition else '('+AMD.Abbreviation+') '+AMVP.VolunteerPosition end)end as AssignedPosition,
                --case when AMVP.VolunteerPosition is null then '' else(case when AMD.Abbreviation is null then AMVP.VolunteerPosition else '('+AMD.Abbreviation+') '+AMVP.VolunteerPosition end)end as AssignedPosition,
                case when TV.VolunteerPositionId is null then '' else convert(varchar(50),TV.VolunteerPositionId) end as AssignedVolunteerPositionId,
                case when MT.DivisionId is null then '' else convert(varchar(50),MT.DivisionId) end as AssignedDivisionId,

                    (SELECT substring ( (SELECT '; ' + cast (b.PlayerName AS varchar(max))
                                    FROM (SELECT DISTINCT ('('+MD.Abbreviation+') '+ PPI.PlayerLastName + ', '+PPI.PlayerFirstName+'$'+'001'+right(MUS.DetailIdURL_Structure + cast(convert(varchar(max),PPI.PlayerId) as varchar(max)),MUS.DetailIdURL_Length)) AS PlayerName
                                                    FROM dbo.Player_PermanentInfo PPI,dbo.Player_SeasonalInfo PSI,dbo.Master_Division MD
                                                    WHERE (PPI.ParentId1=UI.UserId or PPI.ParentId2=UI.UserId)
                                                    and PSI.PlayerId=PPI.PlayerId
                                                    and PSI.IsAvailable=1 and PSI.SeasonId=VSI.SeasonID
                                                    and PSI.DivisionId=MD.DivisionId
                                                    )b FOR XML PATH ( '' )),2,100000))AS PlayerName,

                (SELECT substring((SELECT ','
                                                                    + cast (b.CheckTypeID AS varchar)
                                                        FROM (SELECT DISTINCT (VBI.CheckTypeID)
                                                                        FROM dbo.Volunteer_BackgroundInfo VBI
                                                                        WHERE VBI.VolunteerId = VSI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                                                                        )
                                                                    b
                                                    FOR XML PATH ( '' )),
                                                2,
                                                100000
                                            ))
                                    AS BGCheckType,

                (SELECT substring ((SELECT ','
                                                                    + cast (b.ColorName AS varchar)
                                                            FROM (SELECT DISTINCT (MCL.ColorName)
                                                                            FROM dbo.Volunteer_BackgroundInfo VBI,dbo.Master_Color MCL
                                                                        WHERE MCL.ColorID= VBI.ColorID
                                                                                and VBI.VolunteerId = VI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                                                                                )
                                                                    b
                                                        FOR XML PATH ( '' )),
                                                    2,
                                                    100000
                                            ))
                                    AS BGCheckResult,

                (SELECT substring((SELECT ','
                + cast (b.DatePerformed AS varchar)
                    FROM (SELECT DISTINCT Convert(varchar,VBI.DatePerformed,101) as DatePerformed
                FROM dbo.Volunteer_BackgroundInfo VBI
                WHERE VBI.VolunteerId = VI.VolunteerId  --VBGD.VolunteerSeasonalID = VSI.VolunteerSeasonalId
                )
                b FOR XML PATH ( '' )),2,100000))AS BGCheckDate,
                    VSI.VolunteerSeasonalStatusId as Approved,
                    case when VSI.VolunteerSeasonalStatusId = 1 then 'i_tick.gif' else 'remove_Icon.png' end as IsApproved ,
                                    --,MS.SeasonName
                    '1' as DisplayId,
                    case when VSI.SeasonId = 8 then '2014/15 Regular Season' else 
                 case when VSI.SeasonId = 7 then '2013/14 Regular Season' else '2014/15 FYBA Fall Academy' 
                     end 
            end as SeasonName,
            VSI.SchedulingPriority

    --into #finalTable
    FROM dbo.Master_Url_Setting MUS, dbo.Volunteer_Info VI
    inner join dbo.Volunteer_SeasonalInfo VSI on VSI.VolunteerId = VI.VolunteerId
    inner join User_Info UI on UI.UserId=VI.UserId
    inner JOIN dbo.TeamVolunteers TV
    ON TV.VolunteerSeasonalId= VSI.VolunteerSeasonalId
    LEFT OUTER JOIN dbo.Master_UniformSize MUSZ
    ON MUSZ.UniformSizeId = VI.ShirtSizeId
    left OUTER JOIN dbo.PracticePreference PP
    ON PP.VolunteerSeasonalId = VSI.VolunteerSeasonalId

    --left OUTER JOIN dbo.TeamVolunteers TV
    --ON TV.VolunteerSeasonalId= VSI.VolunteerSeasonalId
    LEFT OUTER JOIN dbo.Master_Teams MT
    ON MT.TeamId = TV.TeamId
    left outer join dbo.Master_Division AMD
    ON MT.DivisionId = AMD.DivisionId
    left outer join dbo.Master_VolunteerPosition AMVP
    ON AMVP.VolunteerPositionId = TV.VolunteerPositionId

    WHERE VSI.IsAvailable = 1
    AND VSI.LeagueId = @LeagueId
    and VSI.Seasonid in (7,8,9)
    --and VSI.VolunteerId=878
    )b
    --where VolunteerSeasonalId=7225


    OPTION (FORCE ORDER);

        IF @p_DataflowId IS NULL
            BEGIN
                    SET @Statement = 'Select * from #finalTable ' + @p_SearchCriteria+' OPTION (FORCE ORDER)';
                    EXEC (@Statement);
            END
        ELSE
            BEGIN
                    CREATE TABLE [#tempExportFields]([DataFieldName] varchar(max),Label varchar(max));


                        Set @Statement = 'Select '

                        insert into #tempExportFields exec sp_getControlsorderingForExport @p_DataflowId;

                        set @Statement = 'Select '+(select substring((select ', '+ b.Label from (Select DataFieldName +' as ' + '['+ Label +']' as Label from #tempExportFields) as b for xml path('')),2,100000)) + ' from #finalTable ' + @p_SearchCriteria;
                    --select (@Statement);
                    EXEC (@Statement);
                    DROP TABLE #tempExportFields;
            END
            DROP TABLE #finalTable;  

            SET NOCOUNT OFF;
    end
    else /* for single Record record*/
    begin
            SELECT distinct VSI.VolunteerSeasonalId,
            VI.VolunteerId,
            right(MUS.DetailIdURL_Structure + cast(convert(varchar(max),VI.VolunteerId) as varchar(max)),MUS.DetailIdURL_Length)  as NewVolunteerId,
            right(MUS.OtherURL_Structure + cast(convert(varchar(max),VSI.SeasonId) as varchar(max)),MUS.OtherURL_Length)  as NewSeasonId,            
            UI.FirstName as VolunteerFirstName,
            UI.LastName as VolunteerLastName,
            UI.LastName+ ', '+ UI.FirstName as VolunteerName,
            UI.Email as VolunteerEmail,
            VI.ShirtSizeId,
            MUSZ.Size as ShirtSize,
            UI.HomePhone as VolunteerHomePhone,
            UI.MobilePhone as VolunteerMobilePhone,
            UI.WorkPhone as VolunteerWorkPhone,
            --convert(varchar,UI.BirthDate,101) as VolunteerBirthDate,
            --UI.Address,
            --UI.StateId,
            --MST.Abbreviation as State,
            --UI.CityId,
            --MC.Abbreviation as City,
            --UI.Zip,
            --VI.DrivingLicenceNumber,
            --UI.Gender as VolunteerGender,
            --VSI.CreatedBy,
            --VSI.CreatedOn,
            --VSI.UpdatedBy,
            --VSI.UpdatedOn,
            VSI.StatusId,
            VSI.SeasonId,
                (SELECT substring ( (SELECT ', ' + cast (b.[Day] as varchar)
                                                                        FROM (select MD.[Day] 
                                                                        from dbo.Master_Day MD where MD.DayID in (select items from dbo.udf_Split(PP.DaysCanNotPractice, ',')))
                                                                        b for xml path ('')),2,10000)) as DaysCanNotPractice,    
                (SELECT substring ( (SELECT ', ' + cast (b.[Time] as varchar)
                                                                        FROM (select PT.[Time] 
                                                                                from dbo.PreferedTime PT where PT.PreferedTimeId in (select items from dbo.udf_Split(PP.TimeCanNotPractice, ',')))
                                                                        b for xml path ('')),2,10000)) as TimeCanNotPractice,
            --PP.LocationId,
            --PP.LocationRankId,
            (select substring((select ', ' + cast (b.ShortName as varchar) 
                                        from (select ML.ShortName 
                                        from dbo.Master_Location ML 
                                        where ML.LocationID in (select items from dbo.udf_Split(PP.LocationId, ','))) 
                                as b for xml path('')),2,1000000)) as ShortName,
            (SELECT MR.[Rank]
                FROM dbo.Master_Rank MR
                WHERE MR.RankID = PP.LocationRankId) as LocationRank,

            --PP.DayOfWeekId,
            --PP.DayOfWeekRankId,
            (select substring((select ', ' + cast (b.[Day] as varchar) 
                                                from (select MD.[Day] from dbo.Master_Day MD
                                                where MD.DayID in (select items from dbo.udf_Split(PP.DayOfWeekId,',')))        
                                                as b for xml path('')),2,1000000)) as [DayOfWeek],
            (select MR.[Rank] 
                    from dbo.Master_Rank MR
                    where MR.RankID = PP.DayOfWeekRankId)AS DayOfWeekRank,

            --PP.TimeOfDayId,
            --PP.TimeOfDayRankId,   
            (select substring((select ', ' + cast (b.[Time] as varchar)
                                                from (select [Time] 
                                                from dbo.PreferedTime PT
                                                where PT.PreferedTimeId in (select items from dbo.udf_Split(PP.TimeOfDayId,',')))
                                                as b for xml path('')),2,1000000)) as TimeOfDay,
            (SELECT MR.[Rank]
                        FROM dbo.Master_Rank MR
                        WHERE MR.RankID = PP.TimeOfDayRankId) as TimeOfDayRank,

                        (SELECT substring ( (SELECT ',' + cast (b.PositionId AS varchar)
                                                    FROM (SELECT DISTINCT (VRP1.PositionId)
                                                                    FROM dbo.Volunteer_Requested_Position VRP1
                                                                    WHERE VRP1.VolunteerSeasonalId = VSI.VolunteerSeasonalId

                                                                                )
                                                                b
                                                FOR XML PATH ( '' )),
                                            2,
                                            100000
                                        ))
                                AS VolunteeredPositionId,
                                (SELECT substring ( (SELECT ';' + cast (b.VolunteerPosition AS varchar)
                                                                        FROM (SELECT DISTINCT (case when VRP1.PositionId>5 then MVP.VolunteerPosition else '('+MD.Abbreviation+') '+MVP.VolunteerPosition end)as VolunteerPosition
                                                                                        FROM dbo.Master_VolunteerPosition MVP,dbo.Volunteer_Requested_Position VRP1
                                                                                                 left outer join dbo.Master_Division MD ON VRP1.DivisionId = MD.DivisionId
                                                                                    WHERE MVP.VolunteerPositionId = VRP1.PositionId
                                                                                                AND VRP1.VolunteerSeasonalId = VSI.VolunteerSeasonalId
                                                                                                )
                                                                                b
                                                                    FOR XML PATH ( '' )),
                                                                2,
                                                                100000
                                        ))
                                AS VolunteeredPosition,
                                /*(SELECT substring ( (SELECT ';' + cast (b.VolunteerPosition AS varchar)
                                                                        FROM (SELECT DISTINCT (case when MD.Abbreviation is null then MVP.VolunteerPosition else '('+MD.Abbreviation+') '+MVP.VolunteerPosition end)as VolunteerPosition
                                                                                        FROM dbo.Master_VolunteerPosition MVP,dbo.Volunteer_Requested_Position VRP1
                                                                                                 left outer join dbo.Master_Division MD ON VRP1.DivisionId = MD.DivisionId
                                                                                    WHERE MVP.VolunteerPositionId = VRP1.PositionId
                                                                                                AND VRP1.VolunteerSeasonalId = VSI.VolunteerSeasonalId
                                                                                                )
                                                                                b
                                                                    FOR XML PATH ( '' )),
                                                                2,
                                                                100000
                                        ))
                                AS VolunteeredPosition,*/


                                (SELECT substring ( (SELECT ',' + cast (b.DivisionId AS varchar)
                                                    FROM (SELECT DISTINCT (VRP1.DivisionId)
                                                                    FROM dbo.Volunteer_Requested_Position VRP1
                                                                    WHERE VRP1.VolunteerSeasonalId = VSI.VolunteerSeasonalId

                                                                                )
                                                                b
                                                FOR XML PATH ( '' )),
                                            2,
                                            100000
                                        ))
                                AS VolunteeredDivisionId,

                                /*(SELECT substring ( (SELECT ', ' + cast (b.Abbreviation AS varchar)
                                                    FROM (SELECT DISTINCT (MD.Abbreviation)
                                                                    FROM dbo.Volunteer_Requested_Position VRP1,dbo.Master_Division MD
                                                                    WHERE VRP1.VolunteerSeasonalId = VSI.VolunteerSeasonalId
                                                                    and VRP1.DivisionId=MD.DivisionId

                                                                                )
                                                                b
                                                FOR XML PATH ( '' )),
                                            2,
                                            100000
                                        ))
                                AS Abbreviation,*/ /*VolunteeredDivision*/

                            (SELECT substring ( (SELECT ',' + cast (b.VolunteerPositionId AS varchar)
                                                        FROM (SELECT DISTINCT (TV.VolunteerPositionId)
                                                                        FROM dbo.TeamVolunteers TV
                                                                        WHERE TV.VolunteerSeasonalId = VSI.VolunteerSeasonalId

                                                                        )
                                                                    b
                                                    FOR XML PATH ( '' )),
                                                2,
                                                100000
                                            ))
                                    AS AssignedVolunteerPositionId,

                                    (SELECT substring ( (SELECT ';' + cast (b.VolunteerPosition AS varchar)

…截断

为了澄清,在您希望查询数据的时间点,您不执行查询,而是运行创建视图,然后从新创建的视图中选择?为什么要创建临时视图?除了有问题的临时视图构造之外,执行时间可能更多地与20多个表和方便连接有关。只有几千行应该是微不足道的,除非您有一个非常糟糕的优化查询,例如许多交叉连接。你能提供一个典型查询的例子吗?见鬼,如果只有2K条记录,数据库可能在很大程度上忽略了持久化索引,并将数据拉到内存中,在这一点上,即使有这么多的表,我预计也需要不到30秒的时间。如果您在SP中执行此操作,可能您是非常糟糕的参数嗅探的受害者?否则,是的,我们的典型示例查询,请。。。。这就是你的问题,我花了30秒才浏览完你的查询!如果你想让任何人帮你解决这个问题,你必须自己努力提出一个明确的问题。您的示例查询有多个被注释掉的部分,以及与原始问题无关的大量逻辑。请拿出一个简明的例子来说明你的问题,然后有人可能会提供帮助。