【问题标题】:Tuning Postgres Fulltext search调整 Postgres 全文搜索
【发布时间】:2016-08-25 23:54:16
【问题描述】:

表格是产品

             Table "public.product"
     Column      |           Type           |
-----------------+--------------------------+
 id              | uuid                     |
 attributes      | jsonb                    |

请注意,attributes 是一个 jsonb 字段。目前我有大约 5k 行,我正在像这样查询它:

select id, to_tsvector(attributes::text) @@ to_tsquery('22222') from product;

这个查询已经需要几秒钟才能完成,我想知道我是否可以做些什么来改善这个时间,即索引或改进的查询?

为了启动这个查询返回:

                  id                  | found 
--------------------------------------+-------
 a8230602-ff3f-4414-affc-3594abcfa617 | f
 da0c70d5-5108-42ea-941d-123589129feb | f
 24ac417a-466c-465c-b346-4fad7a9ad3d8 | f
 4bee6122-c5d7-4e0c-840e-e04c28888a9a | f
 ce5fe539-bcb2-4cec-9012-b2501df7012e | f

这是不可取的,有没有办法只返回匹配的行?

【问题讨论】:

    标签: json postgresql database-design full-text-search full-text-indexing


    【解决方案1】:

    您需要将条件移至WHERE 子句:

    SELECT *
    FROM   product
    WHERE  to_tsvector('english', attributes::text) @@ to_tsquery('22222');
    

    并在表达式上创建全文索引:

    CREATE INDEX textsearch_idx ON product
    USING GIN (to_tsvector('english', attributes::text));
    

    索引表达式和查询中的表达式必须匹配。

    Details in the manual.

    或者也许可以使用jsonb GIN索引:

    但是,如果您想同时搜索键 值,这将不起作用。

    您可能最好从...开始使用规范化的表格布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-03
      • 2012-07-25
      • 1970-01-01
      • 2019-05-16
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多