pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: andrew@pillette.com
To: Tilmann Singer <tils-pgsql@tils.net>
Cc: pgsql-performance@postgresql.org
Subject: Re: Slow query with backwards index scan
Date: Sat, 28 Jul 2007 12:03:52 -0700
Message-ID: <200707281903.l6SJ3qs07818@pillette.com> (raw)

Tilmann Singer <tils-pgsql@tils.net> wrote ..
> * Nis Jørgensen <nis@superlativ.dk> [20070727 20:31]:
> > How does the "obvious" UNION query do - ie:
> > 
> > SELECT * FROM (
> > SELECT * FROM large_table lt
> > WHERE lt.user_id = 12345
> > 
> > UNION
> > 
> > SELECT * FROM large_table lt
> > WHERE user_id IN (SELECT contact_id FROM relationships WHERE user_id=12345)
> > ) q
> > 
> > ORDER BY created_at DESC LIMIT 10;

Let's try putting the sort/limit in each piece of the UNION to speed them up separately.

SELECT * FROM (
 (SELECT * FROM large_table lt
 WHERE lt.user_id = 12345
 ORDER BY created_at DESC LIMIT 10) AS q1
 UNION
 (SELECT * FROM large_table lt
 WHERE user_id IN (SELECT contact_id FROM relationships WHERE user_id=12345)
 ORDER BY created_at DESC LIMIT 10) AS q2
ORDER BY created_at DESC LIMIT 10;



view thread (12+ messages)  latest in thread

Message-ID: <200707281903.l6SJ3qs07818@pillette.com>
Permalink:  ../200707281903.l6SJ3qs07818@pillette.com/
Also on:    postgresql.org/message-id/200707281903.l6SJ3qs07818@pillette.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: andrew@pillette.com, tils-pgsql@tils.net
  Subject: Re: Slow query with backwards index scan
  In-Reply-To: <200707281903.l6SJ3qs07818@pillette.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox