问题描述
什么是打破了可选1的优点和缺点:1属性到自己单独的模型
What are the pros and cons of breaking out optional 1:1 attributes into their own separate model.
例如,我正好遇到Rails的code是这样的:
For example, I just encountered Rails code like this:
class Dogs << ActiveRecord::Base
# :id (pk), :breed, :weight, :height, :tail_length
end
class DogSpotsInfo << ActiveRecord::Base
# :dog_id (pk), :spot_color, :avg_spot_size, :num_spots
end
但是,这是我怎么会做它(null作为必要离开现场字段):
But this is how I would have done it (leaving the spot fields null as necessary):
class Dogs << ActiveRecord::Base
# :id, :breed, :weight, :height, :tail_length, :spot_color, :avg_spot_size, :num_spots
end
在数据库层面,我认为唯一的区别是涉及可选属性将需要查询另一个参加?
At the DB level, I believe the only difference is queries involving the optional attributes will require another join?
还有没有其他缺点,前者的做法?有什么利弊?
Are there any other cons to the former approach? Are there any pros?
编辑:
我想上一个巨大的狗桌子上说,狗20%的点,也许有亲前一种方法比较快顺序扫描,但我不是100%肯定的是,如果这是唯一的亲好像premature优化。
I guess on a massive dogs table where say 20% of dogs have spots, maybe one pro the former approach is faster sequential scans but I'm not 100% sure about that, and if that's the only pro it seems like premature optimization.
又亲我能想到的是它使车型更小,更整洁。但是,如果这是我们的目标,或许你能做到这一点,而不会影响数据库结构,通过具有类似 has_spots:现货
?什么是这里的最佳做法?
Another pro I can think of is it keeps the models smaller and neater. But if that is the goal, perhaps could you do it without affecting the DB structure, by having something like has_spots :spot
? What is the best practice here?
推荐答案
1:1的关系经常被分解时的关系是是的类型,而不是有-A型。在扩展ER模型这就是所谓的专业化。在SQL表的世界中,这有时会被命名为类表继承。你可以看一下其中任何一个方面的最高为pretty的良好的治疗相应的主题。
1:1 relationships are frequently decomposed when the relationship is of the "Is-A" type rather than of the "Has-A" type. In the extended ER model this is called "specialization". In the world of SQL tables, this sometimes goes by the name "Class Table Inheritance". You can look either of these terms up for a pretty good treatment of the corresponding subject.
班表继承对比与单表继承,这导致与空值的单个表的地方,一个值是不相关的。这看起来像你的选择,狗和景点。
Class Table Inheritance contrasts with "Single Table Inheritance" which results in a single table with NULLS in places where a value would be irrelevant. This looks like your choice for Dogs and Spots.
在汽车总动员的世界里,一个专业化可能是表车,一为汽车,一个是卡车。 (货车被称为卡车就在我身边的池塘。)汽车和卡车是专门类型的车辆。存储在卡车,而不是在车辆的属性是无关的车辆是不卡车属性。
In the world of "Cars", a specialization might be a table for "Vehicles", one for "Autos" and one for "Trucks". ("Lorries" are called "Trucks" on my side of the pond.) Autos and Trucks are specialized types of Vehicles. The attributes that are stored in trucks and not in vehicles are attributes that are irrelevant to vehicles that are not trucks.
的目的是通常不是提高性能,但改善的查询的形式。查询是有关车的数据只能查询卡车表。查询是关于车辆数据只能查询车辆表。而涉及有关车辆和卡车的数据查询,可以查询,超过公共列连接车辆和卡车的景色。
The purpose is generally not to improve performance, but to improve the form of queries. Queries that are about truck data only can query the trucks table. Queries that are about vehicle data only can query the vehicles table. And queries that involve data about both vehicles and trucks can query a view that joins vehicles and trucks over a common column.
相反的是他人均指出,狭义的表做跑赢宽表,虽然相比索引的影响较小,并加入。
Contrary to what others have opined, narrow tables do outperform wide tables, although the effect is minor compared to indexing and joins.
您可以结合类表继承了一个名为共享主键技术,它可以查找。你得到非常快,非常容易联接相比,涉及的表给对方的其他方式。共享主密钥涉及在插入时更多的工作,因为你必须在程序控制下,从广义表中的共同价值传播到相应的专门的表格。
You can combine Class Table Inheritance with a technique called "Shared Primary Key", which you can look up. You get very fast, and very easy joins compared to other ways of relating the tables to each other. Shared Primary keys involve more work at insert time, because you have to propagate the common value from the generalized table to the appropriate specialized table under program control.
如果我在做你的情况,我会想办法利用身边你所提到的各种燃料类型的专业化。你的一些查询可能最终只扫描一个五个专业表(又名子类表),从而奔跑速度一样快五倍。
If I were doing your case, I would look for ways to exploit specialization around the various fuel types you mentioned. Some of your queries might end up scanning only one of five specialized tables (aka "subclass tables") thereby running five times as fast.
这篇关于Rails的ActiveRecord的和DB正常化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!