Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Fastes way to increment an id column Expand / Collapse
Author
Message
Posted Sunday, May 17, 2009 12:00 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 1,893, Visits: 3,932
Why?

I use this since about 5 years. Long time before I joined SSC



The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Post #718719
Posted Monday, May 18, 2009 3:49 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 35,218, Visits: 31,677
Florian Reischl (5/17/2009)
Why?

I use this since about 5 years. Long time before I joined SSC


Heh... ok... then 5 bucks for posting a cursor solution to a set based problem .


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #719462
Posted Sunday, September 6, 2009 7:21 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:07 AM
Points: 9,923, Visits: 11,170
Hey Jeff & Flo,

Just a couple of things...

1. This isn't a true 'quirky' update unless there's a clustered index to determine the order of the ID assignment. It is true that the current SQL Server behaviour is always to return rows from heaps in insertion order (at least with a new heap that has never had any update or delete activity on it), but still.

2. This method runs in less than half the time (2005 only):

SET NOCOUNT ON

--===== Conditionally drop the test table for test reruns
IF OBJECT_ID(N'tempdb..#my_tab', N'U') IS NOT NULL DROP TABLE #my_tab;
IF OBJECT_ID(N'tempdb..#my_tab2', N'U') IS NOT NULL DROP TABLE #my_tab2;

--===== Build the test table and populate it with data on the fly
SELECT TOP 100000
ISNULL(CAST(0 AS INT), 0) AS ID,
DATEADD(mi,-ABS(CHECKSUM(NEWID())%100000),GETDATE()) AS Any_Date
INTO #My_Tab
FROM Master.dbo.SysColumns t1 CROSS JOIN Master.dbo.SysColumns t2

DECLARE @now DATETIME;
SET @now = GETDATE();

-- Bulk logged *temporary* IDENTITY addition
SELECT id = IDENTITY(INT, 1000, 1),
any_date
INTO #my_tab2
FROM #my_tab WITH (TABLOCK);

-- Fast clear of the original table
TRUNCATE TABLE #my_tab;

-- Metadata only change
ALTER TABLE #my_tab2 SWITCH TO #my_tab;

PRINT 'Duration for 100000 values: ' + CONVERT(VARCHAR(20), DATEDIFF(MILLISECOND, @now, GETDATE()));

-- Tidy up
DROP TABLE
#my_tab, #my_tab2;

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #783570
Posted Sunday, September 6, 2009 7:23 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:07 AM
Points: 9,923, Visits: 11,170
In case you guys think I'm cheating by changing the nullability of the id column, this version is a true replacement, and is just as fast:

SET NOCOUNT ON;

-- Conditional drops
IF OBJECT_ID(N'tempdb..#my_tab', N'U') IS NOT NULL DROP TABLE #my_tab;
IF OBJECT_ID(N'tempdb..#my_tab', N'U') IS NOT NULL DROP TABLE #my_tab2;
IF OBJECT_ID(N'tempdb..#my_tab', N'U') IS NOT NULL DROP TABLE #my_tab3;

-- Sample data
SELECT TOP (100000)
CAST(NULL AS INT) AS ID,
DATEADD(mi,-ABS(CHECKSUM(NEWID())%100000),GETDATE()) AS Any_Date
INTO #My_Tab
FROM master.sys.all_columns T1, master.sys.all_columns T2, master.sys.all_columns T3;

-- Start timing
DECLARE @now DATETIME;
SET @now = GETDATE();

-- Bulk logged identity add (id column is NOT NULL)
SELECT id = IDENTITY(INT, 1000, 1),
any_date
INTO #my_tab2
FROM #my_tab WITH (TABLOCK);

-- Intermediate table for nullability change
CREATE TABLE #my_tab3 (id INT NOT NULL, any_date DATETIME NULL);

-- Lose the IDENTITY attribute
ALTER TABLE #my_tab2 SWITCH TO #my_tab3;

-- Metadata change to id column nullability
ALTER TABLE #my_tab3 ALTER COLUMN id INT NULL;

-- Fast clear original table
TRUNCATE TABLE #my_tab;

-- Metadata switch data in
ALTER TABLE #my_tab3 SWITCH TO #my_tab;

-- Show duration
PRINT 'Duration for 100000 values: ' + CONVERT(VARCHAR(20), DATEDIFF(MILLISECOND, @now, GETDATE()));

-- Tidy up
DROP TABLE
#my_tab, #my_tab2, #my_tab3;





Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #783571
Posted Sunday, September 6, 2009 7:24 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:07 AM
Points: 9,923, Visits: 11,170
Jeff Moden (5/17/2009)
.. if, for some reason, you cannot use IDENTITY and you're stuck with SQL Server 2000 or less which prevents the use of ROW_NUMBER and the like, then the "quirky" update Flo used is absolutely the fastest method available.

Heh




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #783573
Posted Sunday, September 6, 2009 10:06 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 35,218, Visits: 31,677
This isn't a true 'quirky' update unless there's a clustered index to determine the order of the ID assignment


I kind of agree and kind of don't. In the code Flo posted, order meant nothing. A clustered index was not required to establish the order. And, it's still a "quirky update" in that a 3 part SET statement was used... if you don't think so, ask an Oracle user.

Paul White (9/6/2009)
Jeff Moden (5/17/2009)
.. if, for some reason, you cannot use IDENTITY and you're stuck with SQL Server 2000 or less which prevents the use of ROW_NUMBER and the like, then the "quirky" update Flo used is absolutely the fastest method available.

Heh


Heh? Good idea and good code but you forgot to copy things like indexes, privs, restrictions, triggers, and named constraints to the new table. If I recall correctly, you may also need to find and recompile any views due to the table changes but that might only be if you change the number of columns or their datatypes.

I will admit that even with those additions, the copy job you built may be quicker. I just can't verify your findings for the next week because I won't have access to an SQL computer for that time.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #783595
Posted Sunday, September 6, 2009 10:32 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:07 AM
Points: 9,923, Visits: 11,170
Heh - because the whole thing was kinda jokey - a bit of fun for an otherwise fairly tedious Monday at work.

I agree with what you say about the quirkiness (order not being important) and about the object permissions, indexes, constraints and so on. But it was just funny - to me anyway

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #783596
Posted Sunday, September 6, 2009 10:37 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:07 AM
Points: 9,923, Visits: 11,170
--- duplicate ---



Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #783602
Posted Sunday, September 6, 2009 10:41 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:07 AM
Points: 9,923, Visits: 11,170
--- duplicate ---



Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #783604
Posted Monday, September 7, 2009 2:08 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 1,893, Visits: 3,932
Hi Paul

Paul White (9/6/2009)
2. This method runs in less than half the time (2005 only):


This script was just a little trick for SQL Server 2000 and previous versions . There are a couple of better ways to handle this in SQL Server 2005/2008.

Greets
Flo



The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Post #783654
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse