Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtNTy-0007bs-Kf for pgsql-performance@arkaria.postgresql.org; Wed, 18 Dec 2013 20:12:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VtNTx-0003fx-VN for pgsql-performance@arkaria.postgresql.org; Wed, 18 Dec 2013 20:12:38 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtNTx-0003fr-1W for pgsql-performance@postgresql.org; Wed, 18 Dec 2013 20:12:37 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtNTp-0001vs-PI for pgsql-performance@postgresql.org; Wed, 18 Dec 2013 20:12:36 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1VtNTp-000062-2E for pgsql-performance@postgresql.org; Wed, 18 Dec 2013 12:12:29 -0800 Date: Wed, 18 Dec 2013 12:12:29 -0800 (PST) From: David Johnston To: pgsql-performance@postgresql.org Message-ID: <1387397549054-5783942.post@n5.nabble.com> In-Reply-To: <1387394601821-5783927.post@n5.nabble.com> References: <52A7A428.8070104@gmail.com> <1386746318622-5782822.post@n5.nabble.com> <1387394601821-5783927.post@n5.nabble.com> Subject: Re: Problem with slow query with WHERE conditions with OR clause on primary keys MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 2.8 (++) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org kolszew73@gmail.com wrote > Thanx for your answer > > My example is trivial because i want to show strange (for me) postgres > behavior with dealing with primary keys (extreme example), in real > situation user put search condition e.g. "Panas" and this generates query > ... > where gd.other_code like 'Panas%' OR g.code like 'Panas%' > .. > > both columns has very good indexes and selectivity for "like 'Panas%'" ... > > I have experience from Oracle with this type of queries, and Oracle have > no problem with it, > executes select on index on other_code from gd and join g > in next step executes select on index on code from g and join gd > and this two results are connected in last step (like union) > very fast on minimal cost > > and in my opinion read whole huge tables only for 10 rows in result where > conditions are very good ... is strange I suppose the equivalent query that you'd want would be: SELECT ... FROM gd JOIN gd_data USING (id_gd) WHERE id_gd IN ( SELECT id_gd FROM gd WHERE ... UNION ALL -distinct not required in this situation SELECT id_gd FROM gd_data WHERE ... ) --ignoring NULL implications It does make sense conceptually... David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Problem-with-slow-query-with-WHERE-conditions-with-OR-clause-on-primary-keys-tp5782803p5783942.html Sent from the PostgreSQL - performance mailing list archive at Nabble.com. -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance