首页 > 解决方案 > 行或列的大小是否会影响 Postgresql 中的聚合查询?

问题描述

考虑下面的表定义:

    Column       |           Type           | Collation | Nullable |    Default    
-----------------+--------------------------+-----------+----------+-------------
 id              | uuid                     |           | not null | 
 reference_id    | text                     |           |          | 
 data            | jsonb                    |           |          | 
 tag             | character varying(255)   |           |          | 
 created_at      | timestamp with time zone |           |          | 
 updated_at      | timestamp with time zone |           |          | 
 is_active       | boolean                  |           | not null | true
 status          | integer                  |           |          | 0
 message         | text                     |           |          | 
 batch_id        | uuid                     |           | not null | 
 config          | jsonb                    |           |          | 

总表大小超过 500M,并且表中的每一行都包含一个数据列,其 JSON 超过 50MB。

问题 -

列的大小是否会data影响聚合,例如count?假设我们正在运行以下查询 -

select count(*)
from table 
where batch_id = '88f30539-32d7-445c-8d34-f1da5899175c';

列的大小是否会data影响聚合,例如sum?假设我们正在运行以下查询 -

查询 1 -

select sum(data->>'count'::int)
from table 
where batch_id = '88f30539-32d7-445c-8d34-f1da5899175c';

查询 2 -

select sum(jsonb_array_length(data->'some_array'))
from table 
where batch_id = '88f30539-32d7-445c-8d34-f1da5899175c';

标签: postgresqlquery-performance

解决方案


最好的了解方法是测量。

一旦数据足够大以始终进行 TOAST,那么它的大小将不再影响不需要访问 TOAST 数据内容的查询的性能,例如您的第一个。您的最后两个确实需要访问内容,它们的性能将取决于大小。


推荐阅读