title: SQL Server Temp Table with a High Sort
body: <p>I don't seen to understand why I would be getting an <code>Sort Cost</code> on an <code>temp table</code> that I included an non-clustered. </p>
  
  <blockquote>
    <p>Do I need to build the Statistics after the creating the index?</p>
  </blockquote>
  
  <pre><code>CREATE TABLE #TempGuidelineLog
  (
  [ID] int identity(1,1) NOT NULL,
  [CustomerId] int,[LogDate] DateTime,
  CONSTRAINT [PK_#TempTable'] PRIMARY KEY CLUSTERED 
      ([ID] ASC) WITH (PAD_INDEX = OFF
       ,STATISTICS_NORECOMPUTE = OFF
       ,IGNORE_DUP_KEY = OFF
       ,ALLOW_ROW_LOCKS = ON
       ,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
      ) ON [PRIMARY]
  
  INSERT #TempGuidelineLog ([CustomerId], [LogDate]) 
  SELECT g.CustomerId, g.LogDate FROM vwGuidelineLog  g
  where g.LogDate &gt;= '2017-10-01' and g.LogDate &lt; dateadd(day, 1, '2018-09-30')
  
  CREATE NONCLUSTERED INDEX ix_temp1Customer ON #TempGuidelineLog (CustomerId)
  INCLUDE ([LogDate])
  
      select
      g.*,
      a.StateId, 
      a.CountryId
      into #TempCustomerAddress
      from #TempGuidelineLog g
      JOIN [vwCustomerAddress] a ON a.CustomerId = g.CustomerId
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/1Erpf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1Erpf.png" alt="Temp Table Sort at 77%"></a></p>