【问题标题】:Performance issues jsonb in PostgresPostgres 中的性能问题 jsonb
【发布时间】:2015-12-15 16:52:27
【问题描述】:

我在 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

【问题讨论】:

  • “真的很长时间”不是很精确的描述。您能否提供您的索引配置(\d tableName)和EXPLAIN ANALYZE 输出?
  • “真的很长”我指的是 Rails 控制台中给出的 149.4 毫秒的产品加载时间。我用请求的信息改进了这个问题。

标签: ruby-on-rails postgresql database-performance postgresql-9.4 jsonb


【解决方案1】:

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

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

当然,查看可能并非都在同一个单位中的值有点奇怪。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 2022-01-06
    • 1970-01-01
    • 2018-12-20
    • 2019-04-22
    • 1970-01-01
    • 2022-01-03
    • 2015-10-24
    相关资源
    最近更新 更多