Hi,I have a table with 12 million rows of data that we use for data mining. It is structured like thisCREATE TABLE [dbo].[NodeBDataTable] ( [ID] [bigint] NOT NULL , [NodeBID] [int] NOT NULL , [AddDate] [datetime] NOT NULL ) ON [PRIMARY]GOCREATE INDEX [IX_AddDate] ON [dbo].[NodeBDataTable] ([AddDate])WITH FILLFACTOR = 90) ON [PRIMARY]GO
This is the only Index on the AddDate columnI have only shown the relevant columns and Indexes of Interest here but there are lots moreIf I run the following query on itDeclare @Startdate datetimeDeclare @EndDate datetimeset @startdate = getdate() - 5set @enddate = getdate() - 4/**Query A **/select min(ID) from NodeBDataTable where adddate between getdate() - 5 and getdate() - 4 /** Query B */select min(ID) from NodeBDataTable where adddate between @startdate and @enddate
Query A does a Non-clustered Index seek and takes a fraction of a second, but Query B does a table scan and takes 3 minutes. That is disappointing as queries of Type A are fairly useless to us. Query A working as expected would seem to prove that the Index is OK, has statistics etc.As far as I can tell, both queries are exactly the same, so should behave the same. Can anyone see what I have missed?