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?
CREATE PROCEDURE [dbo].[GetVRROSAuthenticateUser_test]
(
@StartDate Date,
@EndDate Date,
@Vendor INT,
@VendorId INT
)
AS
BEGIN
SET NOCOUNT ON;
If ((@Vendor !=0) OR (@VendorId !=6))
return
IF( @StartDate > @EndDate )
RETURN
DECLARE @StartDateRestricted TABLE (Restricted_Date DATE)
INSERT INTO @StartDateRestricted
EXEC [dbo].[GetStartDateRestriction]
IF ( @StartDate < (SELECT Restricted_Date FROM @StartDateRestricted))
RETURN
IF @StartDate <= DATEADD(YEAR, -1, @EndDate)
RETURN;
DECLARE @NewStartDate DATETIME = CONVERT(DATETIME, @StartDate)
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
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
SELECT
VBF.VendorIDs AS VendorId,
'OS' AS VendorName,
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
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