1

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 番目のクエリの一番上の操作は非常に低速です。たぶん、誰かがこの問題についていくつかのヒントを持っていますか?

4

2 に答える 2

1

2番目の実行計画は恐ろしいです。SQL Server は、列ストアを行ストアの一時テーブルにバッファリングすることで、列ストアの利点をすべて台無しにしています。これは、クエリ オプティマイザーの品質上の問題です。

TOP が何もしないことを SQL Server に納得させます。

DECLARE @top BIGINT = 100;
SELECT TOP (@top) ...
OPTION (OPTIMIZE FOR (@top = 100000000000000000000000000000000));
于 2015-12-16T15:38:43.177 に答える