2015-12-16 38 views
1

我有一个关于在SQL Azure上对使用群集群集索引的表使用Top的问题。Azure SQL,群集列存储索引,“TOP”性能

这两个表都具有群集列存储索引,表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

您不能指向查询计划的特定元素,并保持它的责任。你几乎声称TOP总是很慢,这不可能是真的。将实际执行计划作为XML发布到某处。 – usr

+0

这是计划:[链接](http://ws01.emigo.biz/xmlExecutionPlanTopProblem.xml) –

回答

1

第二个执行计划令人震惊。 SQL Server通过将Columnstore缓存到一个rowstore临时表中来破坏所有的Columnstore优点......这是查询优化器的一个质量问题,因为这种策略在任何情况下都没有意义。

试图说服SQL Server中的TOP什么都不做:

DECLARE @top BIGINT = 100; 
SELECT TOP (@top) ... 
OPTION (OPTIMIZE FOR (@top = 100000000000000000000000000000000)); 
+0

感谢您的解决方案,这有助于。它运行2秒。 –

2

这在增强在新的(兼容级别130优化,兼容级别130目前支持的预览,并没有如一般可用)Azure上的数据库。

ALTER DATABASE <dbname> SET COMPATIBILITY_LEVEL = 130

使其中的差别。