Postgres 中的性能问题 jsonb

Performance issues jsonb in Postgres

我在 postgres 中遇到 jsonb 的一些性能问题。我的数据库中有很多产品,每个产品都有一个名为 technical_details 的列,它保存了所有技术细节,每个都像这样

{ 
    detail: {
        unit: unit_name,
        value: value 
    }
} 

例如:

{ 
    weight: { 
        unit: ‘kg’,
        value: 1000 
    } 
}

现在我想进行如下查询:

Product Load (149.4ms) 
    SELECT "products".* FROM "products" 
    WHERE (COALESCE(CAST( nullif(technical_details#>>’{weight,value}','') AS FLOAT),0) BETWEEN 500 AND 1500)

但是如您所见,进行这样的查询需要很长时间。有人知道如何提高此类查询的性能吗?

非常感谢!

编辑: 正如德米特里所要求的: 这些是我拥有的与技术细节相关的指数:

"index_products_on_technical_details" gin (technical_details)
"index_products_on_technical_details_width" btree ((technical_details ->> 'width'::text))
"index_products_on_technical_details_weight" btree ((technical_details ->> 'weight'::text))

这是 EXPLAIN ANALYZE 查询:

Seq Scan on products  (cost=0.00..926.80 rows=22 width=1288) (actual time=0.100..30.563 rows=498 loops=1)
   Filter: ((COALESCE((NULLIF((technical_details #>> '{weight,value}'::text[]), ''::text))::double precision, 0::double precision) >= 500::double precision) AND (COALESCE((NULLIF((technical_details #>> '{weight,value}'::text[]), ''::text))::double precision, 0::double precision) <= 1500::double precision))
   Rows Removed by Filter: 3997
 Planning time: 0.236 ms
 Execution time: 30.740 ms

您可以创建基于表达式的索引来加快速度:

create index on products (
  COALESCE(CAST(nullif(technical_details#>>'{weight,value}','') AS FLOAT),0)
);

当然,查看可能不在同一单位的值有点奇怪。