Query on a large table using DateTime WHERE clause
I am trying to query data from a fairly large table, about 98 million rows, using datetime columns in a WHERE clause. It takes about 12 minutes to complete, which is obviously not acceptable. The request is simple:
SELECT ID, DateTime1, DateTime2, Value1, Value2
FROM dataTable
WHERE DateTime1 >= '2017-05-15 09:00' AND
DateTime1 <= '2017-05-15 09:30'
The table has the following structure:
Column Name | DataType
-------------------------
ID | float
DateTime1 | datetime
DateTime2 | datetime
Value1 | float
Value2 | varchar(20)
The table has the following index:
Nonclustered: DateTime1, DateTime2, ID, Value2
source to share
In SQL Server:
Your index does not cover Value1
, so it must retrieve that column from the table for every row if it uses your existing index.
You can create a coverage index (including all columns needed for the query):
create nonclustered index ix_dataTable_DateTime1_cover
on dbo.dataTable (DateTime1)
include (Id, DateTime2, Value1, Value2);
or modify the existing index to include Value1
.
Also, check the execution plan. If there are still performance issues, please share your execution plans using Paste The Plan @ brentozar.com , here are the instructions: How to use Paste the Plan .
source to share