在数据库中使用继承的优点和缺点是什么
我正在 Postgresql 中设计一个数据库。我想知道使用继承的优点和缺点是什么。
我还想知道以下内容:
对数据库性能的影响(即插入、更新、删除、索引等)?
父/子是否意味着重复输入[内部]?
它在Postgresql数据库中常用吗?
除了易用性之外,它比使用 FK 更好吗?
除了是否应该与 in Reason 一起使用来存储在整个数据库中使用的通用和重复属性(例如 id、名称、时间戳等)
I am designing a database in Postgresql. I would like to know what are the pros and cons of using inheritance.
I would also like to know the following :
Effects on database performance (i.e. insert,update,delete, indexing,etc) ?
Does parent/child mean duplicated input [ internally ] ?
Is it used commonly in Postgresql databases ?
How is it better than using FK other than ease of use ?
Should it be used with in reason to store generic and repetitive attributes that are used throughout the database (e.g. id, name, time stamps,etc)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
影响不大,因为达到相同结果的其他技术也会对性能产生影响。
你的意思是重复数据?不。
据我所知,不知道,但公平地说,这并没有说太多。
我认为这不是一个好主意。继承关系应该是有意义的,如果它现在只是用来节省你的一点工作,那么它只会让你和其他人感到困惑。
就我个人而言,我不使用表继承,除非它解决了真正的问题。还有其他适合关系模型的方法,可以将类层次结构映射到表,这些方法对于许多用例来说效果更好或同样好。
Not affected much because other techniques to reach the same result would also have an impact on performance.
You mean duplicated data? No.
Not that I know of, but to be fair that doesn't say that much.
I don't think that is a good idea. Inheritance relationships should be meaningfull if it is only used to save you a little work now it will only serve to confuse you and others later.
Personally I use no table inheritance unless it solves a real problem. There are other methods that fit within the relation model to map class hierarchies to tables which work better or equally well for many use cases.
在简单浏览了 mu 指出的教程后,
“对数据库性能的影响(即插入、更新、删除、索引等)?”
性能考虑可能正是发明该结构的原因。
“父/子是否意味着重复输入[内部]?”
大概不是。看起来内部实现更像是基于 ROWID() 之类的东西。如果我必须实现这样的功能,我会这样做,并且我怀疑任何 DBMS 工程师都会有不同的想法。
“除了易用性之外,它比使用 FK 更好吗?”
我会远离它并使用 FK 的“正确”设计。 “易于使用”可能是这种继承技术的一个品质,只有当你足够表面地看待它时才存在。我预计表面之下会潜伏着许多令人不快的意外,例如本教程末尾记录的一些意外。据我了解,关于仍然允许重复行的键声明的一个对我来说只是一个杀手。我的意思是,允许重复的键,你能有多疯狂?
我不这样做的另一个原因是我不确定这是否是标准 SQL。
“它应该与 in Reason 一起使用吗?”
如果键不再是唯一性的声明,我唯一想知道的是所有的 Reason 都去了哪里。
After having taken a brief look in the piece of tutorial mu pointed to,
"Effects on database performance (i.e. insert,update,delete, indexing,etc) ?"
Performance considerations were probably the very reason why the construct was invented.
"Does parent/child mean duplicated input [ internally ] ?"
Presumably not. It looks more like the internal implementation will be based on things like ROWID(). I would go that way if I had to implement such a feature, and I doubt any DBMS engineer would think differently.
"How is it better than using FK other than ease of use ?"
I would stay away from it and use "proper" design with FKs. "Ease of use" might be a quality of this inheritance technique that exists only if you look at it superficially enough. I expect there will be many unpleasant surprises lurking under the surface such as the few ones documented at the end of the tutorial. The one about key declarations that would still allow duplicate rows, as far as I understand it, is just a killer for me. I mean, keys that allow duplicates, how insane can you get ?
Another reason I'd stay away from this is that I don't know for sure whether or not this is standard SQL.
"Should it be used with in reason ..."
If keys are no longer declarations of uniqueness, the only thing I can wonder is where all the reason has gone.
我已经成功地使用了表继承,但仅限于许多表所需的公共属性,而不是“类”继承。
像这样的事情:
我使用
base
来保存多个表所需的数据。请注意,我实际上并没有在基表中放入任何东西(通过撤销所有权限来强制执行)。每个子表都存储自己的uuid、名称等。这种方法实际上只是节省了复制/粘贴。这可能不是一个巨大的节省,因为每个子表仍然需要有 PK、FK 和 PK。单独定义的索引。这样做的缺点是,您无法在没有联合的所有表中按
name
进行查询。如果您尝试进行类继承,这可能是一个要求。像具有员工子类的人员这样的东西可以更好地建模为具有公共数据的人员表和具有与人员一对一链接的“子类”数据的员工表。这应该表现得很好,因为你将通过 PK 加入。搜索将查询人员表,然后您可以对员工数据进行外连接(使用 NULL 来暗示人员与员工)。
I have used table inheritance successfully, but only for common attributes needed by many tables, not for "class" inheritance.
Something like this:
Where I use
base
to hold data needed by a number of tables. Note that I don't actually put anything in the base table (enforced by revoking all privileges). Each child table stores its own uuid, name, etc. This method really just saves copy/paste. This may not be a huge savings since each child table still needs to have PKs, FKs & indexes defined separately.The downside of this is that you can't do queries by
name
across all tables without unions. If you are trying to do class inheritance this may be a requirement.Something like person with an employee subclass may be better modeled as a a person table with common data and an employee table with 'subclass' data that has a 1-to-1 link to person. This should perform pretty well since you would be joining by PK. Searches would query the person table and then you can do outer joins for employee data (using
NULL
to imply person vs employee).