I have a query (which was created by LINQ to SQL) to get me a list of 'site visits' that were made between a certain date range which resulted in an order (orderid is not null).
Theres nothing wrong with the query. I just need advice on creating the correct index for it. I was playing around trying different combinations on a production site and managed to screw things up such that a foreign key got disconnected. I fixed that after some panic - but thought I'd ask for advice now before recreating an index.
The table is getting close to a million rows and I need the indexes to help me out here. This query is only used for reporting so doesnt have to be extremely fast, just not delay other user's queries (which it is doing).
SELECT TOP 1000
t0.SiteVisitId, t0.OrderId, t0.Date,
t1.Domain, t0.Referer, t0.CampaignId
FROM
SiteVisit AS t0
LEFT OUTER JOIN KnownReferer AS t1 ON t1.KnownRefererId = t0.KnownRefererId
WHERE
t0.Date <= @p0
AND t0.Date >= @p1
AND t0.OrderId IS NOT NULL
ORDER BY
t0.Date DESC
@p0='2008-11-1 23:59:59:000', @p1='2008-10-1 00:00:00:000'
I currently have a clustered index on SiteVisitId
, which is my identity integer column.
I dont know which of the following are most likely to be most efficient:
- Create an index on
Date
- Create an index on
Date
AND a separate index onOrderId
- Create a 'multicolumn' index on
Date
ANDOrderId
- Some other combination?
I am also wondering whether I should create a separate bit column for hasOrder
instead of checking if OrderId IS NOT NULL
if that might be more efficient.
FYI: The KnownReferer is just a table which contains a list of 100 or so known HttpReferers so i can easily see how many hits from google, yahoo etc.