SQL оптимизация запросов с использованием динамического c запроса - PullRequest
0 голосов
/ 23 марта 2020

Execution planExecution Plan У меня есть следующая хранимая процедура. Он использует общее табличное выражение и табличную функцию. Выполнение этого sp занимает 600 мс. Я использовал динамический запрос c, чтобы избежать использования табличной функции, а время выполнения сократилось примерно до 300 мс. Можно ли дополнительно настроить этот запрос выбора так, чтобы время выполнения уменьшалось до 100 мс.

- EXE C Sp_GetTripListByBusications 1,2,3,4,5,6,7,8, 9,10,11,12,13,14,93 ', 1, 3, 1

CREATE PROCEDURE [dbo].[Sp_GetTripListByBusinesses]  
(  
 @BusinessID varchar (max),  
 @TripFlag int,  
 @TripFrom INT,  
 @IsAllTrip INT  
)  
AS  

BEGIN  
 DECLARE @Query nvarchar(max) = '';

     SET @Query= '
    ;With CTE  
     AS  
     (  
      select TripID, count(TripNotesID) as NoteCount from tripnotes  WHERE 
      TripNoteTypeID != 13 group by TripID  
     )  

    SELECT TRP.[TripID]  
     ,isnull(TRP.[AssignedTruckerID],0) as AssignedTruckerID  
     ,TRP.[BusinessID] AS BusinessID  
     ,isnull(TRP.[TruckRate],''0.0'') as TruckRate  
     ,TRP.[RequestedDatetime]  
     ,TRP.[ArrivedDatetime]  
     ,TRP.[DepartedDatetime]  
     ,TRP.[TripStatusID]  
     ,isnull(TRP.[IsDelayed] ,0) as IsDelayed  
     ,isnull(TRP.[IsStopped] ,0) as IsStopped  
     ,isnull(TRP.[RepeatUntilStop],'''') AS RepeatUntilStop  
     ,TRP.[ArrivedAtBusinessDatetime]  
     ,TRP.[OrgSiteID]  
     ,TRP.[OriginCustomerID] AS OrgCustomerID       
     ,OS.[SiteName] + '' / '' + ISNULL(OS.[AS400_ID],'''') AS OrgSiteName  
     ,isnull(OS.[SiteLat],''0.0'') as OrgSiteLat  
     ,isnull(OS.[SiteLong],''0.0'') as OrgSiteLong  
     ,isnull(OS.[WaypointsAvl],0) as OrgWaypointsAvl       
     ,isnull(OS.[NearestRoadwayLat],''0.0'') as OrgNearestRoadwayLat  
     ,isnull(OS.[NearestRoadwayLong],''0.0'') as OrgNearestRoadwayLong  
     ,OS.[SiteStatus] AS OrgSiteStatus  
     ,OS.[AS400_ID] AS OrgAS400_ID  
     ,TRP.[DestSiteID]  
     ,TRP.[DestCustomerID] AS DestCustomerID       
     ,DS.[SiteName] + '' / '' + ISNULL(DS.[AS400_ID],'''') AS DestSiteName  
     ,isnull(DS.[SiteLat],''0.0'') as DestSiteLat  
     ,isnull(DS.[SiteLong],''0.0'') as DestSiteLong       
     ,isnull(TRK.FirstName,'''') + '' '' + ISNULL(TRK.[LastName],'''') AS 
      TruckerName  
     ,isnull(TRK.[TruckType],''A'') as TruckType  
     ,TRK.[Mobile] AS TruckerMobile  
     ,B.[Name] AS BusinessName  
     ,isnull(B.BusinessLat,''0.0'') as BusinessLat  
     ,isnull(B.BusinessLong,''0.0'') as BusinessLong  
     ,B.ContactNo AS BusinessContactNo  
     ,B.Address1 AS BusinessAddress1  
     ,B.Address2 AS BusinessAddress2  
     ,B.City AS BusinessCity  
     ,B.[State] AS BusinessState  
     ,B.Zip AS BusinessZip  
     ,TS.[StatusText]  
     ,CASE WHEN TA.[BillOfLading] IS NULL THEN ''NA'' ELSE ''BOL'' END AS 
      BillOfLading  
     ,CASE WHEN TA.[ScaleTicket] IS NULL THEN ''NA'' ELSE ''ST'' END AS 
      ScaleTicket  
     ,CASE WHEN TA.[OriginScaleTicket] IS NULL THEN ''NA'' ELSE ''OST'' END AS 
      OriginScaleTicket  
     ,TRP.OriginGrossBushels  
     ,TRP.DestGrossBushels  
     ,(SELECT count(TripNotesID) FROM dbo.[TripNotes]) as Notes  
     ,TRP.MainTripID  
     ,Convert(DATE, TRP.[RequestedDatetime]) AS TripDate  
     ,1 as TripOrderNo  
     ,isnull(TRP.IsSiteDelayed ,0) as IsSiteDelayed  
     ,TRP.CommodityID  
     ,C.CommodityName  
     ,CTE.NoteCount  
     ,TRP.InvoiceId  
     ,TRP.OneWayMiles as OneWayMile
     ,OS.SiteType AS OriginSiteType
     ,DS.SiteType AS DestSiteType
    FROM  dbo.[Trip] AS TRP  
     INNER JOIN dbo.[Site] AS OS ON TRP.OrgSiteID = OS.SiteID  
     INNER JOIN dbo.[Site] AS DS ON TRP.DestSiteID = DS.SiteID  
     LEFT JOIN dbo.[Trucker] AS TRK ON TRP.AssignedTruckerID = TRK.TruckerID AND ISNULL(TRK.TruckerStatus,''A'') = ''A''  
     LEFT JOIN dbo.[Commodity] as C ON TRP.CommodityID = C.CommodityID AND C.RecordStatus = ''A''  
     INNER JOIN dbo.[Business] AS B ON B.BusinessID = TRP.BusinessID  
     LEFT JOIN dbo.[TripStatus] AS TS ON TS.TripStatusID = TRP.TripStatusID  
     LEFT JOIN dbo.[TripAsset] AS TA ON TA.TripID = TRP.TripID  
     LEFT JOIN CTE ON CTE.TripID = TRP.TripID   
    WHERE   
     TRP.RecordStatus=''A'' and   
     TRP.BusinessID in (' +@BusinessID + ') 
    ORDER BY TRP.RequestedDatetime ASC';  

  execute sp_executesql @Query;

END 
...