1
我已经在存储过程中创建了一个临时表并对其进行了索引,并创建了一个临时变量表并将其编入索引。我发现临时表更快。请参阅下面的我的实施。可能有人告诉我,如果有我的方式已申请指标临时表与临时表变量索引
临时表执行
if object_id('tempdb..#maxPeriod') is not null drop table #maxPeriod else
select
fp.companyId,
max(fi.periodenddate) as maxPeriod
into #maxPeriod
from ciqfinperiod fp inner join ciqcompany ci on fp.companyId = ci.companyId
join ciqfininstance fi on fi.financialperiodid = fp.financialperiodid
where fp.periodtypeid = 4
and fi.periodenddate > @date
and fi.latestforfinancialperiodflag = 1
and latestfilingforinstanceflag = 1
group by fp.companyId
CREATE NONCLUSTERED INDEX IDX_companyId2 on #maxPeriod(companyId,maxPeriod)
临时表变量执行
DECLARE @maxPeriod TABLE (companyId INT, maxPeriod smalldatetime,
UNIQUE NONCLUSTERED (companyId, maxPeriod))
INSERT INTO @maxPeriod
select
fp.companyId,
max(fi.periodenddate) as maxPeriod
from ciqfinperiod fp inner join ciqcompany ci on fp.companyId = ci.companyId
join ciqfininstance fi on fi.financialperiodid = fp.financialperiodid
where fp.periodtypeid = 4
and fi.periodenddate > @date
and fi.latestforfinancialperiodflag = 1
and latestfilingforinstanceflag = 1
group by fp.companyId
后者有一个UNIQUE约束前者没有.. –
我如何将非聚集索引只应用于临时表变量 – Tom
我现在发现前者比后者更快 – Tom