Question : T-SQL Statement taking waaaaay too long to execute..

Hi SQL experts,

I am wondering if any of you can help me to figure out why the below query is taking more than four minutes to return ~70,000 records?

Is there a better way to use table variables?

Cheers,

Critto.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
ALTER PROCEDURE SP_BookingAd_Renew_Print
 
AS
BEGIN
	SET NOCOUNT ON;
 
	Declare @yesterday date
	Declare @today date
	Declare @maxDate date
	Declare @shelfDate date
	Declare @PrintAdID int
 
	select @yesterday = DATEADD(DAY,DATEDIFF(DAY,'20000102',GETDATE()),'20000101')
	select @today = GETDATE()
 
	DECLARE @AdTable TABLE
	(
		 AdTableID int IDENTITY(1,1) PRIMARY KEY
		,BookingID int --UNIQUE
		,Heading nvarchar(500)
		,AdExternalReference nvarchar(500)
		,UserExternalReference nvarchar(500)
		,FirstName nvarchar(500)
		,LastName nvarchar(500)
		,Email nvarchar(500)
		,Cost decimal
		,OnlineEndDate date
	)
 
    -- Get all the ad, user, online, cost data
    INSERT INTO @AdTable ( 
		BookingID, 
		Heading, 
		AdExternalReference, 
		UserExternalReference,
		FirstName, 
		LastName, 
		Email, 
		Cost,
		OnlineEndDate ) 
    
		SELECT 
			 b.booking_id as 'BookingID'
			,baov.heading as 'Heading'
			,ad.external_id as 'AdExternalReference'
			,u.external_id as 'UserExternalReference'
			,u.given_name as 'FirstName'
			,u.surname as 'LastName'
			,u.email_login as 'Email'
			,rcp.total_exc_vat as 'Cost'
			,bao.end_date as 'OnlineEndDate'
			
			FROM
			booking b-- booking
			INNER JOIN booking_ad_online bao on b.booking_id = bao.booking_id-- booking_ad_online
			INNER JOIN booking_ad_online_version baov on bao.booking_ad_online_id = baov.booking_ad_online_id-- booking_ad_online_version
			INNER JOIN ad on b.ad_id = ad.ad_id-- ad
			INNER JOIN order_package op on b.order_package_id = op.order_package_id-- order_package
			INNER JOIN rate_card_package rcp on op.rate_card_package_id = rcp.rate_card_package_id-- rate_card_package
			INNER JOIN [order] o on op.order_id = o.order_id-- order
			INNER JOIN [user] u on o.owner_user_id = u.[user_id]-- user
 
	DECLARE @PrintTable TABLE
	(
		PrintAdInsertionID int PRIMARY KEY,
		BookingAdPrintID int,
		CreatedTimestamp date,
		EndShelfLife date
	)
	
	-- get all the print ad data
	INSERT INTO @PrintTable ( 
		PrintAdInsertionID,
		BookingAdPrintID,
		CreatedTimestamp)
		
		SELECT 
		print_ad_insertion_id as 'PrintAdID',
		booking_ad_print_id as 'BookingPrintID',
		created_timestamp as 'CreatedTimestamp'
		
		FROM
		print_ad_insertion
	
 
        -- Add them together and return. need to get all as not 1:1 related print-online.	
	SELECT 
	
	 at.BookingID as 'BookingID'
	,at.Heading as 'Heading'
	,at.AdExternalReference as 'AdRef'
	,at.UserExternalReference as 'UserRef'
	,at.FirstName as 'First'
	,at.LastName as 'Last'
	,at.Email as 'Email'
	,at.Cost as 'Cost'
	,at.OnLineEndDate as 'OnlineEnd'
	,bap.booking_id as 'Booking'
	,pt.BookingAdPrintID as 'PrintID'
	,pt.CreatedTimestamp as 'Created'
 
	FROM 
	
	--@PrintTable pt
	--RIGHT JOIN booking_ad_print bap on bap.booking_ad_print_id = pt.BookingAdPrintID
	--INNER JOIN @AdTable at on at.BookingID = bap.booking_id
	
	@AdTable at 
	LEFT JOIN booking_ad_print bap on at.BookingID = bap.booking_id
	LEFT OUTER JOIN @PrintTable pt on bap.booking_ad_print_id = pt.BookingAdPrintID
	
END
GO
 
--exec SP_BookingAd_Renew_Print
Open in New Window Select All

Answer : T-SQL Statement taking waaaaay too long to execute..

Check out the following:

1. SELECT statements used for fetching records into Table variables needs to be checked whether it uses appropriate indexes.
2. If those two table variables will have huge no of records, use Temp tables with Indexes created accordingly so that it will perform faster compared to the present one.
Random Solutions  
 
programming4us programming4us