SQL Azure で Clustered Clustered Index を使用して Top with tables を使用することについて質問があります。
両方のテーブルにクラスター化された列ストア インデックスがあり、テーブル HeaderTable には 300K 行、テーブル ValuesTable には 6.5M 行があります。
-- with no "Top"
--responce after 2 sec
declare @Date datetime = getdate()
select zp.idCol1, Value1, zp.idcol2 from [HeaderTable] zp
inner join [dbo].[ValuesTable] zpp
on zp.idcol2 = zpp.idcol2
where zp.Date > @Date-30 and zp.Date < @Date-10 and zp.idCol1>0 and zpp.Value2 = 'SZT'
order by idcol2
go
-- with "Top 100"
--responce after 27 sec
declare @Date datetime = getdate()
select top 100 zp.idCol1, Value1, zp.idcol2 from [HeaderTable] zp
inner join [dbo].[ValuesTable] zpp
on zp.idcol2 = zpp.idcol2
where zp.Date > @Date-30 and zp.Date < @Date-10 and zp.idCol1>0 and zpp.Value2 = 'SZT'
order by idcol2
go
-- Result into Temporary Table and Select top 100 from Temporaty Table
-- responce after 2 sec
declare @Date datetime = getdate()
select zp.idCol1, Value1, zp.idcol2 into #d from [HeaderTable] zp
inner join [dbo].[ValuesTable] zpp
on zp.idcol2 = zpp.idcol2
where zp.Date > @Date-30 and zp.Date < @Date-10 and zp.idCol1>0 and zpp.Value2 = 'SZT'
select top 100 * from #d order by #d.idcol2
drop table #d
go
ご覧のとおり、2 番目のクエリの一番上の操作は非常に低速です。たぶん、誰かがこの問題についていくつかのヒントを持っていますか?