Sql Server使用DateTime作为主键 [英] Sql Server using DateTime as Primary Key

查看:570
本文介绍了Sql Server使用DateTime作为主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于:



MySQL:使用DATETIME作为主键



但是我对Sql Server特别感兴趣,我想用一个我想要存储用户执行的事件/动作。



多个用户在相同的100ms间隔中执行动作的可能性非常低,不频繁的冲突是可以接受的。如果我可以离散地表达10ms甚至1ms的差距,那么我对这些风险感到非常高兴。



因此,提出了这个问题,我可以使用DateTime作为主键而不是唯一的标识符,因为我将定期查询最新的100个事件并按事件排序事件他们发生的时间。

解决方案

DateTime列可以很好地作用为聚簇索引键。但是,他们选择了一个主要的关键。看来,你混淆了两个不同的,经常混淆的概念。



请参阅如果主键始终分配为聚簇索引聚集索引应该包含什么列?


Hi my questions is similar to:

MySQL: Using DATETIME as primary key

But I'm specifically interested in Sql Server and I want to approach the question practically with a specific scenario in mind rather than theoretically as in the other post.

I want to store events/actions that users perform. The odds of more than one user performing an action in the same 100ms gap is very low and infrequent collision are acceptable. If I could discretely express 10ms or even 1ms gaps then I'm very happy with the risks.

Thus begs the question, can I use a DateTime as my primary key instead of a unique identifier, because I will be regularly querying the latest 100 events and sorting the events by the time which they occurred.

解决方案

DateTime columns can act very well as clustered index keys. But they make a poor choice of a primary key though. It seems that you are confusing the two distinct, often confused, concepts.

See Should primary keys be always assigned as clustered index, What column should the clustered index be put on?

这篇关于Sql Server使用DateTime作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆