Postgresql 是否加快此查询以创建物化视图?
我在OSX上使用Postgres9.4,内存为16GB。我正在我的Postgres表上创建一个物化视图,但是它的创建速度非常慢,我想知道是否有任何方法可以加快速度 以下是显示支出项目和组织的基本表格:Postgresql 是否加快此查询以创建物化视图?,postgresql,Postgresql,我在OSX上使用Postgres9.4,内存为16GB。我正在我的Postgres表上创建一个物化视图,但是它的创建速度非常慢,我想知道是否有任何方法可以加快速度 以下是显示支出项目和组织的基本表格: Table "public.spending_item" Column | Type | Modifiers -------------------+---------------
Table "public.spending_item"
Column | Type | Modifiers
-------------------+-------------------------+--------------------------------------------------------------------
id | integer | not null default nextval('spending_item_id_seq'::regclass)
presentation_code | character varying(15) | not null
presentation_name | character varying(1000) | not null
total_items | integer | not null
actual_cost | double precision | not null
processing_date | date | not null
organisation_id | character varying(9) | not null
Table "public.organisation"
Column | Type | Modifiers
-----------+------------------------+-----------
code | character varying(9) | not null
name | character varying(200) | not null
支出项目
表中有100万行,而组织
表中大约有10000行
我想创建的具体化视图如下-基本上是每月的总开支和总项目,按演示代码和组织进行分组
(我之所以创建它,是因为我需要诸如“对于代码为0304%的组织,请按月和组织向我提供总支出”或“对于代码为030405的组织,请按月和演示代码向我提供总支出”之类的查询。我的数据每月只更改一次,因此我认为物化视图对此有意义。)
我用于创建物化视图的查询如下所示:
SELECT spending_item.processing_date,
spending_item.organisation_id,
organisation.name,
spending_item.presentation_code,
spending_item.presentation_name,
SUM(spending_item.total_items) AS items,
SUM(spending_item.actual_cost) AS cost
FROM organisation, spending_item
WHERE spending_item.organisation_id=organisation.code
GROUP BY spending_item.processing_date, spending_item.presentation_name,
spending_item.presentation_code, spending_item.organisation_id,
organisation.name;
查询已运行了许多小时,尚未完成
这是对更严格的查询进行解释分析的结果(如上所述,但添加了WHERE expensing\u item.organization\u id,如“0101%”
):
GroupAggregate(成本=2263051.70..2291072.45行=934025宽度=86)(实际时间=60890.505..61069.808行=180圈=1)
->排序(成本=2263051.70..2265386.76行=934025宽度=86)(实际时间=60890.497..60945.558行=397717循环=1)
排序键:支出项目。处理项目。日期,支出项目。展示项目名称,支出项目。展示项目代码,支出项目。组织id,组织名称
排序方法:外部排序磁盘:32240kB
->散列联接(成本=23877.71..2125733.64行=934025宽度=86)(实际时间=180.302..42251.826行=397717循环=1)
散列条件:((支出项目.组织机构id)::text=(组织机构代码)::text)
->支出项目上的位图堆扫描(成本=23276.92..2109954.94行=934025宽度=68)(实际时间=178.521..41743.141行=397717循环=1)
筛选器:((组织id)::text~~'0201%::text)
->支出、项目、组织、id、varchar、模式、运营、idx上的位图索引扫描(成本=0.00..23043.41行=924684宽度=0)(实际时间=136.077..136.077行=397717循环=1)
索引条件:((组织id)::text~>='0201'::text)和((组织id)::text~Hash(成本=558.13..558.13行=3413宽度=27)(实际时间=1.774..1.774行=3413循环=1)
存储桶:1024批:1内存使用率:203kB
->组织顺序扫描(成本=0.00..558.13行=3413宽度=27)(实际时间=0.004..0.991行=3413圈=1)
总运行时间:61089.664毫秒
如果您能给我提些建议,我将不胜感激。我已经将维护\u工作\u mem
,共享\u缓冲区
等设置得很高,所以我认为我在这方面没有什么可以做的
或者对于我想要运行的两种类型的查询,使用两种不同的物化视图更有意义
如果我能提供任何其他信息,请告诉我。“外部排序磁盘:32240kB”,您是否也更改了work_mem?对于此查询所使用的数据量来说,这是一个不够好的问题。请尝试50MB、100MB或类似的方式。但这不是您最大的问题,这是位图堆扫描。请看一看,您创建了哪些索引?请参阅我对低基数大键的评论:@joop谢谢。我已经更新了如果有帮助的话,请给出关于依赖关系的答案。@FrankHeikens有很多索引-关于支出项目:“支出项目”主键,btree(id),“支出项目”btree(处理日期),“支出项目”a69d813a(组织id),“支出项目”b9b2c7ab(演示代码),“支出项目组织id varchar模式运作idx”b树(组织id varchar模式运作),“支出项目展示代码69403ee04fda6522类似于”b树(展示代码varchar模式运作)
…在组织上有一个索引代码。
SELECT spending_item.processing_date,
spending_item.organisation_id,
organisation.name,
spending_item.presentation_code,
spending_item.presentation_name,
SUM(spending_item.total_items) AS items,
SUM(spending_item.actual_cost) AS cost
FROM organisation, spending_item
WHERE spending_item.organisation_id=organisation.code
GROUP BY spending_item.processing_date, spending_item.presentation_name,
spending_item.presentation_code, spending_item.organisation_id,
organisation.name;
GroupAggregate (cost=2263051.70..2291072.45 rows=934025 width=86) (actual time=60890.505..61069.808 rows=180 loops=1)
-> Sort (cost=2263051.70..2265386.76 rows=934025 width=86) (actual time=60890.497..60945.558 rows=397717 loops=1)
Sort Key: spending_item.processing_date, spending_item.presentation_name, spending_item.presentation_code, spending_item.organisation_id, organisation.name
Sort Method: external sort Disk: 32240kB
-> Hash Join (cost=23877.71..2125733.64 rows=934025 width=86) (actual time=180.302..42251.826 rows=397717 loops=1)
Hash Cond: ((spending_item.organisation_id)::text = (organisation.code)::text)
-> Bitmap Heap Scan on spending_item (cost=23276.92..2109954.94 rows=934025 width=68) (actual time=178.521..41743.141 rows=397717 loops=1)
Filter: ((organisation_id)::text ~~ '0201%'::text)
-> Bitmap Index Scan on spending_item_organisation_id_varchar_pattern_ops_idx (cost=0.00..23043.41 rows=924684 width=0) (actual time=136.077..136.077 rows=397717 loops=1)
Index Cond: (((organisation_id)::text ~>=~ '0201'::text) AND ((organisation_id)::text ~<~ '0202'::text))
-> Hash (cost=558.13..558.13 rows=3413 width=27) (actual time=1.774..1.774 rows=3413 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 203kB
-> Seq Scan on organisation (cost=0.00..558.13 rows=3413 width=27) (actual time=0.004..0.991 rows=3413 loops=1)
Total runtime: 61089.664 ms