Postgres按性能划分顺序

时间:2011-06-07 16:41:16

标签: postgresql optimization partitioning

我在documentation使用规则之后使用分区的postgres表,使用基于日期范围的分区方案(我的日期列是一个纪元整数)

问题是选择具有分片列最大值的行的简单查询不使用索引:

首先,一些设置强制postgres做我想要的:     SET constraint_exclusion = on;     SET enable_seqscan = off;

单个分区上的查询有效:

explain (SELECT * FROM urls_0 ORDER BY date_created ASC  LIMIT 1);
Limit  (cost=0.00..0.05 rows=1 width=38)
  ->  Index Scan using urls_date_created_idx_0 on urls_0  (cost=0.00..436.68 rows=8099 width=38)

但是,整个表上的相同查询是seq扫描:

explain (SELECT * FROM urls ORDER BY date_created ASC  LIMIT 1);
Limit  (cost=50000000274.88..50000000274.89 rows=1 width=51)
   ->  Sort  (cost=50000000274.88..50000000302.03 rows=10859 width=51)
         Sort Key: public.urls.date_created
         ->  Result  (cost=10000000000.00..50000000220.59 rows=10859 width=51)
               ->  Append  (cost=10000000000.00..50000000220.59 rows=10859 width=51)
                     ->  Seq Scan on urls  (cost=10000000000.00..10000000016.90 rows=690 width=88)
                     ->  Seq Scan on urls_15133 urls  (cost=10000000000.00..10000000016.90 rows=690 width=88)
                     ->  Seq Scan on urls_15132 urls  (cost=10000000000.00..10000000016.90 rows=690 width=88)
                     ->  Seq Scan on urls_15131 urls  (cost=10000000000.00..10000000016.90 rows=690 width=88)
                     ->  Seq Scan on urls_0 urls  (cost=10000000000.00..10000000152.99 rows=8099 width=38)

最后,date_created的查找可以正确使用禁令排除和索引扫描:

explain (SELECT * FROM urls where date_created = 1212)
Result  (cost=10000000000.00..10000000052.75 rows=23 width=45)
   ->  Append  (cost=10000000000.00..10000000052.75 rows=23 width=45)
         ->  Seq Scan on urls  (cost=10000000000.00..10000000018.62 rows=3 width=88)
               Filter: (date_created = 1212)
         ->  Index Scan using urls_date_created_idx_0 on urls_0 urls  (cost=0.00..34.12 rows=20 width=38)
               Index Cond: (date_created = 1212)

有没有人知道如何使用分区,以便这种类型的查询将使用索引扫描?

1 个答案:

答案 0 :(得分:4)

Postgresql 9.1知道如何优化开箱即用。

在9.0或更早版本中,您需要手动分解查询,方法是通过/ limit语句将每个子查询与它们自己的顺序联合起来。