数据库继承的技术?
当您需要将继承的类持久化到不支持继承的关系数据库时,有哪些技巧/技巧?
假设我有一个经典的例子:
Person -> Employee -> Manager
-> Team lead
-> Developer
-> Customer -> PrivilegedCustomer
-> EnterpriseCustomer
有哪些可用的技术来设计数据库? 各自的优点和缺点?
ps 我搜索并发现了几个有关数据库继承的问题,但大多数都是关于更改为本机支持它的数据库引擎。 但假设我无法使用 SQL Server 2005...我有什么选择?
What are the tips/techniques when you need to persist classes with inheritance to relational database that doesn't support inheritance?
Say I have this classic example:
Person -> Employee -> Manager
-> Team lead
-> Developer
-> Customer -> PrivilegedCustomer
-> EnterpriseCustomer
What are the available techniques to design the database? Pros and cons of each?
p.s. I have searched and found several question regarding database inheritance but most were about changing to a database engine that supports it natively. But let's say I'm stuck with SQL Server 2005... what are my options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
三种常见策略:
为层次结构中的每个类创建一个表,其中包含为每个类定义的属性以及返回顶级超类表的外键。 因此,您可能有一个
vehicle
表,以及其他具有vehicle_id
列的表,例如car
和airplane
。 这里的缺点是,您可能需要执行大量联接才能获取一种类类型。为层次结构中的每个类创建一个包含所有属性的表。 这可能会很棘手,因为除非您使用序列之类的东西,否则在所有表中维护通用 ID 并不容易。 对超类类型的查询需要针对所有相关表进行联合。
为整个类层次结构创建一张表。 这消除了联接和并集,但要求所有类属性的所有列都位于一张表中。 您可能需要将大多数列保留为空,因为某些列不适用于不同类型的记录。 例如,
vehicle
表可能包含一个名为wingspan
的列,该列对应于Airplane
类型。 如果将此列设置为 NOT NULL,则插入表中的任何Car
实例都将需要wingspan
值,即使值为NULL
可能更有意义。 如果您将列保留为空,您可能可以通过检查约束来解决此问题,但它可能会变得丑陋。 (单表继承)Three common strategies:
Create a table for each class in the hierarchy that contain the properties defined for each class and a foreign key back to the top-level superclass table. So you might have a
vehicle
table with other tables likecar
andairplane
that have avehicle_id
column. The disadvantage here is that you may need to perform a lot of joins just to get one class type out.Create a table for each class in the hierarchy that contains all properties. This one can get tricky since it's not easy to maintain a common ID across all the tables unless you're using something like a sequence. A query for a superclass type would require unions against all the tables in question.
Create one table for the entire class hierarchy. This eliminates joins and unions but requires that all of the columns for all class properties be in one table. You'll probably need to leave most columns nullable since some columns won't apply to records of a different type. For example, the
vehicle
table might contain a column calledwingspan
that corresponds to theAirplane
type. If you make this column NOT NULL then any instance of aCar
inserted into the table will require a value forwingspan
even though a value ofNULL
might make more sense. If you leave the column nullable you might be able to work around this with check constraints but it could get ugly. (Single Table Inheritance)在某些情况下要小心数据库继承 - 我们在应用程序中实现了它以用于我们的审计策略,但我们最终遇到了性能瓶颈/噩梦。
问题是我们使用的基表仅用于插入并且变化很快,因此我们最终遇到的就是整个地方的死锁。 我们目前正计划将它们拆分成自己的表,因为在 15 个不同的表中拥有相同的列所带来的头痛和性能噩梦是值得的。 实体框架不一定能有效地处理继承(这是 Microsoft 的一个已知问题),这一事实也使情况变得更加复杂。
不管怎样,我只是想分享一些知识,因为我们已经在这个问题上绞尽脑汁了。
Be careful with database inheritance in certain situations - we implemented it in our application for our auditing strategy and we ended up with a performance bottleneck/nightmare.
The problem was that the base table we used was insert only and rapidly changing so what we ended up with were deadlocks all over the place. We are currently planning to break these apart into their own tables because the headache of having the same columns in 15 different tables versus a performance nightmare is well worth it. This was also compounded by the fact that the entity framework doesn't necessarily handle inheritance efficiently (this is a known issue by Microsoft).
Anyway, just thought I'd share some knowledge since we've been through the wringer on this issue.
以下链接中的第 8 章继承映射也对此进行了讨论。 http://nhibernate.info/doc/nh/en/index.html#继承
是NHibernate文档。
Chapter 8. Inheritance Mapping in following link also discussed this. http://nhibernate.info/doc/nh/en/index.html#inheritance
It is the NHibernate document.