Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cjp02-0006VS-9v for pgsql-performance@arkaria.postgresql.org; Fri, 03 Mar 2017 15:20:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cjp01-0003Vb-HZ for pgsql-performance@arkaria.postgresql.org; Fri, 03 Mar 2017 15:20:05 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cjp00-0003Uo-Gy for pgsql-performance@postgresql.org; Fri, 03 Mar 2017 15:20:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cjozt-0007eh-NS for pgsql-performance@postgresql.org; Fri, 03 Mar 2017 15:20:03 +0000 Received: from pro.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v23FJnhp006087; Fri, 3 Mar 2017 10:19:50 -0500 From: Tom Lane To: Dinesh Chandra 12108 cc: Nur Agus , "pgsql-performance@postgresql.org" Subject: Re: Performance issue in PostgreSQL server... In-reply-to: References: <0796d1d53d3e4aa692cfdc329f86b399@cyient.com> Comments: In-reply-to Dinesh Chandra 12108 message dated "Fri, 03 Mar 2017 12:44:07 +0000" Date: Fri, 03 Mar 2017 10:19:48 -0500 Message-ID: <18838.1488554388@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) 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 Dinesh Chandra 12108 writes: > The below is the output for psql=> EXPLAIN ANALYZE SELECT DISTINCT feature_id FROM evidence.point p INNER JOIN evidence.observation_evidence oe ON p.feature_id = oe.evd_feature_id WHERE p.domain_class_id IN (11) AND (p.modification_time > '2015-05-10 00:06:56.056 IST' OR oe.modification_time > '2015-05-10 00:06:56.056 IST') ORDER BY feature_id I think that's a fundamentally slow query and you're not going to be able to make it better without rethinking your requirements and/or data representation. As written, that requires the server to form the entire join of p to oe on feature_id, with the only filter before the join being the evidently-none-too-selective domain_class_id condition. Only after joining can it apply the OR condition. So this is inherently processing a lot of rows. If the OR arms were individually pretty selective you could rewrite this into a UNION of two joins, a la the discussion at https://www.postgresql.org/message-id/flat/7f70bd5a-5d16-e05c-f0b4-2fdfc8873489@BlueTreble.com but given the dates involved I'm betting that won't help very much. Or maybe you could try select feature_id from p where domain_class_id IN (11) AND p.modification_time > '2015-05-10 00:06:56.056 IST' intersect select feature_id from oe where oe.modification_time > '2015-05-10 00:06:56.056 IST' order by feature_id although I'm not entirely certain that that has exactly the same semantics (-ENOCAFFEINE), and it might still be none too quick. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance