Hi Bill,
In SQL Server I would do the following. Both return a summary of production by Grade and run against a transaction table containing about a 1,000,000 rows.
In the first example the HPCDateTime column is indexed so it is very fast. In the second example HPCCreated is not indexed which causes a full table scan.
I used HPCXRefCount to simulate your selection based on Active.
SELECT HPC_HGRID, COUNT(HPCID) AS TotalProduction
FROM dbo.tblHPCHidePiece
WHERE (HPCXrefCount = 0) AND (HPCDateTime BETWEEN CONVERT(DATETIME, '2006-12-05 00:00:00', 102) AND CONVERT(DATETIME, '2006-12-06 00:00:00', 102))
GROUP BY HPC_HGRID
Runtime - sub second. Returns 12 rows - total rows selected ~6000
SELECT HPC_HGRID, COUNT(HPCID) AS TotalProduction
FROM dbo.tblHPCHidePiece
WHERE (HPCXrefCount = 0) AND (HPCCreated BETWEEN CONVERT(DATETIME, '2006-12-05 00:00:00', 102) AND CONVERT(DATETIME, '2006-12-06 00:00:00', 102))
GROUP BY HPC_HGRID
Runtime - 10 seconds. Returns 13 rows - total rows selected ~6000
Note that both queries use GROUP BY to return summary info only. HPCID is the PK on the table and Count(HPCID) gives the count of PK's associated with the grouping column.
Both queries run localy on my development laptop, an ASUS G1 with 2Gb memory.
As Trent advised - the Profiler is a great tool for spotting what is going on inside those pesky long running queries. Sometimes you can't do much about them but most times you can.
Cheers, Peter