Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Speeding up Stored Procedure
Message
De
14/03/2013 09:48:58
 
 
À
13/03/2013 17:51:15
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Divers
Thread ID:
01568261
Message ID:
01568347
Vues:
39
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?


Charles Hankey

Though a good deal is too strange to be believed, nothing is too strange to have happened.
- Thomas Hardy

Half the harm that is done in this world is due to people who want to feel important. They don't mean to do harm-- but the harm does not interest them. Or they do not see it, or they justify it because they are absorbed in the endless struggle to think well of themselves.

-- T. S. Eliot
Democracy is two wolves and a sheep voting on what to have for lunch.
Liberty is a well-armed sheep contesting the vote.
- Ben Franklin

Pardon him, Theodotus. He is a barbarian, and thinks that the customs of his tribe and island are the laws of nature.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform