缩短创建临时表的时间

时间:2015-09-15 17:36:33

标签: sql sql-server-2008-r2

我尝试使用下面的脚本创建一个临时表,并且它将永远消失。我希望有人可能会有一些关于如何重写代码以加快速度的提示。

Select ProductName,
        Convert(date, messagereceived) as ProcessDate,
        datepart(hour,messagereceived) as ProcessHour, 
        Pipename,Partnername,
        DaTEDIFF(SECOND,MessageCreated,MessageReceived) as TransitTime,
        DaTEDIFF(SECOND,MessageReceived,ProcessingStarted) as WaitTime,
        DaTEDIFF(SECOND,ProcessingStarted,ProcessingFinished) as ProcessingTime
into #Temp
from Table ml 
inner join MessagingProduct mp on ml.MessagingProductId=mp.ProductId
where ml.processingstarted > '2015-09-08' and ml.processingstarted < '2015-09-09'
AND 
ml.MessagingSystemId=1

1 个答案:

答案 0 :(得分:0)

对于此查询:

Select  . . .
into #Temp
from Table ml inner join
     MessagingProduct mp
     on ml.MessagingProductId = mp.ProductId
where ml.processingstarted > '2015-09-08' and
      ml.processingstarted < '2015-09-09' AND 
      ml.MessagingSystemId = 1 ;

您需要表格上的索引。最重要的索引是Table(MessagingSystemId, processingStarted, MessagingProductId)上的综合索引。此外,MessagingProduct(ProductId, ProductName)上的索引也会有所帮助。