何时在PostgreSQL中使用继承的表? [英] When to use inherited tables in PostgreSQL?

查看:87
本文介绍了何时在PostgreSQL中使用继承的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪些情况下应该使用继承的表?我尝试过很短地使用它们,并且在OOP世界中似乎没有继承。

In which situations you should use inherited tables? I tried to use them very briefly and inheritance didn't seem like in OOP world.

我认为它是这样工作的:

I thought it worked like this:

users 具有所有用户级别所需的所有字段。诸如主持人管理员 bloggers 之类的表,但是字段是从父母那里检查过的。例如,个用户拥有电子邮件字段,而继承的 bloggers 现在也具有该字段,但是对于用户和 blogger 。即。

Table users which has all fields required for all user levels. Tables like moderators, admins, bloggers, etc but fields are not checked from parent. For example users has email field and inherited bloggers has it now too but it's not unique for both users and bloggers at same time. ie. same as I add email field to both tables.

只有我能想到的是通常使用的字段,例如 row_is_deleted created_at modified_at

Only usage I could think of is fields that are usually used, like row_is_deleted, created_at, modified_at. Is this the only usage for inherited tables?

推荐答案

在postgres中使用表继承有一些主要原因。

There are some major reasons for using table inheritance in postgres.

可以说,我们有一些统计数据需要的表,这些表每个月都会创建和填充:

Lets say, we have some tables needed for statistics, which are created and filled each month:

statistics
    - statistics_2010_04 (inherits statistics)
    - statistics_2010_05 (inherits statistics)

在此示例中,每个表中都有2.000.000行。每个表都有一个CHECK约束,以确保仅将匹配月份的数据存储在表中。

In this sample, we have 2.000.000 rows in each table. Each table has a CHECK constraint to make sure only data for the matching month gets stored in it.

那是什么使继承成为一个很酷的功能-为什么拆分很酷数据?

So what makes the inheritance a cool feature - why is it cool to split the data?


  • 性能:选择数据时,我们选择* FROM统计信息,其中x和Y之间的日期,而Postgres仅使用表格,在有意义的地方。例如。 SELECT * FROM'2010-04-01'和'2010-04-15'之间的统计信息仅扫描表statistics_2010_04,所有其他表都不会被触及-快速!

  • 索引大小:我们没有大的肥胖表,而该日期的肥胖指数也很大。

  • 维护:我们可以在每个月的表上运行真空完全,重新索引,集群,而无需锁定所有其他数据

要正确使用表继承作为性能增强器,请查看postgresql手册。
您需要在每个表上设置CHECK约束以告知数据库,您的数据将在哪个键上进行分割(分区)。

For the correct use of table inheritance as performance booster, look at the postgresql manual. You need to set CHECK constraints on each table to tell the database, on which key your data gets splitted (partitioned).

我大量使用表继承,尤其是在存储按月分组的日志数据时。提示:如果存储永不更改的数据(日志数据),请使用CREATE INDEX ON()WITH(fillfactor = 100)创建或建立索引;这意味着索引中将不保留更新空间-磁盘上的索引较小。

I make heavy use of table inheritance, especially when it comes to store log data grouped by month. Hint: If you store data, which will never change (log data), create or indexes with CREATE INDEX ON () WITH(fillfactor=100); This means no space for updates will be reserved in the index - index is smaller on disk.

更新:
fillfactor默认为100,来自 http://www.postgresql.org/docs/9.1/static/sql-createtable.html

表的填充系数是10到100之间的百分比。默认值为100(完全打包)

The fillfactor for a table is a percentage between 10 and 100. 100 (complete packing) is the default

这篇关于何时在PostgreSQL中使用继承的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆