I’m trying to execute stored procedure on SQL Server 2008 from C# application. It takes a lot of time and my application “not responding”. My stored procedure:
ALTER PROCEDURE [dbo].[PROC066]
@date_1 date,
@date_2 date
AS
SELECT
RTRIM(tt1.[Org]) as 'Org',
RTRIM(tt1.[CustomerHQ]) as 'Customer',
RTRIM(tt1.[Customer]) as 'ShipTo',
RTRIM(tt1.[Date]) as 'Date',
RTRIM(tt2.Clean) as 'Clean',
RTRIM(tt1.[PO number]) as 'PONumber',
RTRIM(tt1.[GCAS]) as 'GCAS',
'Description' = (SELECT DISTINCT [Description] FROM tDeploymentDB WHERE tDeploymentDB.[Item Code] = tt1.[GCAS]),
RTRIM(tt1.NQTY) as 'NQTY',
RTRIM(tt1.[Dummy]) as 'Dummy',
RTRIM(tt1.[MOQ]) as 'MOQ',
RTRIM(tt1.[Inactive]) as 'Inactive',
RTRIM(tt1.[Allocation]) as 'Allocation',
RTRIM(tt1.[MultiSector]) as 'MultiSector',
RTRIM(tt2.Pallet_Checked) as 'FullPallets',
RTRIM(tt2.FullTruck_Checked) as 'Full_Truck',
FROM tCleanOrderTracking_prod as tt1, tCleanOrderTracking_HDR_prod as tt2
WHERE
SUBSTRING(RTRIM(tt1.[PO number]), 6, LEN(RTRIM(tt1.[PO number])) - 5) =
RTRIM(tt2.CustomerOrderNumber) AND
tt1.[Date] = @date_1 AND tt1.[Date] <= @date_2
ORDER BY Org, [PO Number]
If I’m trying to execute this procedure on SQL Server Management Studio – it takes 5-7 sec. But through C# I can’t to execute this query. I was trying to delete this row in query
tt1.[Date] = @date_1 AND tt1.[Date] <= @date_2
After that it works fine.. my application takes 5 sec for executing. Also I was trying to rewrite this row
tt1.[Date] BETWEEN @date_1 AND @date_2
No result! My main table in query tCleanOrderTracking_prod have about 40500 records. What can I try else? What I’m doing wrong?
Have a look at How to read SQL Server execution plans it will show you how to analyse your own SQL Query and make improvements on speed and execution performance.
You want to run this against your query and look HOW it is being executed so that you can add indexes / change joins etc etc.
The
Trimfunction is also a computationally expensive function, its not just a select from a row but a processing command. Running it on 40,000 can take quite a hit. Consider only doing it on columns you need it on, and do it during INSERT time and not select time (then you do it once, not a million times 😉 ).