pg_search icon indicating copy to clipboard operation
pg_search copied to clipboard

multisearch slow

Open robbieshepherd opened this issue 10 years ago • 9 comments

Struggling to get multisearch to work in a timely manner. I have approx 60k rows to search across, and results are taking 7-10 seconds.

My models look like:

multisearchable :against => [:heading, :tagline, :tags],
  :order_within_rank => "stories.created_at DESC",
  :ignoring => :accents,
  :using => {
    :tsearch => {:dictionary => "english"},
    :trigram => {:dictionary => "english"}
  },
  :if => :published?

My dev log shows:

  [1m[35m (3846.2ms)[0m  SELECT COUNT(*) FROM "pg_search_documents" WHERE (((to_tsvector('simple', unaccent(coalesce("pg_search_documents"."content"::text, '')))) @@ (to_tsquery('simple', ''' ' || unaccent('g20') || ' '''))) OR ((unaccent(coalesce("pg_search_documents"."content"::text, ''))) % unaccent('g20')))
  Rendered application/_search.html.haml (1.2ms)
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT COUNT(*) FROM "pg_search_documents" WHERE (((to_tsvector('simple', unaccent(coalesce("pg_search_documents"."content"::text, '')))) @@ (to_tsquery('simple', ''' ' || unaccent('g20') || ' '''))) OR ((unaccent(coalesce("pg_search_documents"."content"::text, ''))) % unaccent('g20')))[0m
  [1m[35mPgSearch::Document Load (3872.0ms)[0m  SELECT "pg_search_documents".*, ((ts_rank((to_tsvector('simple', unaccent(coalesce("pg_search_documents"."content"::text, '')))), (to_tsquery('simple', ''' ' || unaccent('g20') || ' ''')), 0))) AS pg_search_rank FROM "pg_search_documents" WHERE (((to_tsvector('simple', unaccent(coalesce("pg_search_documents"."content"::text, '')))) @@ (to_tsquery('simple', ''' ' || unaccent('g20') || ' '''))) OR ((unaccent(coalesce("pg_search_documents"."content"::text, ''))) % unaccent('g20'))) ORDER BY pg_search_rank DESC, "pg_search_documents"."id" ASC LIMIT 10 OFFSET 0

Why a simple count query is taking 4 seconds is beyond me. I've also tried storing the searchable data in a tsv_vector column as mentioned in https://github.com/Casecommons/pg_search/wiki/Building-indexes but it made no difference. Can anyone help point me as to what I'm doing wrong?

Thanks

robbieshepherd avatar Dec 29 '14 01:12 robbieshepherd

+1

umerfarooq avatar May 11 '16 18:05 umerfarooq

I'm Having a similar issue, did you guys found the root cause? Or any optimization/workaround? Thx!

benoror avatar Jun 14 '16 02:06 benoror

Did you guys find a solution?

asia653 avatar Jul 05 '17 15:07 asia653

I'm having a similar issue

kevinnguyen8699 avatar Aug 23 '18 22:08 kevinnguyen8699

I ended up switching search to a Elasticsearch instance

benoror avatar Aug 23 '18 23:08 benoror

Had to do the same

mojobiri avatar Aug 24 '18 08:08 mojobiri

I'm wondering if there's a particular reason that the pg_search_documents.content field isn't of tsvector type? Haven't dug into it too deep, but seems to me that keeping this as a text field and doing the heavy lifting every time is pretty naive?

arcreative avatar Jul 19 '19 02:07 arcreative

I'm wondering if there's a particular reason that the pg_search_documents.content field isn't of tsvector type? Haven't dug into it too deep, but seems to me that keeping this as a text field and doing the heavy lifting every time is pretty naive?

I am not using pg_search yet, but I was looking at the migration it created and wondering exactly the same thing.

grncdr avatar Mar 12 '20 13:03 grncdr

Not all pg_search scopes use tsearch.

That said, using a tsvector is a great idea. I've been wanting to move towards generating the multisearch Active Record model instead of bundling PgSearch::Document directly in the gem, but I haven't gotten around to it yet.

nertzy avatar Apr 10 '20 01:04 nertzy