【问题标题】:Postgres partitioning order by performancePostgres 按性能划分顺序
【发布时间】:2011-06-07 16:41:15
【问题描述】:

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

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

首先,一些设置强制 postgres 做我想做的事: 设置约束排除=打开; 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)

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

【问题讨论】:

    标签: postgresql optimization partitioning


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,9.1 出来的时候我很期待 :)
    • 测试版已经发布,因此如果您的数据不是绝对关键任务,您可能想立即尝试。我已经使用它几个星期了,就我一直在尝试它而言,它非常稳定。
    猜你喜欢
    • 2011-11-07
    • 2023-03-28
    • 2012-09-18
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2013-08-14
    相关资源
    最近更新 更多