I am using the following code to fetch the report.
DECLARE @CountryID smallint = 100
DECLARE @UTCTDIM smallint = 330
DECLARE @MarketProfileID smallint = 2
DECLARE @StartDate datetime = '2017-01-01'
DECLARE @EndDate datetime = '2017-01-03'
DECLARE @CustomerTypeID smallint = 1
SET NOCOUNT ON
DECLARE @AirportList TABLE (ID bigint)
INSERT INTO @AirportList (ID)
SELECT
DISTINCT Airport.ID
FROM
Airport
INNER JOIN CityList ON Airport.CityID = CityList.ID
INNER JOIN CountryList ON CityList.CountryID = CountryList.ID
where
CountryList.NameEN in ('San Marino','Serbia','Slovakia','Slovenia','Spain','Sweden','Switzerland','Turkey','Ukraine','United Kingdom','Vatican CityList')
DECLARE @TempSales TABLE
(
[Type] varchar(50),
[From] char(3),
[To] char(3),
[Tickets] bigint,
[Average fare] decimal(18,3),
[Net sale] decimal(18,3)
)
INSERT INTO @TempSales
SELECT
[AirTrips].[NameEN] AS [Type],
[Airport1].[IATACode] AS [From],
[Airport2].[IATACode] AS [To],
NULL,
AVG(ISNULL([AirTraveler].[FareBaseAmount], 0)) + AVG(ISNULL([AirTraveler].[Taxes], 0)) AS [Average fare],
SUM(ISNULL([AirTraveler].[AmountNetRemit], 0)) AS [Net sale]
FROM
[BookingDetails] WITH (READPAST)
INNER JOIN [Bookings] WITH (READPAST) ON Booking.BookingFileID = BookingFile.ID
INNER JOIN [AirTrips] WITH (READPAST) ON Booking.AirTripTypeID = AirTrips.ID
INNER JOIN [AirTraveler] WITH (READPAST) ON AirTraveler.BookingID = Booking.ID
INNER JOIN [AirOrigin] WITH (READPAST) ON AirOrigin.BookingID = Booking.ID
INNER JOIN [Airport] AS [Airport1] WITH (READPAST) ON [Airport1].[ID] = [AirOrigin].[DepartureAirportID]
INNER JOIN [Airport] AS [Airport2] WITH (READPAST) ON [Airport2].[ID] = [AirOrigin].[ArrivalAirportID]
WHERE
[Bookings].[BookingStatusID] IN (16, 20, 22, 23)
AND [Bookings].[IsActive] = 1
AND [AirOrigin].[AirTripDirectionTypeID] = 1
AND (ISNULL([AirTraveler].[BaseAmountNetRemit], 0) + ISNULL([AirTraveler].[TaxesNetRemit], 0)) <> 0
AND [Bookings].[RemitDate] BETWEEN @StartDate AND @EndDate
AND [BookingDetails].[MarketProfileID] = @MarketProfileID
AND [BookingDetails].[BookedForOrganizationID] IS NULL
AND (
EXISTS (SELECT A.ID FROM @AirportList A WHERE A.ID = [Airport1].ID)
OR
EXISTS (SELECT A.ID FROM @AirportList A WHERE A.ID = [Airport2].ID)
)
GROUP BY
[AirTrips].[NameEN],
[Airport1].[IATACode],
[Airport2].[IATACode]
INSERT INTO @TempSales
SELECT
[AirTrips].[NameEN] AS [Type],
[Airport1].[IATACode] AS [From],
[Airport2].[IATACode] AS [To],
COUNT(*) [Tickets],
NULL,
NULL
FROM
[BookingDetails] WITH (READPAST)
INNER JOIN [Bookings] WITH (READPAST) ON Booking.BookingFileID = BookingFile.ID
INNER JOIN [AirTrips] WITH (READPAST) ON Booking.AirTripTypeID = AirTrips.ID
INNER JOIN [AirTraveler] WITH (READPAST) ON AirTraveler.BookingID = Booking.ID
INNER JOIN [AirOrigin] WITH (READPAST) ON AirOrigin.BookingID = Booking.ID
INNER JOIN [Airport] AS [Airport1] WITH (READPAST) ON [Airport1].[ID] = [AirOrigin].[DepartureAirportID]
INNER JOIN [Airport] AS [Airport2] WITH (READPAST) ON [Airport2].[ID] = [AirOrigin].[ArrivalAirportID]
WHERE
[Bookings].[BookingStatusID] IN (16, 22, 23)
AND [Bookings].[IsActive] = 1
AND [AirOrigin].[AirTripDirectionTypeID] = 1
AND (ISNULL([AirTraveler].[BaseAmountNetRemit], 0) + ISNULL([AirTraveler].[TaxesNetRemit], 0)) <> 0
AND [Bookings].[RemitDate] BETWEEN @StartDate AND @EndDate
AND [BookingDetails].[MarketProfileID] = @MarketProfileID
AND [BookingDetails].[BookedForOrganizationID] IS NULL
AND (
EXISTS (SELECT A.ID FROM @AirportList A WHERE A.ID = [Airport1].ID)
OR
EXISTS (SELECT A.ID FROM @AirportList A WHERE A.ID = [Airport2].ID)
)
GROUP BY
[AirTrips].[NameEN],
[Airport1].[IATACode],
[Airport2].[IATACode]
SELECT
[Type],
[From],
[To],
COALESCE( SUM([Tickets]), 0) AS [Tickets],
COALESCE( SUM([Average fare]), 0) AS [Average fare],
COALESCE( SUM([Net sale]), 0) AS [Net sale]
FROM
@TempSales
GROUP BY
[Type],
[From],
[To]
ORDER BY
[Tickets] DESC,
[Type] ASC,
[From] ASC,
[To] ASC
I have million records in the database. For small records (2,3) days , it is working fine. But for large records (10 days) , I am getting the resource locked/time out error.
I found one issue is with following code :
AND (
EXISTS (SELECT A.ID FROM @AirportList A WHERE A.ID = [Airport1].ID)
OR
EXISTS (SELECT A.ID FROM @AirportList A WHERE A.ID = [Airport2].ID)
)
If i remove this code.I am not facing much issue. Please help me to find an alternate way to write this code or optmize it
Try this:
Add primary key to the temp table