Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing with vs declare a temporary table: performance / difference?
    primarykey
    data
    text
    <p>I have created a sql function in <strong>SQLServer 2008</strong> that declared a temporary table and uses it to compute a moving average on the values inside</p> <pre><code>declare @tempTable table ( GeogType nvarchar(5), GeogValue nvarchar(7), dtAdmission date, timeInterval int, fromTime nvarchar(5), toTime nvarchar(5), EDSyndromeID tinyint, nVisits int ) insert @tempTable select * from aces.dbo.fEDVisitCounts(@geogType, @hospID,DATEADD(DD,-@windowDays + 1,@fromDate), @toDate,@minAge,@maxAge,@gender,@nIntervalsPerDay, @nSyndromeID) INSERT @table (dtAdmission,EDSyndromeID, MovingAvg) SELECT list.dtadmission , @nSyndromeID , AVG(data.nVisits) as MovingAvg from @tempTable as list inner join @tempTable as data ON list.dtAdmission between data.dtAdmission and DATEADD(DD,@windowDays - 1,data.dtAdmission) where list.dtAdmission &gt;= @fromDate GROUP BY list.dtAdmission </code></pre> <p>but I also found out that you can declare the tempTable like this:</p> <pre><code>with tempTable as ( select * from aces.dbo.fEDVisitCounts('ALL', null,DATEADD(DD,-7,'01-09-2010'), '04-09-2010',0,130,null,1, 0) ) </code></pre> <p><strong>Question: Is there a major difference in these two approaches? Is one faster than the other or more common / standard?</strong> I would think the declare is faster since you define what the columns you are looking for are.. Would it also be even faster if I were to omit the columns that were not used in the calculations of moving average?(not sure about this one since it has to get all of the rows anyways, though selecting less columns makes intuitive sense that it would be faster/less to do)</p> <p>I also have found a <code>create temporary table @table</code> from here <a href="https://stackoverflow.com/questions/4680886/how-to-declare-internal-table-in-mysql">How to declare Internal table in MySQL?</a> but I don't want the table to persist outside of the function (I am not sure if the create temporary table does this or not.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload