We have a stored proc that runs VERY slowly, and upon examination it was clear even to me that many sins have been committed in the design here. I've been asked to suggest what I would do, and wanted to avail myself of everyone's experience. So, I have a few questions:
1. Does Count(*) slow everything down? Would it be better to count a single field?
2. Is it better to use CASE statements than going through the same table once for each field?
3. None of these tables are keyed. How much performance can I expect to get by keying the tables and indexing them appropriately?
4. Any other suggestions?
Here's the proc. I've abbreviated it and changed all the names of tables and fields for regulatory reasons.
TIA for any help.
1. Does Count(*) slow everything down? Would it be better to count a single field?
2. Is it better to use CASE statements than going through the same table once for each field?
3. None of these tables are keyed. How much performance can I expect to get by keying the tables and indexing them appropriately?
4. Any other suggestions?
Here's the proc. I've abbreviated it and changed all the names of tables and fields for regulatory reasons.
TIA for any help.
Code:
USE [MYDB]
GO
/****** Object: StoredProcedure [dbo].[MYDB_Summary] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MYDB_Summary]
AS
BEGIN
SET NOCOUNT ON;
-- ===================================================================================
-- Set TANKERS sort fields
-- ===================================================================================
UPDATE [MYDB].[dbo].[TANKERS]
SET SortTankerTaxID = TaxID,
SortTankerName = Name,
SortTankerState = State;
UPDATE [MYDB].[dbo].[TANKERS]
SET SortTankerTaxID = CorrectedTaxID
WHERE CorrectedTaxID <> ' ';
UPDATE [MYDB].[dbo].[TANKERS]
SET SortTankerName = CorrectedName
WHERE CorrectedName <> ' ';
UPDATE [MYDB].[dbo].[TANKERS]
SET SortTankerState = CorrectedState
WHERE CorrectedState <> ' ';
-- ===================================================================================
-- Set MANIFESTS tanker information
-- ===================================================================================
UPDATE [MYDB].[dbo].[MANIFESTS]
SET TankerName = (SELECT SortTankerName
FROM [MYDB].[dbo].[TANKERS]
WHERE MANIFESTS.TankerID = TANKERS.TankerID),
TankerState = (SELECT SortTankerState
FROM [MYDB].[dbo].[TANKERS]
WHERE MANIFESTS.TankerID = TANKERS.TankerID);
*********** Same sort of thing in 3 other tables.
-- ===================================================================================
-- Set DRIVERS totals
-- ===================================================================================
UPDATE [MYDB].[dbo].[DRIVERS]
SET UCFCodesReceived = (SELECT COUNT(*) FROM [MYDB].[dbo].[LOADS]
WHERE LOADS.PackListID = DRIVERS.PackListID
AND LOADS.FuelTypeID = DRIVERS.FuelTypeID
AND LOADS.RequestID = DRIVERS.RequestID
AND LOADS.TankerID = DRIVERS.TankerID
AND LOADS.DriverID = DRIVERS.DriverID);
UPDATE [MYDB].[dbo].[DRIVERS]
SET UCFCodesReceived = 0
WHERE UCFCodesReceived IS NULL;
UPDATE [MYDB].[dbo].[DRIVERS]
SET RAUCFCodesReceived = (SELECT COUNT(*) FROM [MYDB].[dbo].[LOADS]
WHERE LOADS.PackListID = DRIVERS.PackListID
AND LOADS.FuelTypeID = DRIVERS.FuelTypeID
AND LOADS.RequestID = DRIVERS.RequestID
AND LOADS.TankerID = DRIVERS.TankerID
AND LOADS.DriverID = DRIVERS.DriverID
AND LOADS.ICCCode <> ' ');
UPDATE [MYDB].[dbo].[DRIVERS]
SET RAUCFCodesReceived = 0
WHERE RAUCFCodesReceived IS NULL;
********** 4 more of these for other summary fields
-- ===================================================================================
-- Set MANIFESTS totals
-- ===================================================================================
UPDATE [MYDB].[dbo].[MANIFESTS]
SET Requests = (SELECT COUNT(*) FROM [MYDB].[dbo].[DRIVERS]
WHERE DRIVERS.PackListID = MANIFESTS.PackListID
AND DRIVERS.FuelTypeID = MANIFESTS.FuelTypeID
AND DRIVERS.RequestID = MANIFESTS.RequestID
AND DRIVERS.TankerID = MANIFESTS.TankerID);
UPDATE [MYDB].[dbo].[MANIFESTS]
SET ManCt = 0
WHERE ManCt IS NULL;
UPDATE [MYDB].[dbo].[MANIFESTS]
SET Responses = (SELECT COUNT(*) FROM [MYDB].[dbo].[DRIVERS]
WHERE DRIVERS.PackListID = MANIFESTS.PackListID
AND DRIVERS.FuelTypeID = MANIFESTS.FuelTypeID
AND DRIVERS.RequestID = MANIFESTS.RequestID
AND DRIVERS.TankerID = MANIFESTS.TankerID
AND (DRIVERS.Status = 'EX'
OR DRIVERS.Status = 'FH'
OR DRIVERS.Status = 'DP'
OR DRIVERS.Status = 'NP'));
UPDATE [MYDB].[dbo].[MANIFESTS]
SET Responses = 0
WHERE Responses IS NULL;
************* 8 more like this ****************************
-- ===================================================================================
-- Set BILLSOFLADING totals
-- ===================================================================================
UPDATE [MYDB].[dbo].[BILLSOFLADING]
SET Requests = (SELECT SUM(Requests) FROM [MYDB].[dbo].[MANIFESTS]
WHERE MANIFESTS.PackListID = BILLSOFLADING.PackListID
AND MANIFESTS.FuelTypeID = BILLSOFLADING.FuelTypeID
AND MANIFESTS.RequestID = BILLSOFLADING.RequestID);
UPDATE [MYDB].[dbo].[BILLSOFLADING]
SET Responses = (SELECT SUM(Responses) FROM [MYDB].[dbo].[MANIFESTS]
WHERE MANIFESTS.PackListID = BILLSOFLADING.PackListID
AND MANIFESTS.FuelTypeID = BILLSOFLADING.FuelTypeID
AND MANIFESTS.RequestID = BILLSOFLADING.RequestID);
**************** 9 more of these
-- ===================================================================================
-- Set TANKERS totals
-- ===================================================================================
UPDATE [MYDB].[dbo].[TANKERS]
SET Requests = (SELECT SUM(Requests) FROM [MYDB].[dbo].[MANIFESTS]
WHERE MANIFESTS.PackListID = TANKERS.PackListID
AND MANIFESTS.TankerID = TANKERS.TankerID);
UPDATE [MYDB].[dbo].[TANKERS]
SET Responses = (SELECT SUM(Responses) FROM [MYDB].[dbo].[MANIFESTS]
WHERE MANIFESTS.PackListID = TANKERS.PackListID
AND MANIFESTS.TankerID = TANKERS.TankerID);
************ 7 more of these
END