Can you tell me how do i populate my table with dummy data ?
Don't use *,instead mention all require column along with their data type
so that it give fair idea whent creating index
If you answer my above question then I update my answer accordingly.
Try this,
DECLARE @Start date = '2000-01-23'
DECLARE @End date = '2017-04-22';
CREATE table MainTable(yr int not null,mnth int not null,d int not null)
create nonclustered index #MainTable_ymd on MainTable(yr ,mnth ,d )
----create nonclustered index #MainTable_y on #MainTable(yr)include(mnth ,d )
----here include(other column) so that it is covering index
----truncate table MainTable
insert into MainTable WITH(tablock)
select year(dt),month(dt),day(dt)
from
(
select top (10000000) dateadd(day,ROW_NUMBER()over(order by a.number)/5,'1900-01-01')dt from master..spt_values a
,master..spt_values b
)t4
I populate maintable with dummy data with 63,25,225 rows
--Solution 1
SELECT *
FROM (
SELECT yr
,mnth
,d
,cast(cast(yr AS VARCHAR(4)) + right('00' + cast(mnth AS VARCHAR), 2) + right('00' + cast(d AS VARCHAR), 2) AS DATE) dt
FROM MainTable
WHERE yr >= year(@Start)
AND yr <= year(@End)
) t4
WHERE dt >= @Start
AND dt <= @End
--RETURN
-- Solution 2
-- here put the result in temp table
CREATE table #tmp1(yr int,mnth int,d int,dt date not null)
INSERT INTO #tmp1
SELECT yr
,mnth
,d
,cast(cast(yr AS VARCHAR(4)) + right('00' + cast(mnth AS VARCHAR), 2) + right('00' + cast(d AS VARCHAR), 2) AS DATE) dt
FROM MainTable
WHERE yr >= year(@Start)
AND yr <= year(@End)
create clustered index #tmp1dt on #tmp1(dt)
select * from #tmp1
where dt>=@Start and dt<=@End
--drop table MainTable
drop table #tmp1
I am getting Index seek in both solution.
With JOhnRC query I get table scan and parallelism ,with urlreader also i get table scan using same sample data.
@Monah query-- TableScan
date
type that returnsDATEFROMPARTS(Year, Month, Day)
and add it to an index – Panagiotis KanavosStartDate
is holding i meandate
ordatetime
? – Susang