I have a stored procedure in a SQL Server 7 database to find duplicate records in one of the tables that is taking a very long time (over a minute and a half) to execute. I'm hoping someone can show me a way to speed it up.
I'm calling this from a VB6 SP5 program using ADO and OLEDB:
The DataInput table contains about 1.5 million records. I've had to set the CommandTimeout property to 120 seconds to prevent a Timeout Expired error.
Are there any changes I can make to the stored procedure to get a better execution time?
Thanks in advance for any suggestions.
Code:
CREATE PROCEDURE [spDataInputDupsRange]
@StartDate smalldatetime,
@EndDate smalldatetime,
@CostCenter char(5)
AS
SELECT DISTINCT DataInput.CostCenter,
DataInput.EmpID,
EmployeeList.EmployeeName,
DataInput.CategoryName,
DataInput.Date,
DataInput.AccountNumber,
DataInput.TransID,
DataInput.SupID,
DataInput.MgrID,
DataInput.Title,
DataInput.WorkCategory,
DataInput.CategoryType,
DataInput.Standard,
DataInput.Volume,
DataInput.Time,
DataInput.Amount,
DataInput.InputDate
FROM DataInput INNER JOIN EmployeeList ON DataInput.EmpID = EmployeeList.EmpID
WHERE (DataInput.Date BETWEEN @StartDate AND @EndDate)
AND DataInput.CostCenter=@CostCenter
AND DataInput.AccountNumber IS NOT NULL
AND LEN(DataInput.AccountNumber) = 16
AND (DataInput.CostCenter IN
(SELECT CostCenter
FROM DataInput AS Tmp
GROUP BY CostCenter, EmpID, CategoryName, Date, AccountNumber
HAVING COUNT(*) > 1
AND EmpID = DataInput.EmpID
AND CategoryName = DataInput.CategoryName
AND AccountNumber = DataInput.AccountNumber
AND [Date] = DataInput.Date
AND CostCenter = DataInput.CostCenter))
ORDER BY DataInput.EmpID, DataInput.CategoryName, DataInput.AccountNumber
Code:
rs.Open "spDataInputDupsRange '" & dtStartDate & "', '" & _
dtEndDate & "', '" & _
strCostCenter & "'", cn, adOpenForwardOnly, adLockReadOnly
Are there any changes I can make to the stored procedure to get a better execution time?
Thanks in advance for any suggestions.