Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 1837E9FB614 for ; Sat, 28 Jul 2007 16:27:15 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 39236-02 for ; Sat, 28 Jul 2007 16:27:12 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mailb.org (mailb.org [213.239.200.52]) by postgresql.org (Postfix) with ESMTP id 5006A9FB5FF for ; Sat, 28 Jul 2007 16:27:12 -0300 (ADT) Received: from localhost (e178042039.adsl.alicedsl.de [85.178.42.39]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mailb.org (Postfix) with ESMTP id EB8224573B2; Sat, 28 Jul 2007 21:27:09 +0200 (CEST) Received: by localhost (sSMTP sendmail emulation); Sat, 28 Jul 2007 21:27:13 +0200 Date: Sat, 28 Jul 2007 21:27:13 +0200 From: Tilmann Singer To: andrew@pillette.com Cc: pgsql-performance@postgresql.org Subject: Re: Slow query with backwards index scan Message-ID: <20070728192713.GH19392@tils.net> References: <200707281903.l6SJ3qs07818@pillette.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200707281903.l6SJ3qs07818@pillette.com> X-message-flag: Please consider using an e-mail program other than Outlook. It's insecure X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.158 tagged_above=0 required=5 tests=AWL=0.343, BAYES_40=-0.185 X-Spam-Level: X-Archive-Number: 200707/397 X-Sequence-Number: 26081 * andrew@pillette.com [20070728 21:05]: > 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; It's not possible to use ORDER BY or LIMIT within unioned queries. http://www.postgresql.org/docs/8.2/static/sql-select.html#SQL-UNION Would that make sense at all given the way the postgresql planner works? Does that work in other DB's? Til