Click here to Skip to main content
15,896,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Sql query works fine for small interval of days but when i query for a period of 1 year it takes long time to load.

Please suggest me as how i can improve the speed of my query execution?
SQL
CREATE PROCEDURE [dbo].[GetVRROSAuthenticateUser_test] 
 (
	@StartDate Date,
	@EndDate   Date,
	@Vendor INT,
	@VendorId INT	--This is 'Usage' (select 'authenticated type' in report) in report
	)
    
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
  
 --If VendorId is 6 (Authenticated User OR 'OS') then proceed else return
   If ((@Vendor !=0) OR (@VendorId !=6))
     return
  

 -- If start Date is greater than End Date, return         
        IF( @StartDate > @EndDate )
             RETURN    

     DECLARE @StartDateRestricted TABLE (Restricted_Date DATE)
        -- Get Restricted date from Stored Procedure.
          INSERT INTO @StartDateRestricted
                  EXEC [dbo].[GetStartDateRestriction]

      -- If Start date is less than restricted date, return
            IF ( @StartDate < (SELECT Restricted_Date FROM @StartDateRestricted))
                  RETURN

      --Start date and End date must be within 1 year of date range.
            IF @StartDate <= DATEADD(YEAR, -1, @EndDate)
                  RETURN;
      
     
      -- Start Date is converted from DATE type to DATETIME.
		DECLARE @NewStartDate DATETIME = CONVERT(DATETIME, @StartDate)

		-- @NewEndDate is defined to make sure we fetch all the data for the last date 
		-- ie from (00:00:00 hrs to 23:59:59)
		DECLARE	@NewEndDate DATETIME = DATEADD(DAY, 1, @EndDate)
		SET		@NewEndDate = DATEADD(SECOND, -1, @NewEndDate)
		
DECLARE @ResultSet Table(VendorId INT,VendorName Varchar(50),BillableUsage INT) 

	
DECLARE @TblAccount TABLE(AccountId INT)

INSERT INTO @TblAccount
SELECT AccountId FROM BingMapsPlatform_Staging.dbo.MapPointAccounts WHERE CustomerTypeId IN (2, 7, 8, 10, 11)
UNION
SELECT new_accountid FROM BingMapsPlatform_MSCRM.dbo.FilteredAccount  WHERE new_ownertype IN (2, 7, 8, 10, 11)


INSERT INTO @ResultSet
  --MWS
SELECT
      TV.VendorId AS VendorId,
      TV.VendorName AS VendorName,    
      SUM(MWS.TransactionCount) AS BillableUsage
FROM 
      BingMapsPlatform_Staging.dbo.MWSDailyAggTrans  AS MWS WITH (NOLOCK)      
      INNER JOIN LoggingLookup.[dbo].[TblVendor] AS TV WITH (NOLOCK)
      ON MWS.VendorId = TV.VendorId      
      WHERE   (MWS.APIBillType in (1,2))
      AND MWS.EnvironmentId =0
      AND MWS.TransactionDate BETWEEN @NewStartDate AND @NewEndDate
      AND MWS.VendorId = 6 
      AND (MWS.AccountId IN (SELECT AccountId 
							FROM BingMapsPlatform_Staging.dbo.MapPointAccounts
							WHERE CustomerTypeId IN (2, 7, 8, 10, 11))
		)
GROUP BY 
TV.VendorId,
TV.VendorName


UNION

  --VEAPI
SELECT
      VBF.VendorIDs AS VendorId,	
      'OS' AS VendorName, -- need this      
      ROUND(SUM(ISNULL(VBF.TransactionCount,0)),0) AS BillableUsage
FROM 
      BingMapsPlatform_Staging.dbo.VEWSDailyAggTrans  AS VBF with (Nolock)      
      INNER JOIN LoggingLookup.[dbo].TblAPI AS TA
      ON VBF.APICode = TA.ApiCode
      WHERE 
      (VBF.APIBillType in (1,2))
      AND VBF.ServerRole =0
      AND (VBF.VendorIDs LIKE '%6%')    
      AND VBF.Date BETWEEN @NewStartDate AND @NewEndDate
      AND (VBF.PrimaryId IN (SELECT AccountId FROM BingMapsPlatform_Staging.dbo.MapPointAccounts WHERE CustomerTypeId IN (2, 7, 8, 10, 11))
		OR VBF.PrimaryId IN (SELECT new_accountid FROM BingMapsPlatform_MSCRM.dbo.FilteredAccount  WHERE new_ownertype IN (2, 7, 8, 10, 11)))
GROUP BY VBF.VendorIds

UNION

--Tiles

SELECT
      TV.VendorId AS VendorId, 
      TV.VendorName AS VendorName,       
      ROUND(SUM(ISNULL(TFE.TransactionCount,0)),0) AS BillableUsage
FROM 
      BingMapsPlatform_Staging.dbo.TFEDailyAggTrans  AS TFE WITH (NOLOCK) 
      INNER JOIN  @TblAccount AS TblAccount
      ON TFE.AccountId=TblAccount.AccountId
      INNER JOIN LoggingLookup.dbo.ProductToVendor PV WITH (NOLOCK)
      ON ([BingMapsPlatform_Staging].[dbo].[FnIfNumberExists](TFE.Products,',',148)=PV.ProductId OR [BingMapsPlatform_Staging].[dbo].[FnIfNumberExists](TFE.Products,',',147)=PV.ProductId)
      INNER JOIN LoggingLookup.dbo.TblVendor TV WITH (NOLOCK)
      ON TV.VendorId=PV.VendorId 
      WHERE 
      (TFE.TransactionBillTypeId in (1,3))
      AND TFE.EnvironmentId =0
      AND TFE.TokenStatus IN (2,4,5,6)     
      AND TFE.TransactionDate BETWEEN @NewStartDate AND @NewEndDate
     
GROUP BY TV.VendorId,TV.VendorName
ORDER BY TV.VendorId,TV.VendorName

SELECT VendorId,VendorName,ROUND((SUM(BillableUsage)/4),-0) AS BillableUsage
FROM @ResultSet 
GROUP BY VendorId,VendorName
ORDER BY VendorId,VendorName

END






GO
Posted
Updated 28-Nov-12 18:56pm
v2

1 solution

Some observations!
You are working with multiple databases and multiple sub queries combined with unions. You seem to be filtering on CustomerTypeId many times in both databases. I suggest you look at gathering the information into @table vars or temp tables.

Try getting a list of all the customers from both databases into a temp table then using an inner join.
Identify the filter that is going to reduce your data the most and apply that first.
Do some research into parameter sniffing, it may not be valid but it is worth considering.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900