简体   繁体   English

SQL Server松耦合表-性能考虑?

[英]SQL Server loosely coupled tables - performance considerations?

I am building a database schema for a new application from scratch, and my two goals are loose coupling (scalability) and performance (but performance is the MOST important). 我正在从头开始为新应用程序构建数据库架构,我的两个目标是松散耦合(可伸缩性)和性能(但是性能是最重要的)。 I am not sure if it would be a good idea to include foreign key columns in central tables. 我不确定在中央表中包含外键列是否是一个好主意。 My question would probably be best understood using an example (please keep in mind that this example is purely hypothetical): 使用示例可以更好地理解我的问题(请记住,该示例纯粹是假设的):

We have a table, let's call this table "Animal". 我们有一个表,我们称此表为“动物”。 In this table we have several entries which would define properties for various types of "Animal"s stored in the database. 在此表中,我们有几个条目,这些条目将为数据库中存储的各种类型的“动物”定义属性。 We also have another table called "AnimalName", the purpose of which is to store the name of each Animal in the "Animal" table in conjunction with a Language ID (so we have a table which stores the names of each animal in the "Animal" table in each language). 我们还有另一个名为“ AnimalName”的表,其目的是将每种动物的名称与语言ID一起存储在“ Animal”表中(因此,我们有一个表将“ “动物”表(每种语言)。

I have two ways of implementing the above tables: 我有两种方法可以实现上述表格:

First Way 第一路

Animal Table: AnimalID (PK) 动物表:AnimalID(PK)
AnimalName Table: AnimalNameID (PK), AnimalID (FK), LanguageID (FK), Name AnimalName表:AnimalNameID(PK),AnimalID(FK),LanguageID(FK),名称

and queries would look like this: 查询看起来像这样:

SELECT * FROM Animal a JOIN AnimalName an ON an.AnimalID = a.AnimalID and an.LanguageID = ? WHERE a.AnimalID = ?

Second Way 第二路

Animal Table: AnimalID (PK), AnimalNameID (FK) 动物表:AnimalID(PK),AnimalNameID(FK)
AnimalName Table: AnimalNameID (PK), LanguageID (FK), Name AnimalName表:AnimalNameID(PK),LanguageID(FK),名称

and queries would look like this: 查询看起来像这样:

SELECT * FROM Animal a JOIN AnimalName an ON an.AnimalNameID = a.AnimalNameID and an.LanguageID = ? WHERE a.AnimalID = ?

For the second way, if I was to add an "AnimalID" FK column to the AnimalName table, then it would support querying expressed in the first way as well. 对于第二种方法,如果我要在AnimalName表中添加“ AnimalID” FK列,则它也将支持以第一种方法表示的查询。

Which one of the above methods will provide the FASTEST performance (this is crucial!)? 以上哪一种方法将提供最快的性能(这很关键!)? Which one of the above methods would you generally recommend from your experience? 您通常会根据经验推荐上述哪一种方法?

Much thanks in advance to all who answer! 在此先感谢所有回答!

Only the first way models correctly the problem you described: an animal has many names, one for each language . 只有第一种方法可以正确地为您描述的问题建模: 一种动物有很多名字,每种语言都有一个名字 The second way models something along the lines of an animal has one name which happens to be in language foo , something completely different from your problem description. 用第二种方式对动物进行建模的名称只有一个名称,该名称恰好是foo语言 ,与您的问题描述完全不同。

For this kind of query as you described the AnimalNames table must be clustered uniquely by (AnimalId, LanguageId) and have the primary key as a non-clustered constraint, or even better dispose the AnimalLanguageID PK altogether and model a composite PK of (AnimalID, LanguageID) . 对于您描述的这种查询,AnimalNames表必须由(AnimalId, LanguageId)唯一地聚集(AnimalId, LanguageId)并且主键必须作为非聚集约束,甚至更好地完全处置AnimalLanguageID PK并为(AnimalID, LanguageID)

Also you must read Designing Indexes 另外,您必须阅读设计索引

The first way gives you a standard one-to-many relationship between Animals and AnimalName, allowing many names for each Animal, which makes sense. 第一种方法为您提供了Animals和AnimalName之间的标准一对多关系,从而为每个Animal提供了许多名称,这很有意义。

With the second way, each Animal gets exactly one name, and a single name can be assigned to many animals, which does not make sense. 通过第二种方法,每个动物都只能获得一个名称,并且可以为许多动物指定一个名称,这没有意义。

The second approach is better. 第二种方法更好。 AnimalName and Animal will have a 1-to-many relationship, which makes better sense here. AnimalName和Animal将具有一对多关系,在这里更有意义。

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

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