HI
I would add a non cluster index on :
TableID, ProductID, CreatedOn
Main Topics
Browse All TopicsHave a transactional table like this (tblTrx):
TableID, int
ProductID, int
CreatedOn, datetime
Quantity, int
Size of table is getting to 2.5M records and will grow at about 1.5M/year.
Most queries are date related wanting to know what the product quantities were at a certain date so we generally query like this:
SELECT * FROM tblTable T
INNER JOIN
(SELECT ProductID, MAX(TableID) AS TableID FROM tblTrx WHERE CreatedOn > @TIMESTAMP GROUP BY ProductID) M
ON T.ProductID = M.ProductID
AND T.TableID = M.TableID
Clustered index is on TableID
Non-clustered index on CreatedOn
So, a couple questions:
1. Is this type of query optimal for getting this data?
2. Thoughts on indexes?
What I'm finding is that a lot of queries are doing Clustered Index Scans rather than Seeks and it's getting slow.
Thoughts?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
SELECT ProductID, MAX(TableID) AS TableID
into #temp
FROM tblTrx WHERE CreatedOn > @TIMESTAMP GROUP BY ProductID
create clustered index idx_temptable_productidtab
SELECT * FROM tblTable T
INNER JOIN
#temp M
ON T.ProductID = M.ProductID
AND T.TableID = M.TableID
from there, youll be in good shape. even better if you add the indexes as suggested earlier...
Creating indexes on ProductID and TableID will give you "index coverage" in that all the data you need can be found in the indexes themselves, so the database doesn't actually have to go to the data at all. This can sometimes speed things up.
Whether SQL uses indexes or table scans often depends on how much data your queries are returning. If your results bring back more than about 1-2% of the table, SQL Server will normally ignore the indexes and opt for a table scan anyway.
Business Accounts
Answer for Membership
by: chapmandewPosted on 2009-11-05 at 05:50:50ID: 25749377
If I were you, I would put this:
SELECT ProductID, MAX(TableID) AS TableID FROM tblTrx WHERE CreatedOn > @TIMESTAMP GROUP BY ProductID
Into a temp table, index the temp table, and then do the join on it instead.