简体   繁体   English

日志数据库中的主键

[英]Primary keys in a logging database

I'm creating a database that is going to be logging PC usage. 我正在创建一个将记录PC使用情况的数据库。 There will be several entries of the format: Computer, User, AuditTime. 将有几种格式的条目:计算机,用户,审计时间。 It is for keeping track of who was logged in to what computer at the time an audit was performed. 它用于跟踪在执行审核时谁登录到了哪台计算机。 Computer and User reference tables of all the computers and users in our company and their respective departments. 公司及其各自部门中所有计算机和用户的计算机和用户参考表。

Should I make Computer and AuditTime together a composite key? 我应该将Computer和AuditTime一起组合在一起吗? Is there a performance hit or gain from bothering to specify a primary key when really, a unique constraint on this table is unnecessary? 确实不需要此表的唯一约束时,麻烦指定主键是否会对性能产生影响或提高?

The database is MS SQL Server 2008. 该数据库是MS SQL Server 2008。

Edit : Vote for Marc_s! 编辑 :为马克投票投票!

There is a performance hit by having a primary key -- that's why they aren't generally configured in tables with high insertion, low reading use (IE: logs). 具有主键会对性能造成影响-这就是为什么通常不将它们配置在具有高插入,低读取使用率(IE:日志)的表中的原因。 But a clustered key is good. 但是群集密钥是好的。

SQL Server makes your primary keys your clustering keys by default (unless you specifically tell it not to), and as Kimberly Tripp shows in her blog post The Clustered Index Debate Continues , having a clustered index on a table is beneficial for any operation - including INSERT and DELETE - as long as it's the right type of clustered index. 默认情况下,SQL Server将主键设置为群集键(除非您明确要求不要这样做),并且正如Kimberly Tripp在其博客文章《聚簇索引辩论》中所显示的那样,在表上具有聚簇索引对任何操作都有利-包括INSERT和DELETE-只要它是正确的聚簇索引类型。

A good clustered index should be: 一个好的聚簇索引应该是:

  • narrow 狭窄
  • unique 独特
  • stable (no changing) 稳定(无变化)
  • ever increasing 不断增加

The best fit would be a INT IDENTITY surrogate key - I would advise against compound indices for the vast majority of cases. 最好的选择是INT IDENTITY代理键-在大多数情况下,我建议不要使用复合索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM