Re: reducing number of ANDs speeds up query

From: Eduardo Morras <emorrasg(at)yahoo(dot)es>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: reducing number of ANDs speeds up query
Date: 2013-01-12 10:52:59
Message-ID: 20130112115259.f1e12624e96f1a343759a1b7@yahoo.es
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, 12 Jan 2013 02:47:26 +0100
"T. E. Lawrence" <t(dot)e(dot)lawrence(at)icloud(dot)com> wrote:

> Hello,
>
> I have a pretty standard query with two tables:
>
> SELECT table_a.id FROM table_a a, table_b b WHERE ... AND ... AND b.value=...;
>
> With the last "AND b.value=..." the query is extremely slow (did not wait for it to end, but more than a minute), because the value column is not indexed (contains items longer than 8K).

You can construct your own home made index, add a new column in table b, with the first 8-16 bytes/chars of b.value, use this column on your query and refine to a complete b.value. Don't forget tocCreate an index for it too. You can keep this column updated with a trigger.

Perhaps you can use a partial index for b.value column, i never used that feature so documentation/others can point you how to do it.

> However the previous conditions "WHERE ... AND ... AND" should have already reduced the candidate rows to just a few (table_b contains over 50m rows). And indeed, removing the last "AND b.value=..." speeds the query to just a millisecond.
>
> Is there a way to instruct PostgreSQL to do first the initial "WHERE ... AND ... AND" and then the last "AND b.value=..." on the (very small) result?
>
> Thank you and kind regards,
> T.

--- ---
Eduardo Morras <emorrasg(at)yahoo(dot)es>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message T. E. Lawrence 2013-01-12 11:38:13 Re: reducing number of ANDs speeds up query
Previous Message Alban Hertroys 2013-01-12 10:47:16 Re: reducing number of ANDs speeds up query