After running the following query:
SELECT [hour], count(*) as hits, avg(elapsed)
FROM myTable
WHERE [url] IS NOT NULL and floordate >= '2017-05-01'
group by [hour]
the execution plan is basically a clustered Index Scan on the PK (int, auto-increment, 97% of the work)
The thing is: URL
has a index on it (regular index because i'm always searching for a exact match), floordate
also has an index...
Why are they not being used? How can i speed up this query?
PS: table is 70M items long and this query takes about 9 min to run
Edit 1
If i don't use (select or filter) a column on my index, will it still be used? Usually i also filter-for/group-by clientId
(approx 300 unique across the db) and hour
(24 unique)...
hour
andElapsed
, if these columns are not included in the indexes you have mentioned query will need to lookup in the clustered index to get these values.hour
andelapsed
will still need to be looked up), and if most rows haveurl
NOT NULL
the index onurl
isn't useful at all. So it all comes down to how many rows the engine estimates will be covered byfloordate >= '2017-05-01'
and whether clustered index lookups will beat scanning it directly. If your statistics are out of date, this estimate may be wildly off. Try updating those before you do anything else (the next logical step would be toINCLUDE
columns in that index).