Postgres - 优化对大型,紧密相关的数据集的读取

时间:2014-02-07 19:38:33

标签: sql database postgresql database-schema

我目前正在为项目构建postgres数据库。我有一些继承相同领域的不同模型。

class Animal
  int legs

class Cat < Animal
  int tail_length

class Snake < Animal
  bool is_venomous

我需要针对多达一百万动物的数据集进行快速读取进行优化,其中大多数是Cats。我担心这些关系可能会损害这些大型数据集中连接过多的性能。

我希望能够通过id获取Animal,但也能够获取所有Cats或Snakes。

我看到两种不同的方法:

具有实体化视图的外键

Animal                           Cat
+---------------+----------+     +--------------+-------------+
| primary key   | id       |     | foreign key  | animal_id   |
+---------------+----------+     +--------------+-------------+
| int           | legs     |     | int          | tail_length |
+---------------+----------+     +--------------+-------------+

然后有一个物化视图在查询之前为Cats执行连接以检索所有猫

OR

Postgres数据库继承

Animal                           Cat INHERITS (Animal);
+---------------+----------+     +--------------+-------------+
| primary key   | id       |     | int          | legs        |
+---------------+----------+     +--------------+-------------+
| int           | legs     |     | int          | tail_length |
+---------------+----------+     +--------------+-------------+

这允许来自Animal的查询select *抓住所有动物并从Cats中选择*来抓住所有猫。

优化对postgres中继承且紧密相关的大型数据集的读取的最佳方法是什么?

编辑:在写入后索引物化视图需要多长时间?

0 个答案:

没有答案
相关问题