开发者

Using natural identifiers (like an e-mail address)

Is there a particular reason why you wouldn't use a natural identifier like 开发者_JAVA技巧an e-mail address?

Currently I use Java's UUID class to create unique identifiers but for some objects they have a natural key - like a user and their e-mail address. It would make more sense (and make my code cleaner) if I used their e-mail address but I wondered if there was any particular reason you shouldnt?

Thanks

UPDATE

I am using MongoDB in this particular case.


Natural identifiers are often not constant. For example, I've updated my email address a few times over the past 20 or so years, yet I'm still the same person. It is reasonable to have an index on the email address column though; after all, people don't change them very often…


Not only are emails changed by the users (which means you would have to change all related child record which could be in the thousands or millions causing serous performance problems), they are reused by companies as people come and go. They are even occasionally shared by multiple people. They are a very poor choice for a natural key.

Natural keys need to be unique and unchanging and the shorter the better for joining. In most cases an integer key with a unique index on the natural key will perform better and be easier to maintian in the face of the key changing. A GUID key can be acceptable as well if you are doing replication or if you need to be database neutral but it generally won't perform as well as an integer key.


One reason I can think of is that a natural identifier isn't always constant. For instance, a user may, at some point, want to change their e-mail address. Using it as a key wouldn't necessarily preclude that, but it would make the change much more difficult and complex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜