0

我正在对一堆物化视图测试一些查询。它们都具有相同的结构,如下所示:

EXPLAIN ANALYZE SELECT mr.foo, ..., CAST(SUM(mr.bar) AS INTEGER) AS stuff
    FROM foo.bar mr
    WHERE
        mr.a = 'TRUE' AND
        mr.b = 'something' AND
        mr.c = '12'
    GROUP BY
        mr.a,
        mr.b,
        mr.c;

显然,系统为每个查询计划提供了不同的查询计划,但如果(且仅当)WHERE 子句涉及布尔列(如示例中所示),计划程序总是在完成之前对结果集进行排序。例子:

Finalize GroupAggregate  (cost=16305.92..16317.98 rows=85 width=21) (actual time=108.301..108.301 rows=1 loops=1)
   Group Key: festivo, nome_strada, ora
   ->  Gather Merge  (cost=16305.92..16315.05 rows=70 width=77) (actual time=108.279..109.015 rows=2 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial GroupAggregate  (cost=15305.90..15306.95 rows=35 width=77) (actual time=101.422..101.422 rows=1 loops=3)
               Group Key: festivo, nome_strada, ora
               ->  Sort  (cost=15305.90..15305.99 rows=35 width=21) (actual time=101.390..101.395 rows=28 loops=3)
                     Sort Key: festivo
                     Sort Method: quicksort  Memory: 25kB
                     ->  Parallel Seq Scan on sft_vmv3_g3 mr  (cost=0.00..15305.00 rows=35 width=21) (actual time=75.307..101.329 rows=28 loops=3)
                           Filter: (festivo AND ((nome_strada)::text = '16th St'::text) AND (ora = '12'::smallint))
                           Rows Removed by Filter: 277892

我对这种方法真的很好奇,但我仍然没有找到对此的解释。

4

1 回答 1

0

我很好奇您为什么不将逻辑表述为:

SELECT true as a, 'something' as b, '12' as c, CAST(SUM(mr.bar) as INTEGER)
FROM foo.bar as mr
WHERE mr.a AND
      mr.b = 'something' AND
      mr.c = '12';

这是一个聚合查询(因为SUM()在 中SELECT)并且没有明确的GROUP BY. 我认为它应该产生一个更优化的执行计划。此外,即使没有符合条件的行,它也将始终返回一行。

于 2019-06-10T14:16:07.680 回答