Welcome to Sign in | Help

Re: tabele temporare

  •  06-26-2009, 2:49 PM

    Re: tabele temporare

    E bine sa verifici fiecare caz in parte.
    Daca, de exemplu, rulez scriptul de mai jos la mine (Vista Business 32 biti SP1, 4 CPU 2.8 Ghz, 2GB RAM, SQL 2008 dev SP1) obtin 1026 ms in cazul variabilei tabel si 596 ms in cazul tabelului temporar. Scriptul este propus de Gail Shaw (http://sqlinthewild.co.za/):
    Use AdventureWorks2008
    go

    -- Table variable

    SET NOCOUNT ON

    DECLARE @StartTime DATETIME

    DECLARE @LineItems TABLE (
    SalesOrderID INT PRIMARY KEY,
    ProductID INT,
    Total NUMERIC(38,6)
    )

    INSERT INTO @LineItems (SalesOrderID, ProductID, Total)
    SELECT SalesOrderDetailID, productid, LineTotal FROM Sales.SalesOrderDetail where ModifiedDate > '2003-10-01 00:00:00.000'

    SET @StartTime = GETDATE()

    SELECT ProductNumber, SUM(Total)
    FROM Production.Product p
    INNER JOIN @LineItems Sales on p.ProductID = Sales.ProductID
    GROUP BY ProductNumber

    PRINT 'Duration with table variable: ' + CAST(DATEDIFF(ms, @StartTime, getdate()) as varchar(10)) + ' ms'

    CREATE TABLE #LineItems (
    SalesOrderID INT PRIMARY KEY,
    ProductID INT,
    Total NUMERIC(38,6)
    )
    INSERT INTO #LineItems (SalesOrderID, ProductID, Total)
    SELECT SalesOrderDetailID, productid, LineTotal FROM Sales.SalesOrderDetail where ModifiedDate > '2003-10-01 00:00:00.000'

    SET @StartTime = GETDATE()

    SELECT ProductNumber, SUM(Total)
    FROM Production.Product p
    INNER JOIN #LineItems Sales on p.ProductID = Sales.ProductID
    GROUP BY ProductNumber

    PRINT 'Duration with temp table: ' + CAST(DATEDIFF(ms, @StartTime, getdate()) as varchar(10)) + ' ms'

    DROP TABLE #LineItems

    Este vorba cam de 65000 inregistrari.

    Vezi si http://www.sqlservercentral.com/articles/Temporary+Tables/66720/.
View Complete Thread
Powered by Community Server (Commercial Edition), by Telligent Systems