加速缓慢 SELECT DISTINCT 查询 SQL 服务器的解决方案
Solution for speeding up a slow SELECT DISTINCT query SQL Server
这与 (Solution for speeding up a slow SELECT DISTINCT query in Postgres )
中提出的问题基本相同
情况完全相同,部署了一个庞大的数据库,由于旧的遗留应用程序,无法对其进行规范化。不断添加新行并删除满足特定条件的旧行。
我已经通过使用 CTE 尝试了这个建议,但我在这里没有看到任何性能提升,
与原始
的执行时间几乎相同
select distinct [somecolumn]
from bigtable
同样适用于使用 Group by
的建议。
似乎最有效的建议是创建视图并改为查询视图。 (缓存已在查询之间重置)
我在这里需要一些建议,因为我真的不明白为什么这会带来更好的性能。
create view [dbo].[vwDistinct]
with schemabinding
as
select
[somecolumn], count_big(*) as TableCount
from
dbo.BigTable
where
somecolumn IS NOT NULL
group by
somecolumn;
select distinct somecolumn
from vwDistinct
应用程序使用存储过程进行调用。数据库在 SQL Server 2008 R2 上,但如果有充分的理由,可以将其移动到 SQL Server 2014。
谢谢
这是执行计划
select vwDistinct 中的某个列
<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.2" Build="12.0.2000.8" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
<BatchSequence>
<Batch>
<Statements>
<StmtSimple StatementCompId="1" StatementEstRows="41138.3" StatementId="1" StatementOptmLevel="FULL" CardinalityEstimationModelVersion="70" StatementSubTreeCost="5.10782" StatementText="select somecolumn from vwDistinct" StatementType="SELECT" QueryHash="0x23700E4CF62A8E4E" QueryPlanHash="0x79D8240601D270CB" RetrievedFromCache="true">
<StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
<QueryPlan NonParallelPlanReason="EstimatedDOPIsOne" CachedPlanSize="16" CompileTime="59" CompileCPU="18" CompileMemory="336">
<MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0" />
<OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="1239807" EstimatedPagesCached="77487" EstimatedAvailableDegreeOfParallelism="1" />
<RelOp AvgRowSize="37" EstimateCPU="1.03877" EstimateIO="4.06905" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="944197" LogicalOp="Clustered Index Scan" NodeId="1" Parallel="false" PhysicalOp="Clustered Index Scan" EstimatedTotalSubtreeCost="5.10782" TableCardinality="944197">
<OutputList>
<ColumnReference Database="[BigData]" Schema="[dbo]" Table="[vwDistinct]" Column="somecolumn" />
</OutputList>
<IndexScan Ordered="false" ForcedIndex="false" ForceScan="false" NoExpandHint="true" Storage="RowStore">
<DefinedValues>
<DefinedValue>
<ColumnReference Database="[BigData]" Schema="[dbo]" Table="[vwDistinct]" Column="somecolumn" />
</DefinedValue>
</DefinedValues>
<Object Database="[BigData]" Schema="[dbo]" Table="[vwDistinct]" Index="[cdxDistinct]" IndexKind="ViewClustered" Storage="RowStore" />
<IndexedViewInfo>
<Object Database="[BigData]" Schema="[dbo]" Table="[BigTable]" />
</IndexedViewInfo>
</IndexScan>
</RelOp>
</QueryPlan>
</StmtSimple>
</Statements>
</Batch>
</BatchSequence>
</ShowPlanXML>
<Object Database="[BigData]" Schema="[dbo]" Table="[vwDistinct]" Index="[cdxDistinct]" IndexKind="ViewClustered" Storage="RowStore" />
表明您的视图已编入索引。根据您声明的评论
Around 2.5-3 milion rows and there are about 100 distinct values.
查询
select distinct [somecolumn]
from bigtable
如果没有视图,将扫描 table 索引中的 2.5+ 百万行以查找所有不同的值。
但是,该视图将只包含 100 行。因此,当它存在时,它可以对视图的聚簇索引执行扫描以查找所有不同的值。
成本是所有修改 somecolumn
的插入和任何更新都会更昂贵。
这与 (Solution for speeding up a slow SELECT DISTINCT query in Postgres )
中提出的问题基本相同情况完全相同,部署了一个庞大的数据库,由于旧的遗留应用程序,无法对其进行规范化。不断添加新行并删除满足特定条件的旧行。 我已经通过使用 CTE 尝试了这个建议,但我在这里没有看到任何性能提升, 与原始
的执行时间几乎相同select distinct [somecolumn]
from bigtable
同样适用于使用 Group by
的建议。
似乎最有效的建议是创建视图并改为查询视图。 (缓存已在查询之间重置)
我在这里需要一些建议,因为我真的不明白为什么这会带来更好的性能。
create view [dbo].[vwDistinct]
with schemabinding
as
select
[somecolumn], count_big(*) as TableCount
from
dbo.BigTable
where
somecolumn IS NOT NULL
group by
somecolumn;
select distinct somecolumn
from vwDistinct
应用程序使用存储过程进行调用。数据库在 SQL Server 2008 R2 上,但如果有充分的理由,可以将其移动到 SQL Server 2014。
谢谢
这是执行计划 select vwDistinct 中的某个列
<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.2" Build="12.0.2000.8" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
<BatchSequence>
<Batch>
<Statements>
<StmtSimple StatementCompId="1" StatementEstRows="41138.3" StatementId="1" StatementOptmLevel="FULL" CardinalityEstimationModelVersion="70" StatementSubTreeCost="5.10782" StatementText="select somecolumn from vwDistinct" StatementType="SELECT" QueryHash="0x23700E4CF62A8E4E" QueryPlanHash="0x79D8240601D270CB" RetrievedFromCache="true">
<StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
<QueryPlan NonParallelPlanReason="EstimatedDOPIsOne" CachedPlanSize="16" CompileTime="59" CompileCPU="18" CompileMemory="336">
<MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0" />
<OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="1239807" EstimatedPagesCached="77487" EstimatedAvailableDegreeOfParallelism="1" />
<RelOp AvgRowSize="37" EstimateCPU="1.03877" EstimateIO="4.06905" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="944197" LogicalOp="Clustered Index Scan" NodeId="1" Parallel="false" PhysicalOp="Clustered Index Scan" EstimatedTotalSubtreeCost="5.10782" TableCardinality="944197">
<OutputList>
<ColumnReference Database="[BigData]" Schema="[dbo]" Table="[vwDistinct]" Column="somecolumn" />
</OutputList>
<IndexScan Ordered="false" ForcedIndex="false" ForceScan="false" NoExpandHint="true" Storage="RowStore">
<DefinedValues>
<DefinedValue>
<ColumnReference Database="[BigData]" Schema="[dbo]" Table="[vwDistinct]" Column="somecolumn" />
</DefinedValue>
</DefinedValues>
<Object Database="[BigData]" Schema="[dbo]" Table="[vwDistinct]" Index="[cdxDistinct]" IndexKind="ViewClustered" Storage="RowStore" />
<IndexedViewInfo>
<Object Database="[BigData]" Schema="[dbo]" Table="[BigTable]" />
</IndexedViewInfo>
</IndexScan>
</RelOp>
</QueryPlan>
</StmtSimple>
</Statements>
</Batch>
</BatchSequence>
</ShowPlanXML>
<Object Database="[BigData]" Schema="[dbo]" Table="[vwDistinct]" Index="[cdxDistinct]" IndexKind="ViewClustered" Storage="RowStore" />
表明您的视图已编入索引。根据您声明的评论
Around 2.5-3 milion rows and there are about 100 distinct values.
查询
select distinct [somecolumn]
from bigtable
如果没有视图,将扫描 table 索引中的 2.5+ 百万行以查找所有不同的值。
但是,该视图将只包含 100 行。因此,当它存在时,它可以对视图的聚簇索引执行扫描以查找所有不同的值。
成本是所有修改 somecolumn
的插入和任何更新都会更昂贵。