Use item specific prefixes and autonumber for primary keys?

后端 未结 9 1458
孤街浪徒
孤街浪徒 2020-12-05 20:12

We had a meeting this morning about how would should store our ID for some assets that we have in our database that we are making, the descusion generated a bit of heat so I

相关标签:
9条回答
  • 2020-12-05 20:48

    I'd go for the former. Creating unique IDs should be left to the SQL server, and you can't have those created automagically in a thread-safe manner if they're strings. To my understanding you'd have to handle that yourself somehow?

    Speed is another factor. Dealing with int values is always going to be faster than strings. I'd say that there are other perf benefits around indexing that a much more SQL savvy person than me could elaborate on ;)

    In my experience, having string IDs has been a fail.

    0 讨论(0)
  • 2020-12-05 20:53

    I prefer Example 1 for the reasons you mentioned and the only argument that I can think of for using Example 2 is if you are trying to accomodate string IDs from an existing database (quite common) however even in that scenario, I prefer to use the following approach.

    ==AssetId(PK)==Type========DeprecatedId====
      12345        "Manhole"   "MH64247"
      155415       "Pit"       "P6487246"
    
    0 讨论(0)
  • 2020-12-05 20:54

    I would choose a numeric primary key for performance reasons. Integer comparisons are much cheaper than string comparisons, and it will occupy less space in the DB.

    0 讨论(0)
提交回复
热议问题