Casecommons / pg_search

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search
http://www.casebook.net
MIT License
1.33k stars 372 forks source link

pg_search_scope : how to prevent auto builded nested queries ? #307

Open itkin opened 8 years ago

itkin commented 8 years ago

Hello,

I've the pg_search implemtentation :

class Product
  include PgSearch
  pg_search_scope :search_by_title, against: :title,
    using:{
      tsearch: {
        dictionary: 'simple',
        prefix: true,
        tsvector_column: 'tsv'
      }
}
end

here is the query produced by Porduct.search_by_title('windo') :

SELECT "products".* FROM "products" 
INNER JOIN (SELECT "products"."id" AS pg_search_id, (ts_rank(("products"."tsv"), (to_tsquery('simple', ''' ' || 'windo' || ' ''' || ':*')), 0)) AS rank FROM "products" WHERE ((("products"."tsv") @@ (to_tsquery('simple', ''' ' || 'windo' || ' ''' || ':*'))))) AS pg_search_0a3e27b8ca818264d75c8d 
ON "products"."id" = pg_search_0a3e27b8ca818264d75c8d.pg_search_id  
ORDER BY pg_search_0a3e27b8ca818264d75c8d.rank DESC, "products"."id" ASC

I'm wondering why is pg_search nesting queries (killing the perf) instead of building something like that :

SELECT "products".*, (ts_rank(("products"."tsv"), (to_tsquery('simple', ''' ' || 'wind' || ' ''' || ':*')), 0)) AS rank 
FROM "products" 
WHERE ((("products"."tsv") @@ (to_tsquery('simple', ''' ' || 'wind' || ' ''' || ':*'))))
ORDER BY rank DESC, "products"."id" ASC

What am i missing ? thx for the help ...

jjohnson commented 7 years ago

In support of more ActiveRecord::Relation methods, according to changelog, https://github.com/Casecommons/pg_search/blob/master/CHANGELOG.md#100

Explained more here, https://github.com/Casecommons/pg_search/issues/292#issuecomment-202604151