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 1fLU39-0002vh-79 for pgsql-performance@arkaria.postgresql.org; Wed, 23 May 2018 13:43:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fLU37-0001yi-D2 for pgsql-performance@arkaria.postgresql.org; Wed, 23 May 2018 13:43:29 +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 1fLU36-0001yY-UP for pgsql-performance@lists.postgresql.org; Wed, 23 May 2018 13:43:29 +0000 Received: from mail-io0-x243.google.com ([2607:f8b0:4001:c06::243]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fLU32-0001cG-Tq for pgsql-performance@postgresql.org; Wed, 23 May 2018 13:43:27 +0000 Received: by mail-io0-x243.google.com with SMTP id d11-v6so22842581iof.11 for ; Wed, 23 May 2018 06:43:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Q0efq4sPe0YHbtDZ9zDRLmk3phreDWnqmPfMV3DiE2Q=; b=yzTeClxik5yh/d+ZTXMw+66/xALm2EY4gYJIUETvUu1aAYjOLzamB4anDZxs+Sfyn9 Opu4W3wkqimQaeI64a1NfNJfQW5oDreEHr0LrIP/dQcyBjrYMxNOJMvnp6qifIuHoUG9 sgjmOozHhZ2At5a+7IfgkmUSgxijOwGpI5wh0/RLcZRFA/0dOkatJBcdy1qWFa/gxI68 geaSTAhWrRQzzeVx8c/4pmaUFueL3/tI4TV1mOE5rZeyQCjsfOGV+tbs2+gT2qagSKam 9Bc/7svq760nZ81BHbbLEZGodN245NTgdAxcpNv31ctjtZHPRgYCihfeOIJuXtZVTW6K Q70g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Q0efq4sPe0YHbtDZ9zDRLmk3phreDWnqmPfMV3DiE2Q=; b=T9pFaJwdbFP8ZLKhKJKCGVVcOHoq5M/wAjGuZU3kkZ2gjcJJwHjMthl3g+FAPLqBZP KF6gAgfK3JjOkZ9Ng8JH8Vw2yGSF55w5vR/xAIhIkFaz+iLu1CZArpy3n4vc0LCYH9HS 3WvG4ES9aEJ7Zbm3GJHGYZUpzuUyyUClFpDlSNwTrgVEkiq6MhW7hQC+jML1raN9Az4i Nt19Xdcv8m/Ca/2Hrg0Q0EprNI1gEJNs+4PjbUUH+5aknY4q670D3m/yvAmvirG+k1oO 8D8Bsm9CZfXCEhyLOUt6Pi1d+A+KuZQSYXhhSGZ45nnNrLa4RFzCs+RYo2CFJ85ghsfE Bsdg== X-Gm-Message-State: ALKqPwc3rjRkr7t/8Pbo5g9Szo7WEwiJnnQEAKYgGRb9MQDD7Yi057f+ yhUGf+NeGVDKHajQEnMpmN4BVQ== X-Google-Smtp-Source: AB8JxZrwBGLnE6RJmKSNg9uGKPzFMoW8H4arLV3EZog4xJXQ4IL8X5Gv+dZhTNRsyKVh5QChmPA8yw== X-Received: by 2002:a6b:e215:: with SMTP id z21-v6mr2486764ioc.280.1527083003740; Wed, 23 May 2018 06:43:23 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id y204-v6sm11599786ioy.88.2018.05.23.06.43.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 May 2018 06:43:23 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 55DF1801727; Wed, 23 May 2018 08:43:22 -0500 (CDT) Date: Wed, 23 May 2018 08:43:22 -0500 From: Justin Pryzby To: pavan95 Cc: pgsql-performance@postgresql.org Subject: Re: Help me in reducing the CPU cost for the high cost query below, as it is hitting production seriously!! Message-ID: <20180523134322.GK30060@telsasoft.com> References: <1526883356469-0.post@n3.nabble.com> <1526917693149-0.post@n3.nabble.com> <1526985179479-0.post@n3.nabble.com> <20180522103906.GF30060@telsasoft.com> <1526986304012-0.post@n3.nabble.com> <20180522182307.GA9330@telsasoft.com> <1527058866609-0.post@n3.nabble.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1527058866609-0.post@n3.nabble.com> User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, May 23, 2018 at 12:01:06AM -0700, pavan95 wrote: > As said, created index on the res_users.res_employee_id and the below link > is the explain plan result. > > Link: https://explain.depesz.com/s/hoct > > And the cost of Previous query is 92,129 and the cost of current modified > query after creating the above said index is 91,462. But good thing is we Forget the cost - that's postgres *model* of the combined IO+CPU. If the model is off, that's may cause bad plans and could be looked into further. In any case, that index cut your runtime from 75sec to 60sec (in spite of the modelled cost). It looks like you resolved the bad estimate on the users table? > 2. tms_workflow_history: > Indexes: > "tms_workflow_history_pkey" PRIMARY KEY, btree (id) > "curract_state_isfinal_app_idx" btree (current_activity, state, is_final_approver) > "timesheet_id_group_id_active_idx" btree (timesheet_id, group_id, active) > "tms_wkf_his_active_is_final_approveridx" btree (active, is_final_approver) > "tms_wkf_his_group_id_idx" btree (group_id) > "tms_wkf_his_timesheet_id_idx" btree (timesheet_id) > "tms_wkf_hist_current_activity_idx" btree (current_activity) > "tms_wkf_hist_state_idx" btree (state) > "wsr_id_idx" btree (wsr_id) How big is the table ? And curract_state_isfinal_app_idx ? Have these been reindexed (or pg_repacked) recently? It seems to me that the remaining query optimization is to improve this: > Bitmap Heap Scan on tms_workflow_history history (cost=193.19..1,090.50 rows=6,041 width=12) (actual time=3.692..15.714 rows=11,351 loops=1) I think you could consider clustering (or repacking) the table on curract_state_isfinal_app_idx (but you'll have to judge if that's okay and won't negatively affect other queries). But, what's your target runtime ? Improvements here could cut at most 15sec off the total 60sec. If you're hoping to save more than that, you'll need to (also) look further than the query: - postgres parameters: what are shared_buffers, work_mem, effective_cache_size ? + https://wiki.postgresql.org/wiki/Server_Configuration - are there other DBs/applications running on the server/VM ? - kernel tuning (readahead, VM parameters, write cache, scheduler, THP, etc) - server hardware (what OS? storage? RAM? filesystem?) - how does the storage perform outside of postgres? + something like this: /usr/sbin/bonnie++ -f -n0 -x4 -d /var/lib/pgsql Justin