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 1fKfnT-0000JL-7e for pgsql-performance@arkaria.postgresql.org; Mon, 21 May 2018 08:03:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fKfnR-0004G8-S4 for pgsql-performance@arkaria.postgresql.org; Mon, 21 May 2018 08:03:57 +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 1fKfnR-0004Fs-HK for pgsql-performance@lists.postgresql.org; Mon, 21 May 2018 08:03:57 +0000 Received: from n3.nabble.com ([162.255.23.22]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fKfnM-0004pN-GA for pgsql-performance@postgresql.org; Mon, 21 May 2018 08:03:55 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id 7B330D0D2EC7 for ; Mon, 21 May 2018 01:03:50 -0700 (MST) Date: Mon, 21 May 2018 01:03:50 -0700 (MST) From: mlunnon To: pgsql-performance@postgresql.org Message-ID: <1526889830358-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 > Hi all, > > Hope my mail finds you in good time. I had a problem with a query which is > hitting the production seriously. > The below is the sub part of the query for which I cannot reduce the CPU > cost. > > Please check and verify whether I'm doing wrong or whether that type index > type suits it or not. > > Kindly help me resolve this issue. > > *Query*: > > explain select sum(CASE > WHEN MOD(cast(effort_hours as decimal),1) = > 0.45 THEN > cast(effort_hours as int)+0.75 > ELSE > CASE > WHEN MOD(cast(effort_hours as decimal),1) > = > 0.15 THEN > cast(effort_hours as int) + 0.25 > > ELSE > CASE > WHEN MOD(cast(effort_hours as decimal),1) > = > 0.30 THEN > cast(effort_hours as int) + 0.5 > > ELSE > CASE > WHEN MOD(cast(effort_hours as decimal),1) > = > 0 THEN > cast(effort_hours as int) > end > END > END > END) from tms_timesheet_details, > tms_wsr_header > header where wsr_header_id=header.id and work_order_no != 'CORPORATE'; > > > > -- > Sent from: > http://www.postgresql-archive.org/PostgreSQL-performance-f2050081.html To start with you can try re-writing this so that it only does the mod cast once. e.g: sum ( CASE MOD(cast(effort_hours as decimal),1) WHEN 0.45 THEN cast(effort_hours as int)+0.75 WHEN 0.15 THEN cast(effort_hours as int)+0.25 WHEN 0.30 THEN cast(effort_hours as int)+0.5 WHEN 0 THEN cast(effort_hours as int) END ) -- Sent from: http://www.postgresql-archive.org/PostgreSQL-performance-f2050081.html