Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Speeding up Stored Procedure
Message
From
14/03/2013 10:59:40
 
 
To
14/03/2013 09:48:58
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
01568261
Message ID:
01568371
Views:
31
Thanks Charles,

I'll try that out too.

>First thing I'd look at would be creating a Common Table Expression for the invoice line item totals using the filter. That way you have a cursor that contains only totals for the record you want and can inner join against that and take the where clause out of the main expression.
>
>(I'm gonna do this very fast to show what I'm talking about so forgive boneheaded typos etc )
>
>
>
>;WITH cte as 
> (SELECT ivd_invfk, SUM(ivd_amount) as invtotal FROM InvoiceDetails 
>WHERE ivd_invfk NOT IN (SELECT crt_invfk 
>						FROM CashReceipts 
>								INNER JOIN Deposits on crt_depfk = dep_pk 
>						WHERE dep_date <= @crt_date)
>GROUP BY ivd_invfk
>)
>	SELECT [inv_awbfk],
>		[inv_cancelled],
>		[inv_cusfk],
>		[inv_cusnumber],
>		[inv_date],
>		[inv_goodsvalue],
>		[inv_insurance],
>		[inv_number],
>		[inv_pieces],
>         	[inv_pk],
>		[inv_printed],
>		[inv_weight] 
>		,cus_company
>		,cus_lastname
>		,cus_firstname
>		,cus_number
>		,cnt_code
>		,inv_total = cte.invtotal
>		,awb_number
>		,asatdate = @crt_date
>	FROM [dbo].[Invoices]
>              INNER JOIN cte on invoices.inv_pk = cte.ivd_invfk
>		INNER JOIN Customers on inv_cusfk = cus_pk
>	       INNER JOIN AirWayBills on inv_awbfk = awb_pk
>		INNER JOIN Countries ON cus_cntfk = cnt_pk
>	
>	ORDER BY inv_number
>
>
>Makes sense?
>
>
>
>>I'm using the following in a Stored Procedure to pull out some data for a report. Unfortunately it's running too slowly and timing out when I call the Dataset.Fill().
>>
>>
	SELECT [inv_awbfk],
>>		[inv_cancelled],
>>		[inv_cusfk],
>>		[inv_cusnumber],
>>		[inv_date],
>>		[inv_goodsvalue],
>>		[inv_insurance],
>>		[inv_number],
>>		[inv_pieces],
>>		[inv_pk],
>>		[inv_printed],
>>		[inv_weight] 
>>		,cus_company
>>		,cus_lastname
>>		,cus_firstname
>>		,cus_number
>>		,cnt_code
>>		,inv_total = (SELECT SUM(ivd_amount) FROM InvoiceDetails WHERE ivd_invfk = inv_pk)
>>		,awb_number
>>		,asatdate = @crt_date
>>	FROM [dbo].[Invoices]
>>		INNER JOIN Customers on inv_cusfk = cus_pk
>>		INNER JOIN AirWayBills on inv_awbfk = awb_pk
>>		INNER JOIN Countries ON cus_cntfk = cnt_pk
>>	WHERE inv_pk NOT IN (SELECT crt_invfk 
>>							FROM CashReceipts 
>>								INNER JOIN Deposits on crt_depfk = dep_pk 
>>							WHERE dep_date <= @crt_date)
>>	ORDER BY inv_number
>>
>>Any suggestions for speeding this up?
>>
>>I've got indexes on all the joined fields.
>>
>>Is there a way for me to post the execution plan here?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Reply
Map
View

Click here to load this message in the networking platform