Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Generating unique user ID

Generating unique user ID

Scheduled Pinned Locked Moved ASP.NET
questiondatabasecomregexhelp
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Otekpo Emmanuel
    wrote on last edited by
    #1

    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?

    www.emmason247.com.ng

    Richard DeemingR D 2 Replies Last reply
    0
    • O Otekpo Emmanuel

      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?

      www.emmason247.com.ng

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Use a Guid / UUID. That way, you'll also mitigate any IDOR[^] issues at the same time.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      O 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Use a Guid / UUID. That way, you'll also mitigate any IDOR[^] issues at the same time.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        O Offline
        O Offline
        Otekpo Emmanuel
        wrote on last edited by
        #3

        Ok thanks. But is the method am using a good technique?

        www.emmason247.com.ng

        Richard DeemingR M 2 Replies Last reply
        0
        • O Otekpo Emmanuel

          Ok thanks. But is the method am using a good technique?

          www.emmason247.com.ng

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • O Otekpo Emmanuel

            Ok thanks. But is the method am using a good technique?

            www.emmason247.com.ng

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • O Otekpo Emmanuel

              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?

              www.emmason247.com.ng

              D Offline
              D Offline
              David Mujica
              wrote on last edited by
              #6

              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:

              J D 2 Replies Last reply
              0
              • D David Mujica

                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:

                J Offline
                J Offline
                jkirkerx
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • D David Mujica

                  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:

                  D Offline
                  D Offline
                  DerekT P
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups