Re: Slow Queries with OR's?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: psql-mail(at)freeuk(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Slow Queries with OR's?
Date: 2003-05-01 15:34:01
Message-ID: 16560.1051803241@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mr Mat psql-mail <psql-mail(at)freeuk(dot)com> writes:
> SELECT msg_id, col_date, from_line, message FROM news_messages
> WHERE
> msg_id in (
> SELECT msg_id FROM news_messages WHERE EXISTS (
> SELECT msg_id FROM news_msg_id_ng_id_link WHERE EXISTS (
> SELECT ng_id FROM news_ng_genre_link WHERE genre_id in( 1, 5 )
> )
> )
> )
> AND col_date >= '2003-01-01'
> AND col_date <= '2003-02-01'
> AND news_messages.subject ILIKE '%CISCO%'
> AND news_messages.message ILIKE '%router%' ORDER BY col_date DESC;

I'm not sure what you intend that "msg_id in (...)" to accomplish,
but you could leave it out and get exactly the same results, so I
suspect it is not doing what you intended. The EXISTS() clause is
independent of the containing SELECT, therefore will be true (or not,
but according to the explain analyze output it was true) at every
row, therefore the result of the SELECT just below IN consists of
every msg_id in news_messages, and so the IN succeeds --- slowly
and painfully --- at every row.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Johnson, Shaunn 2003-05-01 15:44:50 pg_dump question - index error?
Previous Message Mr Mat psql-mail 2003-05-01 14:54:45 Re: Slow Queries with OR's?