3

我有两个表:dim_applications 和应用程序列表

/*dim_applications*/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dim_applications]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[dim_applications](
        [id] [bigint] NOT NULL,
        [country_id] [int] NOT NULL,
        [customer_id] [bigint] NOT NULL,
        [first_status_day_date] [date] NULL,
        [first_status_time_of_day] [time](0) NULL,
        [campaign_id] [int] NULL,
        [campaign_response_time][int] NULL
    ) ON [PRIMARY]
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[dim_applications]') AND name = N'PK_dim_applications')
ALTER TABLE [dbo].[dim_applications] ADD CONSTRAINT [PK_dim_applications] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[dim_applications]') AND name = N'IX_dim_applications')
CREATE NONCLUSTERED INDEX [IX_dim_applications] ON [dbo].[dim_applications]
(
    [country_id] ASC
)
INCLUDE ([customer_id], [first_status_day_date], [first_status_time_of_day]) 
GO

和记录表,其中包含发送给客户的 CRM 消息:

IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'fact_customers_campaigns' AND TABLE_CATALOG = 'dwh')
BEGIN
    CREATE TABLE [dbo].[fact_customers_campaigns](
        [campaign_id] [int] NOT NULL,
        [customer_id] [bigint] NULL,
        [day_date] [date] NOT NULL,
        [time_of_day] [time](0) NOT NULL
        ) ON [PRIMARY]
END 
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[fact_customers_campaigns]') AND name = N'IX_fact_customers_campaigns_3')
CREATE NONCLUSTERED INDEX [IX_fact_customers_campaigns_3] ON [dbo].[fact_customers_campaigns]
(
    [customer_id] ASC,
    [day_date] DESC,
    [time_of_day] DESC
)
INCLUDE([campaign_id])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

我要选择的是针对特定国家/地区的每个应用程序的最接近活动的“campaign_id”。此广告系列不得“超过”14 天。两个表的连接由“customer_id”完成。

我的选择查询是这样的

SELECT 
    a.id
    ,(SELECT TOP 1 
            c.campaign_id 
        FROM [dwh].[dbo].[fact_customers_campaigns] c 
        WHERE 1 = 1
            AND c.[customer_id] = a.[customer_id] 
            AND DATEDIFF(DD, c.[day_date], a.[first_status_day_date]) BETWEEN 0 AND 14
            ORDER BY c.[day_date] DESC, c.[time_of_day] DESC)
FROM [dwh].[dbo].[dim_applications] a
WHERE 1 = 1
    AND a.[country_id] = 1

它工作正常,在 7 秒内返回 150 万条记录。但是,当我使用列表 (14) 中最后一个国家的 country_id 运行相同的查询时,相同的查询会变得非常慢,可能需要长达 2 分钟(120 万条记录)。为什么会有这样的差异?计划是一样的。我只是不明白这种行为。服务器版本是

Microsoft SQL Server 2012 (SP1) - 11.0.3339.0 (X64) 
    Jan 14 2013 19:02:10 
    Copyright (c) Microsoft Corporation
    Developer Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)

Microsoft SQL Server 2008 R2 (SP1) - 10.50.2796.0 (X64) 
    Dec  9 2011 11:27:20 
    Copyright (c) Microsoft Corporation
    Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (Hypervisor)

如何使执行时间独立于所选的county_id?这是 country_id = 14,SQL Server 2012 SP1 的计划

<?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="11.0.3339.0" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="1" StatementEstRows="1253920" StatementId="1" StatementOptmLevel="FULL" StatementSubTreeCost="258.447" StatementText="SELECT &#xD;&#xA;    a.id&#xD;&#xA;    ,(SELECT TOP 1 &#xD;&#xA;            c.campaign_id &#xD;&#xA;        FROM [dwh].[dbo].[fact_customers_campaigns] c &#xD;&#xA;        WHERE 1 = 1&#xD;&#xA;            AND c.[customer_id] = a.[customer_id] &#xD;&#xA;            AND DATEDIFF(DD, c.[day_date], a.[first_status_day_date]) BETWEEN 0 AND 14&#xD;&#xA;            ORDER BY c.[day_date] DESC, c.[time_of_day] DESC)&#xD;&#xA;FROM [dwh].[dbo].[dim_applications] a&#xD;&#xA;WHERE 1 = 1&#xD;&#xA;    AND a.[country_id] = 14" StatementType="SELECT" QueryHash="0x9FA79BEF6AA009EB" QueryPlanHash="0x98FC15D00C35255D" 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 DegreeOfParallelism="8" MemoryGrant="136" CachedPlanSize="40" CompileTime="2" CompileCPU="2" CompileMemory="488">
            <ThreadStat Branches="1" UsedThreads="8">
              <ThreadReservation NodeId="0" ReservedThreads="8" />
            </ThreadStat>
            <MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0" RequiredMemory="136" DesiredMemory="136" RequestedMemory="136" GrantWaitTime="0" GrantedMemory="136" MaxUsedMemory="136" />
            <OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="314572" EstimatedPagesCached="157286" EstimatedAvailableDegreeOfParallelism="4" />
            <RelOp AvgRowSize="19" EstimateCPU="0.125392" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="1253920" LogicalOp="Compute Scalar" NodeId="0" Parallel="false" PhysicalOp="Compute Scalar" EstimatedTotalSubtreeCost="258.447">
              <OutputList>
                <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="id" />
                <ColumnReference Column="Expr1006" />
              </OutputList>
              <ComputeScalar>
                <DefinedValues>
                  <DefinedValue>
                    <ColumnReference Column="Expr1006" />
                    <ScalarOperator ScalarString="[dwh].[dbo].[fact_customers_campaigns].[campaign_id] as [c].[campaign_id]">
                      <Identifier>
                        <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="campaign_id" />
                      </Identifier>
                    </ScalarOperator>
                  </DefinedValue>
                </DefinedValues>
                <RelOp AvgRowSize="19" EstimateCPU="0.940258" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="1253920" LogicalOp="Gather Streams" NodeId="1" Parallel="true" PhysicalOp="Parallelism" EstimatedTotalSubtreeCost="258.322">
                  <OutputList>
                    <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="id" />
                    <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="campaign_id" />
                  </OutputList>
                  <RunTimeInformation>
                    <RunTimeCountersPerThread Thread="0" ActualRows="1253922" ActualEndOfScans="1" ActualExecutions="1" />
                  </RunTimeInformation>
                  <Parallelism>
                    <RelOp AvgRowSize="19" EstimateCPU="1.31035" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="1253920" LogicalOp="Left Outer Join" NodeId="2" Parallel="true" PhysicalOp="Nested Loops" EstimatedTotalSubtreeCost="257.382">
                      <OutputList>
                        <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="id" />
                        <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="campaign_id" />
                      </OutputList>
                      <RunTimeInformation>
                        <RunTimeCountersPerThread Thread="4" ActualRows="86240" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                        <RunTimeCountersPerThread Thread="8" ActualRows="219041" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                        <RunTimeCountersPerThread Thread="5" ActualRows="172480" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                        <RunTimeCountersPerThread Thread="3" ActualRows="86241" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                        <RunTimeCountersPerThread Thread="7" ActualRows="258720" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                        <RunTimeCountersPerThread Thread="6" ActualRows="172480" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                        <RunTimeCountersPerThread Thread="1" ActualRows="172480" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                        <RunTimeCountersPerThread Thread="2" ActualRows="86240" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                        <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" />
                      </RunTimeInformation>
                      <NestedLoops Optimized="false" WithUnorderedPrefetch="true">
                        <OuterReferences>
                          <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="customer_id" />
                          <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="first_status_day_date" />
                          <ColumnReference Column="Expr1008" />
                        </OuterReferences>
                        <RelOp AvgRowSize="26" EstimateCPU="0.344868" EstimateIO="4.14941" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="1253920" LogicalOp="Index Seek" NodeId="4" Parallel="true" PhysicalOp="Index Seek" EstimatedTotalSubtreeCost="4.49428" TableCardinality="6016420">
                          <OutputList>
                            <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="id" />
                            <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="customer_id" />
                            <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="first_status_day_date" />
                          </OutputList>
                          <RunTimeInformation>
                            <RunTimeCountersPerThread Thread="8" ActualRows="219041" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="4" ActualRows="86240" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="5" ActualRows="172480" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="3" ActualRows="86241" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="7" ActualRows="258720" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="6" ActualRows="172480" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="1" ActualRows="172480" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="2" ActualRows="86240" Batches="0" ActualEndOfScans="1" ActualExecutions="1" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" />
                          </RunTimeInformation>
                          <IndexScan Ordered="true" ScanDirection="FORWARD" ForcedIndex="false" ForceSeek="false" ForceScan="false" NoExpandHint="false" Storage="RowStore">
                            <DefinedValues>
                              <DefinedValue>
                                <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="id" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="customer_id" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="first_status_day_date" />
                              </DefinedValue>
                            </DefinedValues>
                            <Object Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Index="[IX_dim_applications]" Alias="[a]" IndexKind="NonClustered" />
                            <SeekPredicates>
                              <SeekPredicateNew>
                                <SeekKeys>
                                  <Prefix ScanType="EQ">
                                    <RangeColumns>
                                      <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="country_id" />
                                    </RangeColumns>
                                    <RangeExpressions>
                                      <ScalarOperator ScalarString="(14)">
                                        <Const ConstValue="(14)" />
                                      </ScalarOperator>
                                    </RangeExpressions>
                                  </Prefix>
                                </SeekKeys>
                              </SeekPredicateNew>
                            </SeekPredicates>
                          </IndexScan>
                        </RelOp>
                        <RelOp AvgRowSize="11" EstimateCPU="1E-07" EstimateIO="0" EstimateRebinds="1253920" EstimateRewinds="0.134582" EstimatedExecutionMode="Row" EstimateRows="1" LogicalOp="Top" NodeId="5" Parallel="true" PhysicalOp="Top" EstimatedTotalSubtreeCost="251.577">
                          <OutputList>
                            <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="campaign_id" />
                          </OutputList>
                          <RunTimeInformation>
                            <RunTimeCountersPerThread Thread="4" ActualRows="0" Batches="0" ActualEndOfScans="86240" ActualExecutions="86240" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="8" ActualRows="36405" Batches="0" ActualEndOfScans="182636" ActualExecutions="219041" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="5" ActualRows="11849" Batches="0" ActualEndOfScans="160631" ActualExecutions="172480" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="3" ActualRows="1367" Batches="0" ActualEndOfScans="84874" ActualExecutions="86241" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="7" ActualRows="47272" Batches="0" ActualEndOfScans="211448" ActualExecutions="258720" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="6" ActualRows="22849" Batches="0" ActualEndOfScans="149631" ActualExecutions="172480" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="1" ActualRows="19968" Batches="0" ActualEndOfScans="152512" ActualExecutions="172480" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="2" ActualRows="0" Batches="0" ActualEndOfScans="86240" ActualExecutions="86240" ActualExecutionMode="Row" />
                            <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" />
                          </RunTimeInformation>
                          <Top RowCount="false" IsPercent="false" WithTies="false">
                            <TopExpression>
                              <ScalarOperator ScalarString="(1)">
                                <Const ConstValue="(1)" />
                              </ScalarOperator>
                            </TopExpression>
                            <RelOp AvgRowSize="25" EstimateCPU="0.000167189" EstimateIO="0.003125" EstimateRebinds="1253920" EstimateRewinds="0.134582" EstimatedExecutionMode="Row" EstimateRows="1" LogicalOp="Index Seek" NodeId="7" Parallel="true" PhysicalOp="Index Seek" EstimatedTotalSubtreeCost="229.491" TableCardinality="1606160">
                              <OutputList>
                                <ColumnReference Column="Bmk1002" />
                                <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="campaign_id" />
                                <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="day_date" />
                                <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="time_of_day" />
                              </OutputList>
                              <RunTimeInformation>
                                <RunTimeCountersPerThread Thread="4" ActualRows="0" Batches="0" ActualEndOfScans="86240" ActualExecutions="86240" ActualExecutionMode="Row" />
                                <RunTimeCountersPerThread Thread="8" ActualRows="36405" Batches="0" ActualEndOfScans="182636" ActualExecutions="219041" ActualExecutionMode="Row" />
                                <RunTimeCountersPerThread Thread="5" ActualRows="11849" Batches="0" ActualEndOfScans="160631" ActualExecutions="172480" ActualExecutionMode="Row" />
                                <RunTimeCountersPerThread Thread="3" ActualRows="1367" Batches="0" ActualEndOfScans="84874" ActualExecutions="86241" ActualExecutionMode="Row" />
                                <RunTimeCountersPerThread Thread="7" ActualRows="47272" Batches="0" ActualEndOfScans="211448" ActualExecutions="258720" ActualExecutionMode="Row" />
                                <RunTimeCountersPerThread Thread="6" ActualRows="22849" Batches="0" ActualEndOfScans="149631" ActualExecutions="172480" ActualExecutionMode="Row" />
                                <RunTimeCountersPerThread Thread="1" ActualRows="19968" Batches="0" ActualEndOfScans="152512" ActualExecutions="172480" ActualExecutionMode="Row" />
                                <RunTimeCountersPerThread Thread="2" ActualRows="0" Batches="0" ActualEndOfScans="86240" ActualExecutions="86240" ActualExecutionMode="Row" />
                                <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" />
                              </RunTimeInformation>
                              <IndexScan Ordered="true" ScanDirection="FORWARD" ForcedIndex="false" ForceSeek="false" ForceScan="false" NoExpandHint="false" Storage="RowStore">
                                <DefinedValues>
                                  <DefinedValue>
                                    <ColumnReference Column="Bmk1002" />
                                  </DefinedValue>
                                  <DefinedValue>
                                    <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="campaign_id" />
                                  </DefinedValue>
                                  <DefinedValue>
                                    <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="day_date" />
                                  </DefinedValue>
                                  <DefinedValue>
                                    <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="time_of_day" />
                                  </DefinedValue>
                                </DefinedValues>
                                <Object Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Index="[IX_fact_customers_campaigns_3]" Alias="[c]" IndexKind="NonClustered" />
                                <SeekPredicates>
                                  <SeekPredicateNew>
                                    <SeekKeys>
                                      <Prefix ScanType="EQ">
                                        <RangeColumns>
                                          <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="customer_id" />
                                        </RangeColumns>
                                        <RangeExpressions>
                                          <ScalarOperator ScalarString="[dwh].[dbo].[dim_applications].[customer_id] as [a].[customer_id]">
                                            <Identifier>
                                              <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="customer_id" />
                                            </Identifier>
                                          </ScalarOperator>
                                        </RangeExpressions>
                                      </Prefix>
                                    </SeekKeys>
                                  </SeekPredicateNew>
                                </SeekPredicates>
                                <Predicate>
                                  <ScalarOperator ScalarString="datediff(day,CONVERT_IMPLICIT(datetimeoffset(7),[dwh].[dbo].[fact_customers_campaigns].[day_date] as [c].[day_date],0),CONVERT_IMPLICIT(datetimeoffset(7),[dwh].[dbo].[dim_applications].[first_status_day_date] as [a].[first_status_day_date],0))&gt;=(0) AND datediff(day,CONVERT_IMPLICIT(datetimeoffset(7),[dwh].[dbo].[fact_customers_campaigns].[day_date] as [c].[day_date],0),CONVERT_IMPLICIT(datetimeoffset(7),[dwh].[dbo].[dim_applications].[first_status_day_date] as [a].[first_status_day_date],0))&lt;=(14)">
                                    <Logical Operation="AND">
                                      <ScalarOperator>
                                        <Compare CompareOp="GE">
                                          <ScalarOperator>
                                            <Intrinsic FunctionName="datediff">
                                              <ScalarOperator>
                                                <Const ConstValue="(4)" />
                                              </ScalarOperator>
                                              <ScalarOperator>
                                                <Convert DataType="datetimeoffset" Style="0" Implicit="true">
                                                  <ScalarOperator>
                                                    <Identifier>
                                                      <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="day_date" />
                                                    </Identifier>
                                                  </ScalarOperator>
                                                </Convert>
                                              </ScalarOperator>
                                              <ScalarOperator>
                                                <Convert DataType="datetimeoffset" Style="0" Implicit="true">
                                                  <ScalarOperator>
                                                    <Identifier>
                                                      <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="first_status_day_date" />
                                                    </Identifier>
                                                  </ScalarOperator>
                                                </Convert>
                                              </ScalarOperator>
                                            </Intrinsic>
                                          </ScalarOperator>
                                          <ScalarOperator>
                                            <Const ConstValue="(0)" />
                                          </ScalarOperator>
                                        </Compare>
                                      </ScalarOperator>
                                      <ScalarOperator>
                                        <Compare CompareOp="LE">
                                          <ScalarOperator>
                                            <Intrinsic FunctionName="datediff">
                                              <ScalarOperator>
                                                <Const ConstValue="(4)" />
                                              </ScalarOperator>
                                              <ScalarOperator>
                                                <Convert DataType="datetimeoffset" Style="0" Implicit="true">
                                                  <ScalarOperator>
                                                    <Identifier>
                                                      <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[fact_customers_campaigns]" Alias="[c]" Column="day_date" />
                                                    </Identifier>
                                                  </ScalarOperator>
                                                </Convert>
                                              </ScalarOperator>
                                              <ScalarOperator>
                                                <Convert DataType="datetimeoffset" Style="0" Implicit="true">
                                                  <ScalarOperator>
                                                    <Identifier>
                                                      <ColumnReference Database="[dwh]" Schema="[dbo]" Table="[dim_applications]" Alias="[a]" Column="first_status_day_date" />
                                                    </Identifier>
                                                  </ScalarOperator>
                                                </Convert>
                                              </ScalarOperator>
                                            </Intrinsic>
                                          </ScalarOperator>
                                          <ScalarOperator>
                                            <Const ConstValue="(14)" />
                                          </ScalarOperator>
                                        </Compare>
                                      </ScalarOperator>
                                    </Logical>
                                  </ScalarOperator>
                                </Predicate>
                              </IndexScan>
                            </RelOp>
                          </Top>
                        </RelOp>
                      </NestedLoops>
                    </RelOp>
                  </Parallelism>
                </RelOp>
              </ComputeScalar>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>
4

2 回答 2

1

此查询适用于这两种情况。

SELECT 
    a.id
    ,(SELECT TOP 1 
            c.campaign_id 
        FROM [dwh].[dbo].[fact_customers_campaigns] c 
        WHERE 1 = 1
            AND c.[customer_id] = a.[customer_id] 
            AND c.[day_date] >= DATEADD(dd, -14, a.[first_status_day_date])
            AND c.[day_date] <= a.[first_status_day_date]
            ORDER BY c.[day_date] DESC, c.[time_of_day] DESC)
FROM [dwh].[dbo].[dim_applications] a
WHERE 1 = 1
    AND a.[country_id] = 14

此版本正确使用索引,并且在这两种情况下都可以快速选择记录。

于 2013-06-30T18:24:10.420 回答
0

从您的计划中,我可以看出您的统计数据有问题。例如:估计行数(索引查找)为 1,但实际行数为 139710。您应该检查您的统计数据!

如果您从存储过程执行此查询,您应该重新编译过程,因为 sql 缓存中的计划(编译计划)不是“好”一秒查询。

我不明白您的过滤条件 1=1,为什么要使用它!?

于 2013-06-30T09:50:50.790 回答