[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Best way to generate userid(integer value), should i use identity column or something else? Please advise

Posted on 2011-02-13
8
Medium Priority
?
234 Views
Last Modified: 2012-05-11
I have a login table which generates users and generates an id for the user. I am not sure whether to use IDENTITY_Column(autonumber) so that sql server generates a unique id by itself or use something else.
Some people advised me using identity specification lock table and causes performance issues.

At present i am using the following:
SET NOCOUNT ON;
	DECLARE @intErrorCode INT
	BEGIN TRANSACTION
		SET @TID=(select (counterfrom + currentcounter) from e_counter 
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
		insert into e_Login
		(id,Username,Password) values
		(@TID, 'U-','U-')
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
		UPDATE e_counter set currentcounter=(currentcounter + 1) where functionno=@FUNID
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
    COMMIT TRANSACTION
    
    PROBLEM:
	IF (@intErrorCode <> 0) 
		BEGIN
			ROLLBACK TRAN
			SET @TID=0
		END

Open in new window


I want to know the right approach as i need to use these for chat applications in msgs tables also where lots of records are inserted at a time by many users.
Please advise
Thanks in advance.
0
Comment
Question by:calorisplanitia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

Accepted Solution

by:
BurnieP earned 1200 total points
ID: 34884049
It depends.

Using Identity with auto increment you are 100% positive that every inserts will have a unique ID.  If you build it yourself, you need to be very cetain that if 2 inserts happen at the same time, you will not get the same generated ID which will cause trouble.  And by looking at the way you are doing it, you seem to be vulnerable to it.

I am personally using Identity with auto-increment in every table where it is required.  I never had any problems with lock issues, and never heard of any.
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 800 total points
ID: 34884052

You are better ff using IDENTITY column
There is no need for adding overhead just to get a unique ID when SQL server is already optimized to do the function for you.

An identity column does not adversely affect your performance in any way
0
 

Author Comment

by:calorisplanitia
ID: 34884063
What if there is a case like chat application where hundreds of msgs are inserted at a time?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:calorisplanitia
ID: 34884074
will the table be available for view when the records are inserted, while using identity?
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34884084

Even in such a case, an Identity column will still perform better than generating your own unique id
0
 
LVL 16

Expert Comment

by:BurnieP
ID: 34884089
It is even more better to use identity if you have hundreds of message inserted at a time.  The table will be available for view since inserting is not locking existing rows.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34886697
Hi,

You have to use the identity column and also use this column with the concatenation with the userid + identity column and store it into any varchar type datafield and also define as unique value.
0
 

Author Comment

by:calorisplanitia
ID: 35000019
Apologies,  i forgot to mark this question as completed.
I tested myself also and found that identity coulmns are much better.
Thanx to you all for helping me out.!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question