reducing number of ANDs speeds up query

From: "T(dot) E(dot) Lawrence" <t(dot)e(dot)lawrence(at)icloud(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: reducing number of ANDs speeds up query
Date: 2013-01-12 01:47:26
Message-ID: A956D05B-BE04-4F02-B4C6-9EED88E885ED@icloud.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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).

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.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargentg 2013-01-12 02:37:43 Re: psql copy from through bash
Previous Message Bruce Momjian 2013-01-12 00:55:12 Re: Libpq and multithreading