简体   繁体   English

手动为主键创建索引是一种好习惯吗?

[英]Is it a good practice to manually create indexes for the primary keys?

I am recently working with Entity Framework Core and I have some issue about the relation between the primary key and the indexes. 我最近正在使用Entity Framework Core,并且对于主键和索引之间的关系存在一些问题。

To be more concrete, I found out that in a table containing composite primary keys an index is created for the second property of the key. 更具体地说,我发现在包含复合主键的表中,为该键的第二个属性创建了一个索引。

You can see an example here 你可以在这里看到一个例子

Can you explain me if I should manually create another index for the first one? 如果我应该为第一个索引手动创建另一个索引,您能解释一下吗? Or is a clustered index created for that first property? 还是为该第一个属性创建聚簇索引?

Generally an index on a set of columns can be used even if a query is only searching some of the columns, with the restriction that the query must ask for columns from the index left to right, no gaps 通常,即使查询仅搜索某些列,也可以使用一组列上的索引,但条件是查询必须从左到右要求索引中的列,且没有空格

Thus if a set of columns A,B,C,D are indexed, this index can still be used to answer queries that are filtering on A , A and B , A and B and C . 因此,如果索引了一组列A,B,C,D,则该索引仍可用于回答对AA and BA and B and C进行过滤的查询。

Thus you don't need to index NoteID separately, because the index that aids the primary key (NoteID, CategoryID) can be used by queries calling for just NoteID. 因此,您不需要单独为NoteID编制索引,因为辅助主键的索引(NoteID,CategoryID)可以被仅用于NoteID的查询使用。 It cannot, however, be used to answer queries calling for just CategoryID, hence the separate index being created 但是,它不能用于回答仅要求CategoryID的查询,因此将创建单独的索引

As an aside, you might find, in some cases, that you can supply values in a where clause that have no purpose other than to encourage use of an index that covers them. 顺便说一句,在某些情况下,您可能会发现可以在where子句中提供值,这些值除了鼓励使用覆盖它们的索引外没有其他目的。 Suppose, for example, that a table has an index on Name, Gender, Age , and you want all 20 year old people named Steven. 例如,假设一个表在Name, Gender, Age上有一个索引,并且您希望所有20岁的人都叫Steven。 If you can reasonably assert that Steven is always male, you can WHERE Name = 'Steven' AND Gender = 'M' AND Age = 20 - even though the Gender of M is redundant, specifying it will let the DB engine use that index. 如果可以合理地断言Steven是永远的男性,则可以在其中WHERE Name = 'Steven' AND Gender = 'M' AND Age = 20即使M的性别是多余的,指定它也可以使DB引擎使用该索引。 Omitting it means the DB will have a much harder job of figuring out whether to use the index or not 省略它意味着数据库将很难确定是否使用索引。

You can also re-arrange index ordering to help your application perform, and give the DB fewer indexes to maintain.. If all your queries will only ever ask for A, A+C or A+B+C, it would be better to specify the index for the columns in the order A,C,B then a single index can cover all queries, rather than having to maintain an index of A+B+C and another of A+C 您还可以重新排列索引顺序,以帮助您的应用程序执行,并减少数据库维护的索引。如果所有查询只询问A,A + C或A + B + C,则最好以A,C,B的顺序指定列的索引,则单个索引可以覆盖所有查询,而不必维护A + B + C和A + C的索引

You don't need to create an index on NoteID because it is the first column of the primary key. 您不需要在NoteID上创建索引,因为它是主键的第一列。 Generally, you want all foreign keys to be the first column in at least one index. 通常,您希望所有外键都成为至少一个索引中的第一列。 In your case, NoteID is the first column in the primary key which acts as a clustered unique index. 在您的情况下,NoteID是主键中的第一列,它充当聚集的唯一索引。

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

相关问题 如何让用户手动输入主键 - How to let the user to enter primary keys manually 创建ContainerBuilder的新实例是一种好习惯吗? - Is it a good practice to create a new instance of ContainerBuilder? 创建自己的自定义ValidationSummary是一个好习惯吗? - It is good practice to create our own custom ValidationSummary? 为特定设置创建类是一种好习惯吗? - Is it good practice to create classes for specific settings? 这是好习惯吗? - Is this good practice? 在同一个表的主键上创建外键约束的不良做法? - Bad practice to create a foreign key constraint on primary key of the same table? 为每个数据表的主键创建自定义类型是一个好主意吗? - Is it a good idea to create a custom type for the primary key of each data table? 创建一个API来处理对多个API的调用以进行集成的优良作法 - Is it good practice to create an API to handle calls to multiple APIs for integration 如何在ASPNETCORE 3中创建通用的Host类扩展方法? - How to and is good practice to create generic Host class extension methods in ASPNETCORE 3? log4net-为跟踪目的创建许多记录器是一种好习惯吗? - log4net - is it a good practice create many loggers for tracing purposes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM