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