简体   繁体   English

为什么主键订单很重要?

[英]Why does primary key order matter?

I recently set up a class in an EntityFramework project which designated a couple of its members as a composite key. 我最近在EntityFramework项目中设置了一个类,它将其几个成员指定为复合键。

However, when it came to time to create the database from this it gave the error 但是,当从它创建数据库时,它给出了错误

Unable to determine composite primary key ordering for type 'NNNNN'. 无法确定类型“NNNNN”的复合主键排序。 Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys. 使用ColumnAttribute或HasKey方法指定组合主键的顺序。

I know how to solve the problem, but I was just wondering why it cares about the order. 我知道如何解决问题,但我只是想知道为什么它关心订单。 Isn't a composite primary key just a set of columns on which to key the records, without any particular ordering necessary? 复合主键只是一组用于键入记录的列,而不需要任何特定的排序吗?

It matters because the order of primary keys matter in the database. 这很重要,因为主键的顺序在数据库中很重要。 Since primary keys are (usually) clustered, and always indices, whether the key is ordered as First Name, Last Name, SSN or SSN, Last Name, First Name, makes a huge difference. 由于主键(通常)是聚类的,并且始终是索引,因此无论密钥是以名字,姓氏,SSN还是SSN,姓氏,名字排序,都会产生巨大的差异。 If I only have the SSN when I'm querying the table, and the PK is the only index, I'll see GREAT performance with the latter, and a full table scan with the former. 如果我在查询表时只有SSN,并且PK是唯一的索引,我将看到后者的GREAT性能,以及前者的全表扫描。

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

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