这基本上是同样的问题在问(Solution for speeding up a slow SELECT DISTINCT query in Postgres)解决方案加快慢速SELECT DISTINCT查询的SQL Server
这是很相同的情况下,庞大的部署数据库,这是不可能正常化它因旧的旧应用程序。新的行不断添加,旧的行被删除符合某些标准。 我以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
谢谢
这是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>
为 选择somecolumn执行计划
由于视图按某个列进行分组,所以它只会返回不同的一些列。所以你可以只做“从vwDistinct中选择一些列”,然后得到不同的值! – jarlh 2015-02-10 10:04:21
您可以在该列上添加索引 – 2015-02-10 10:05:25
表中有多少行以及'somecolumn'中有多少个不同的值? – 2015-02-10 10:07:00