Proper full text index Rails/PostgreSQL/pg_search

后端 未结 1 1802
闹比i
闹比i 2021-01-02 07:28

I am testing performance for PostgreSQL full text search (using pg_search gem) and solr (sunspot_solr gem).

For 4 million records I am getting 13456 ms for

相关标签:
1条回答
  • 2021-01-02 08:04

    This expression:

    to_tsvector('simple', (COALESCE(title::TEXT), ''))
    

    is not sargable against your index.

    You should declare the index on the exactly that expression which is used in the query:

    CREATE INDEX products_gin_title
    ON products
    USING GIN(to_tsvector('simple', COALESCE(title::TEXT,'')))
    

    (or make ruby generate the expression which is used in the index).

    If you want multiple columns to be indexed, just concatenate them:

    CREATE INDEX products_gin_title
    ON products
    USING GIN(to_tsvector('simple', title || ' ' || product_type || ' ' || platform_id))
    

    but again, Ruby should be filtering on exactly same expression for the index to be of use.

    0 讨论(0)
提交回复
热议问题