Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fKn2t-0004g4-Cp for pgsql-performance@arkaria.postgresql.org; Mon, 21 May 2018 15:48:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fKn2o-0000bH-Qn for pgsql-performance@arkaria.postgresql.org; Mon, 21 May 2018 15:48:18 +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.89) (envelope-from ) id 1fKn2o-0000b6-GP for pgsql-performance@lists.postgresql.org; Mon, 21 May 2018 15:48:18 +0000 Received: from n3.nabble.com ([162.255.23.22]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fKn2l-0006ZV-0X for pgsql-performance@postgresql.org; Mon, 21 May 2018 15:48:16 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id 2FEAED0EB2DF for ; Mon, 21 May 2018 08:48:13 -0700 (MST) Date: Mon, 21 May 2018 08:48:13 -0700 (MST) From: mlunnon To: pgsql-performance@postgresql.org Message-ID: <1526917693149-0.post@n3.nabble.com> In-Reply-To: <1526883356469-0.post@n3.nabble.com> References: <1526883356469-0.post@n3.nabble.com> Subject: Re: Help me in reducing the CPU cost for the high cost query below, as it is hitting production seriously!! MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk pavan95 wrote > *Query*: > > explain select ... from tms_timesheet_details, tms_wsr_header header > where wsr_header_id=header.id and work_order_no != 'CORPORATE'; > > QUERY PLAN > --------------------------------------------------------------------------------------------- > Aggregate (cost=9868.91..9868.92 rows=1 width=8) > -> Hash Join (cost=608.27..5647.67 rows=70354 width=8) > Hash Cond: (tms_timesheet_details.wsr_header_id = header.id) > -> Seq Scan on tms_timesheet_details (cost=0.00..3431.14 > rows=72378 width=12) > Filter: ((work_order_no)::text <> 'CORPORATE'::text) > -> Hash (cost=399.23..399.23 rows=16723 width=4) > -> Seq Scan on tms_wsr_header header (cost=0.00..399.23 > rows=16723 width=4) > (7 rows) > > > -- > Sent from: > http://www.postgresql-archive.org/PostgreSQL-performance-f2050081.html Why is the table tms_wsr_header in the from clause as it is not used in the select columns? A simple "wsr_header_id is not null" would do the same as this is a foreign key into the tms_wsr_header table. An index with on tms_timesheet_details.id "where wsr_header_id is not null" might then speed the query up if there were significant numbers of rows with a null wsr_header_id. Cheers Matthew -- Sent from: http://www.postgresql-archive.org/PostgreSQL-performance-f2050081.html