0

我有这个查询,我试图根据给定的过滤器获取记录总数,并尝试在单个查询中获取分页所需的有限数量的数据。早些时候,我在一个两步过程中执行此操作,其中我首先根据 优化 PostgreSql 中为多个分组字段选择的行数计算记录数 ,然后根据页面大小在不同的查询中获得所需的行数和所需的偏移量

EXPLAIN select count (1) OVER () AS numrows , lower(column1) as column1, column2, column3, column4, column5, column6, column7, column8, sum(column9) as column9, sum(column10) as column10 
from tableName tablename 
where 
    column8 in (SOME 50-100 sets) 
    and column_date >= '2013-09-01' 
    and column_date < '2013-09-30' 
    group by lower(column1), column2, column3, column4, column5, column6, column7, column8 
    ORDER BY column9 desc 
    LIMIT 1000 OFFSET 0

XN Limit  (cost=1000134721702.61..1000134721702.63 rows=1000 width=67)
  ->  XN Merge  (cost=1000134721702.61..1000135118514.84 rows=158724893 width=67)
        Merge Key: sum(column9)
        ->  XN Network  (cost=1000134721702.61..1000135118514.84 rows=158724893 width=67)
              Send to leader
              ->  XN Sort  (cost=1000134721702.61..1000135118514.84 rows=158724893 width=67)
                    Sort Key: sum(column9)
                    ->  XN Window  (cost=107149638.61..113101822.10 rows=158724893 width=67)
                          ->  XN Network  (cost=107149638.61..108340075.31 rows=158724893 width=67)
                                Send to slice 0
                                ->  XN HashAggregate  (cost=107149638.61..108340075.31 rows=158724893 width=67)
                                      ->  XN Seq Scan on tableName tablename  (cost=0.00..67468415.44 rows=1587248927 width=67)
                                            Filter: ((column_date < '2013-09-30'::date) AND (column_date >= '2013-09-01'::date) AND (column8 = ANY ('{SOME 50-100 sets}'::integer[])))

版本详情

PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.666 

谢谢

4

1 回答 1

4

Redshift 没有针对分页记录返回进行优化。它旨在最大限度地提高涉及许多记录但仅返回少量输出的分析查询的性能。

如果您打算对结果进行分页,我强烈建议您先将 SELECT 的输出发送到临时表。然后,您可以对该表执行简单(且快速!) COUNT(*) 并对它进行分页,而无需强制重新执行查询。

于 2013-10-14T15:06:26.430 回答