What index would you implement to cover this query:with cte( NaturalKey1, NaturalKey2, SomeValue, RowNumber)as( select NaturalKey1, NaturalKey2, SomeValue, dense_rank() over( partition by NaturalKey1, NaturalKey2 order by EffectiveDate desc) as 'RowNumber' from MyTable)select NaturalKey1, NaturalKey2, SomeValuefrom ctewhere RowNumber = 1
Something like this, right:?create nonclustered index ix_Coveron MyTable( NaturalKey1, NaturalKey2, EffectiveDate)include( SomeValue)
But why is that I can never get a plan that is favorable to this one's:select NaturalKey1, NaturalKey2, SomeValuefrom MyTable t1where not exists ( select 1 from MyTable t2 where t1.NaturalKey1 = t2.NaturalKey1 and t1.NaturalKey2 = t2.NaturalKey2 and t1.EffectiveDate < t2.EffectiveDate)
Is row number partitioning always not the right choice for effective dating?Pondering ...Jayto here knows when