I am trying to write a SQL script to fetch data for a specific date range. In the example shown below I am trying to pull data for 1 day (datetime) only.
But it just take more than 3 mins to complete the query execution.
I am not quite sure if I am missing any indexes to create on Datetime column. If yes could anybody please suggest me how to make this SQL query a quick execution?
Please also see the snapshots attached from query execution plan and statistics IO.
Thank you very much for your help.
The SQL query
set Statistics IO on
declare @StartDate datetime,@EndDate datetime
set @StartDate = '2012-07-19 00:00:00.000'
set @EndDate = '2012-07-20 23:59:00.000'
select * from Admin_Letters A with (nolock)
Where A.Date_Linked > @StartDate and A.Date_Linked < @EndDate
The Execution plan snapshot(s)
1.

2.

With a
SELECT *in your query, you will not have much luck trying to optimize anything…. since you request all columns from the table, SQL Server’s query optimizer will often go for a table scan since that’s just quicker.Also:
table scanimplies that you don’t have any clustering index on your table –> horribly bad choice. You should have a good clustering index on every real table!Once you have that (on e.g. an
INT IDENTITYcolumn), then you need to decide which columns you really need from that table. Put a nonclustered index onDate_Linked, and include all those columns you really truly need – something like:and then a
will really fly! (because now, SQL Server’s query optimizer can choose to just pick a few rows from the index – and even an index scan, on a few columns, will be lots faster than a table scan)