Generating unique user ID
-
Good evening everyone, I want to know the best way to generate unique user ID even when number of registered users are more than two million. The current way am using to achieve this is by initiating an ID i.e 2083928937 for instance, then checking Database if this ID exists. If it does, then I will increment it by 1 then make a search again for the incremented value until no match is found and the unfound ID will be used. But am having the feeling that this will cause database issue or even slow down the site as the code have to iterate several times when the site start to have more users. So, please what is the best way to achieve this?
-
Good evening everyone, I want to know the best way to generate unique user ID even when number of registered users are more than two million. The current way am using to achieve this is by initiating an ID i.e 2083928937 for instance, then checking Database if this ID exists. If it does, then I will increment it by 1 then make a search again for the incremented value until no match is found and the unfound ID will be used. But am having the feeling that this will cause database issue or even slow down the site as the code have to iterate several times when the site start to have more users. So, please what is the best way to achieve this?
-
Ok thanks. But is the method am using a good technique?
-
Ok thanks. But is the method am using a good technique?
It depends on how you're generating the initial ID to check.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Ok thanks. But is the method am using a good technique?
No it is not a good method. You could take advantage of the identity/autoincrement field in the database or use the GUID/UUID method recommended. Currently you create the ID and then check the DB, the above methods would eliminate the check requirement. You should also have a unique index on the id field in the database.
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
-
Good evening everyone, I want to know the best way to generate unique user ID even when number of registered users are more than two million. The current way am using to achieve this is by initiating an ID i.e 2083928937 for instance, then checking Database if this ID exists. If it does, then I will increment it by 1 then make a search again for the incremented value until no match is found and the unfound ID will be used. But am having the feeling that this will cause database issue or even slow down the site as the code have to iterate several times when the site start to have more users. So, please what is the best way to achieve this?
Consider implementing a Sequence table like: SequenceName, NextID The table will consist of a single row such as UserID, 2083928937 This way you immediately know what the next UserID is. Within a transaction, create the User, then update the sequence table by 1. :java:
-
Consider implementing a Sequence table like: SequenceName, NextID The table will consist of a single row such as UserID, 2083928937 This way you immediately know what the next UserID is. Within a transaction, create the User, then update the sequence table by 1. :java:
I up vote this method :) I do it this way, in which I generate order numbers, where I have a sequence table. In MongoDB, it can generate a unique Id, based on the server, date and time which is pretty cool. In the past, I used SQL servers Unique ID with auto increments, but that backfired on me several times. I forget, but auto increment forgot the last number it was on and added a 1000 to the next number. I suppose if there was a GUID generator app that worked like Mongo's ObjectId, I would move towards that.
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
Consider implementing a Sequence table like: SequenceName, NextID The table will consist of a single row such as UserID, 2083928937 This way you immediately know what the next UserID is. Within a transaction, create the User, then update the sequence table by 1. :java:
Sorry, but I downvoted this - for a number of reasons: * It requires an additional two physical i/o calls - to get the latest value, and to update it afterwards * It requires that you have a READ lock on the sequence table; to avoid duplicates, you must read the value, insert the new record, and update it all within a transaction and without allowing any other process to read the value. At best this requires an additional lock, but at worst - if poorly coded - can leave that lock in place for a prolonged period and cause a major performance bottleneck * It creates sequential user ids; presumably you have secure password / two-factor authentication, but by using sequential numeric ids you're making it very easy for hackers as they can just generate sequential hacks on the id If sequential IDs isn't an issue (and it may not be in all cases) the simplest thing is to use an auto-increment field and return the new ID from the insert statement. Any decent DBMS will keep track without issue. In the event of a transaction rollback there may be a "missing" ID but that shouldn't (be allowed) to cause your application a problem. A method I use when generating IDs is to use a GUID value (or sometimes a truncated portion of a GUID) and simply INSERT into the table. With a unique key on the ID, then in the vanishingly small likelihood of a duplicate, the INSERT will fail. Catch the "duplicate record" exception, replace the ID with a new GUID and insert again. The performance hit from that is miniscule as it will probably never ever happen.