为什么主键顺序很重要? [英] Why does primary key order matter?

查看:181
本文介绍了为什么主键顺序很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在EntityFramework项目中设置了一个类,它将一个组合键指定为一个组合键。

I recently set up a class in an EntityFramework project which designated a couple of its members as a composite key.

然而,当它创建时数据库从它它给出错误

However, when it came to time to create the database from this it gave the error


无法确定类型NNNNN的复合主键排序。使用ColumnAttribute或HasKey方法来指定复合主键的顺序。

Unable to determine composite primary key ordering for type 'NNNNN'. Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

我知道如何解决问题,但我是只是想知道为什么它关心订单。不是一个复合主键,只是一组列,在其上键入记录,而不需要任何特定的排序?

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?

推荐答案

这很重要,因为主键的顺序在数据库中很重要。由于主键(通常)是集群的,并且始终是索引,无论密钥是按名字,姓氏,SSN或SSN,姓氏,名字排序都会产生巨大的差异。如果我在查询表时只有SSN,而PK是唯一的索引,那么我将看到后者具有很高的性能,而前者将扫描完整的表。

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. 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.

这篇关于为什么主键顺序很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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