Postgresql Postgres引擎在jsonb字段属性上的异常行为
嗨,我有这个关于postgres 12的问题Postgresql Postgres引擎在jsonb字段属性上的异常行为,postgresql,jsonb,Postgresql,Jsonb,嗨,我有这个关于postgres 12的问题 select count(*) from modulo730.ods_t_record as r left join( select * from modulo730.ods_t_ire as oti where cod_azienda = '080239' and not cod_scenario = 'CUPDT' ) B on r.co
select
count(*)
from
modulo730.ods_t_record as r
left join(
select
*
from
modulo730.ods_t_ire as oti
where
cod_azienda = '080239'
and not cod_scenario = 'CUPDT' ) B on
r.cod_evento = B.cod_evento
where
(r.uid_task = '41b7c8ea-4783-4f60-b3f2-980777b86693' )
and r.cod_azienda = '080239'
and r.rec_aggiornato ->> 'annoDocumento' = '2020'
查询的第一个版本的行为与我预期的一样,它使用字段分区cod\u azienda
它在uid\u任务
字段和作为json一部分的annoDocumento
字段上执行位图索引扫描
计数需要350毫秒
现在,如果我在json上添加另一个条件,那么我会发现最后一部分是这样的
...
and r.rec_aggiornato ->> 'annoDocumento' = '2020'
and ( r.rec_aggiornato ->> 'tipo_documento' = 'FT' )
db变得无法执行查询,甚至不能执行简单的返回。也就是说,只有一个条件(它也可以通过反转条件来工作),如果我将它们组合在一起,它会正确地执行它。。。繁荣
有什么建议吗?
谢谢
这就是计划
Gather (cost=34356.37..34356.48 rows=1 width=8)
Workers Planned: 1
Single Copy: true
-> Aggregate (cost=33356.37..33356.38 rows=1 width=8)
-> Nested Loop Left Join (cost=1410.54..33356.37 rows=1 width=0)
-> Append (cost=1410.54..18597.25 rows=1 width=18)
-> Bitmap Heap Scan on ods_t_record_mch (cost=1410.54..18597.24 rows=1 width=18)
Recheck Cond: (((cod_azienda)::text = '080239'::text) AND ((uid_task)::text = '41b7c8ea-4783-4f60-b3f2-980777b86693'::text))
Filter: (((rec_aggiornato ->> 'annoDocumento'::text) = '2020'::text) AND ((rec_aggiornato ->> 'tipo_documento'::text) = 'FT'::text))
-> Bitmap Index Scan on ods_t_record_mch_cod_azienda_uid_task_idx (cost=0.00..1410.54 rows=14612 width=0)
Index Cond: (((cod_azienda)::text = '080239'::text) AND ((uid_task)::text = '41b7c8ea-4783-4f60-b3f2-980777b86693'::text))
-> Append (cost=0.00..14759.11 rows=1 width=18)
-> Seq Scan on ods_t_ire_mch oti (cost=0.00..14759.10 rows=1 width=18)
Filter: (((cod_scenario)::text <> 'CUPDT'::text) AND ((cod_azienda)::text = '080239'::text) AND ((r.cod_evento)::text = (cod_evento)::text))
第二张桌子
CREATE TABLE modulo730.ods_t_ire (
uid varchar(36) NOT NULL,
inserter varchar(128) NULL,
insert_time timestamp NULL,
modifier varchar(128) NULL,
modify_time timestamp NULL,
uid_task varchar(36) NULL,
cod_evento varchar(128) NOT NULL,
cod_proprietario varchar(128) NULL,
cod_azienda varchar(128) NOT NULL,
tip_evento varchar(128) NOT NULL,
cod_scenario varchar(128) NOT NULL,
data_invio timestamp NULL,
flg_inviato varchar(1) NULL,
flg_ritorno varchar(1) NULL,
rec_inviato_ori jsonb NULL,
rec_inviato text NULL,
rec_ritorno text NULL,
uid_sent_file varchar(36) NULL,
flow_state varchar(50) NULL,
cod_error varchar NULL,
cod_evento_inviato varchar(128) NULL,
dat_evento timestamp NULL,
cod_evento_collegato varchar NULL,
dat_evento_collegato timestamp NULL,
ticket varchar NULL,
flg_cancellazione varchar NULL,
ticket_cancellazione varchar NULL,
cod_destinatario varchar NOT NULL,
CONSTRAINT ods_t_ire_pk PRIMARY KEY (cod_azienda, tip_evento, cod_scenario, cod_destinatario, cod_evento)
)
PARTITION BY LIST (cod_azienda);
CREATE UNIQUE INDEX ods_t_ire_cod_azienda_idx ON ONLY modulo730.ods_t_ire USING btree (cod_azienda, uid);
下面进一步指出,仅考虑json属性的一个条件的普通属性是基数较低的属性。
我不是专家,但我看到的是,在更快的情况下,它执行某种并行计算
Finalize Aggregate (cost=33423.14..33423.15 rows=1 width=8)
-> Gather (cost=33422.93..33423.14 rows=2 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=32422.93..32422.94 rows=1 width=8)
-> Parallel Hash Left Join (cost=15479.31..32422.85 rows=30 width=0)
Hash Cond: ((r.cod_evento)::text = (oti.cod_evento)::text)
-> Parallel Append (cost=1410.56..18353.88 rows=30 width=18)
-> Parallel Bitmap Heap Scan on ods_t_record_mch r (cost=1410.56..18353.73 rows=30 width=18)
Recheck Cond: (((cod_azienda)::text = '080239'::text) AND ((uid_task)::text = '41b7c8ea-4783-4f60-b3f2-980777b86693'::text))
Filter: ((rec_aggiornato ->> 'tipo_documento'::text) = 'FT'::text)
-> Bitmap Index Scan on ods_t_record_mch_cod_azienda_uid_task_idx (cost=0.00..1410.54 rows=14612 width=0)
Index Cond: (((cod_azienda)::text = '080239'::text) AND ((uid_task)::text = '41b7c8ea-4783-4f60-b3f2-980777b86693'::text))
-> Parallel Hash (cost=13160.38..13160.38 rows=72669 width=18)
-> Parallel Append (cost=0.00..13160.38 rows=72669 width=18)
-> Parallel Seq Scan on ods_t_ire_mch oti (cost=0.00..12797.04 rows=72669 width=18)
Filter: (((cod_scenario)::text <> 'CUPDT'::text) AND ((cod_azienda)::text = '080239'::text))
最终确定聚合(成本=33423.14..33423.15行=1宽度=8)
->聚集(成本=33422.93..33423.14行=2宽度=8)
计划人数:2人
->部分骨料(成本=32422.93..32422.94行=1宽=8)
->并行哈希左连接(成本=15479.31..32422.85行=30宽度=0)
哈希条件:((r.cod\u evento)::text=(oti.cod\u evento)::text)
->并行追加(成本=1410.56..18353.88行=30宽度=18)
->ods_t_记录上的并行位图堆扫描(成本=1410.56..18353.73行=30宽度=18)
重新检查条件:((cod_azienda::text='080239'::text)和((uid_任务)::text='41b7c8ea-4783-4f60-b3f2-9807777b86693'::text))
筛选器:((rec_aggiornato->>'tipo_documento'::text)='FT'::text)
->ods上的位图索引扫描\u t\u记录\u mch\u cod\u azienda\u uid\u任务\u idx(成本=0.00..1410.54行=14612宽度=0)
索引条件:((cod_azienda::text='080239'::text)和((uid_任务)::text='41b7c8ea-4783-4f60-b3f2-9807777b86693'::text))
->并行散列(成本=13160.38..13160.38行=72669宽度=18)
->并行追加(成本=0.00..13160.38行=72669宽度=18)
->ods上的并行顺序扫描(成本=0.00..12797.04行=72669宽度=18)
筛选器:((cod_场景)::text'CUPDT'::text)和((cod_azienda::text='080239'::text))
回答问题并提供答案。添加表的CREATE
语句及其索引的CREATE
语句。将示例数据添加为INSERT
语句。添加预期结果。并标记您正在使用的Postgres版本。
Finalize Aggregate (cost=33423.14..33423.15 rows=1 width=8)
-> Gather (cost=33422.93..33423.14 rows=2 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=32422.93..32422.94 rows=1 width=8)
-> Parallel Hash Left Join (cost=15479.31..32422.85 rows=30 width=0)
Hash Cond: ((r.cod_evento)::text = (oti.cod_evento)::text)
-> Parallel Append (cost=1410.56..18353.88 rows=30 width=18)
-> Parallel Bitmap Heap Scan on ods_t_record_mch r (cost=1410.56..18353.73 rows=30 width=18)
Recheck Cond: (((cod_azienda)::text = '080239'::text) AND ((uid_task)::text = '41b7c8ea-4783-4f60-b3f2-980777b86693'::text))
Filter: ((rec_aggiornato ->> 'tipo_documento'::text) = 'FT'::text)
-> Bitmap Index Scan on ods_t_record_mch_cod_azienda_uid_task_idx (cost=0.00..1410.54 rows=14612 width=0)
Index Cond: (((cod_azienda)::text = '080239'::text) AND ((uid_task)::text = '41b7c8ea-4783-4f60-b3f2-980777b86693'::text))
-> Parallel Hash (cost=13160.38..13160.38 rows=72669 width=18)
-> Parallel Append (cost=0.00..13160.38 rows=72669 width=18)
-> Parallel Seq Scan on ods_t_ire_mch oti (cost=0.00..12797.04 rows=72669 width=18)
Filter: (((cod_scenario)::text <> 'CUPDT'::text) AND ((cod_azienda)::text = '080239'::text))