Home Forums SQL Server 2005 SQL Server 2005 General Discussion Is it "better" to specify the definition of a temporary table, or just define it on the fly? RE: Is it "better" to specify the definition of a temporary table, or just define it on the fly?

  • My apologies. I forgot to post the code for my performance claim.

    --http://www.sqlservercentral.com/Forums/Topic1378984-149-1.aspx?Update=1

    DECLARE @StartTime DATETIME;

    RAISERROR('===============================================================',0,1) WITH NOWAIT;

    RAISERROR('========== SELECT/INTO',0,1) WITH NOWAIT;

    RAISERROR('===============================================================',0,1) WITH NOWAIT;

    DBCC FREESYSTEMCACHE ('ALL') WITH NO_INFOMSGS ;

    DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS ;

    SELECT @StartTime = GETDATE();

    SELECT TOP (1000000)

    SomeID = IDENTITY(INT,1,1),

    SomeInt = ABS(CHECKSUM(NEWID()))%50000+1,

    SomeLetters2 = CHAR(ABS(CHECKSUM(NEWID()))%26+65)

    + CHAR(ABS(CHECKSUM(NEWID()))%26+65),

    SomeCSV = 'Part01,Part02,Part03,Part04,Part05,Part06,Part07,Part08,Part09,Part10',

    SomeMoney = ABS(CHECKSUM(NEWID()))%10000 /100.0,

    SomeDate = CAST(RAND(CHECKSUM(NEWID()))*3653.0+36524.0 AS DATETIME),

    SomeHex09 = RIGHT(NEWID(),09)

    INTO #JBMTest

    FROM sys.all_columns t1

    CROSS JOIN sys.all_columns t2

    ;

    --===== Add the PK

    -- Takes about 1 second to execute.

    -- ALTER TABLE #JBMTest

    -- ADD PRIMARY KEY CLUSTERED (SomeID)

    --;

    --===== Duration and HouseKeeping

    PRINT 'Duration = ' + CONVERT(CHAR(12),GETDATE()-@StartTime,114);

    DROP TABLE #JBMTest

    ;

    GO 5

    DECLARE @StartTime DATETIME;

    RAISERROR('===============================================================',0,1) WITH NOWAIT;

    RAISERROR('========== Precreate Table with Index',0,1) WITH NOWAIT;

    RAISERROR('===============================================================',0,1) WITH NOWAIT;

    DBCC FREESYSTEMCACHE ('ALL') WITH NO_INFOMSGS ;

    DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS ;

    SELECT @StartTime = GETDATE();

    CREATE TABLE [dbo].[#JBMTest](

    [SomeID] [int] IDENTITY(1,1) NOT NULL,

    [SomeInt] [int] NULL,

    [SomeLetters2] [char](2) NULL,

    [SomeCSV] [varchar](80) NULL,

    [SomeMoney] [money] NULL,

    [SomeDate] [datetime] NULL,

    [SomeHex09] [char](9) NULL,

    --PRIMARY KEY CLUSTERED

    --(

    --[SomeID] ASC

    --)

    )

    ;

    INSERT INTO #JBMTest

    SELECT TOP (1000000)

    --SomeID = IDENTITY(INT,1,1),

    SomeInt = ABS(CHECKSUM(NEWID()))%50000+1,

    SomeLetters2 = CHAR(ABS(CHECKSUM(NEWID()))%26+65)

    + CHAR(ABS(CHECKSUM(NEWID()))%26+65),

    SomeCSV = 'Part01,Part02,Part03,Part04,Part05,Part06,Part07,Part08,Part09,Part10',

    SomeMoney = ABS(CHECKSUM(NEWID()))%10000 /100.0,

    SomeDate = CAST(RAND(CHECKSUM(NEWID()))*3653.0+36524.0 AS DATETIME),

    SomeHex09 = RIGHT(NEWID(),09)

    FROM sys.all_columns t1

    CROSS JOIN sys.all_columns t2

    ;

    --===== Duration and HouseKeeping

    PRINT 'Duration = ' + CONVERT(CHAR(12),GETDATE()-@StartTime,114);

    DROP TABLE #JBMTest

    ;

    GO 5

    RAISERROR('===============================================================',0,1) WITH NOWAIT;

    Here are the run results in my i5 laptop. 5 runs each. The test code clears cache at the system level so make sure you don't run it on a production box.

    Beginning execution loop

    ===============================================================

    ========== SELECT/INTO

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:03:460

    ===============================================================

    ========== SELECT/INTO

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:03:480

    ===============================================================

    ========== SELECT/INTO

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:03:647

    ===============================================================

    ========== SELECT/INTO

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:03:467

    ===============================================================

    ========== SELECT/INTO

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:03:497

    Batch execution completed 5 times.

    Beginning execution loop

    ===============================================================

    ========== Precreate Table with Index

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:04:070

    ===============================================================

    ========== Precreate Table with Index

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:04:177

    ===============================================================

    ========== Precreate Table with Index

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:04:113

    ===============================================================

    ========== Precreate Table with Index

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:04:123

    ===============================================================

    ========== Precreate Table with Index

    ===============================================================

    (1000000 row(s) affected)

    Duration = 00:00:03:997

    Batch execution completed 5 times.

    ===============================================================

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)