Forcing index usage

From: Jonathan Marks <jonathanaverymarks(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Forcing index usage
Date: 2019-04-03 16:13:22
Message-ID: 679378F9-E085-4FE3-BC78-3073A4B86438@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi folks —

We’ve got several tables that are relatively large (~250-500GB in total size) that use very large (tens to hundreds of GB) GIN indexes for full-text search. We’ve set the column statistics for our tsvector columns as high as they go (10,000). We almost always order our search results by a separate date column (which has an index) and we almost always use a limit.

Whenever the query planner chooses to use the indexes, queries on these tables are somewhat fast, maxing out at a few hundred milliseconds per query (which is terrible, but acceptable to end users).

When the query planner chooses not to use the indexes, queries can take many tens of seconds if they ever finish at all. When this happens, the query planner usually chooses to use the date index instead of the GIN index, and that is almost always a bad idea. We have sometimes been able to trick it into a better query plan by also adding the tsvector column in the ORDER BY clause, but that has bad performance implications if the result set is large.

Is there a way to tell Postgres “please don’t use index X when queries that could use index Y instead occur?”

Thank you!

Responses

Browse pgsql-general by date

  From Date Subject
Next Message david moloney 2019-04-03 16:26:00 PostgreSQL Windows 2019 support ?
Previous Message Lou Tseng 2019-04-03 16:01:32 Lingering replication slots