Guid.NewGuid() VS a random string generator from Random.Next()

后端 未结 7 1727
攒了一身酷
攒了一身酷 2021-02-01 17:51

My colleague and I are debating which of these methods to use for auto generating user ID\'s and post ID\'s for identification in the database:

One option uses a single

7条回答
  •  花落未央
    2021-02-01 17:54

    "Auto generating user ids and post ids for identification in the database"...why not use a database sequence or identity to generate keys?

    To me your question is really, "What is the best way to generate a primary key in my database?" If that is the case, you should use the conventional tool of the database which will either be a sequence or identity. These have benefits over generated strings.

    1. Sequences/identity index better. There are numerous articles and blog posts that explain why GUIDs and so forth make poor indexes.
    2. They are guaranteed to be unique within the table
    3. They can be safely generated by concurrent inserts without collision
    4. They are simple to implement

    I guess my next question is, what reasons are you considering GUID's or generated strings? Will you be integrating across distributed databases? If not, you should ask yourself if you are solving a problem that doesn't exist.

提交回复
热议问题