Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dDuqI-0007Lr-PZ for pgsql-performance@arkaria.postgresql.org; Thu, 25 May 2017 15:38:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dDuqI-0002i4-CT for pgsql-performance@arkaria.postgresql.org; Thu, 25 May 2017 15:38:26 +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 1dDuqH-0002hE-EE for pgsql-performance@postgresql.org; Thu, 25 May 2017 15:38:25 +0000 Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dDuqA-0001w7-IK for pgsql-performance@postgresql.org; Thu, 25 May 2017 15:38:24 +0000 Received: by mail-wm0-x22e.google.com with SMTP id e127so110874214wmg.1 for ; Thu, 25 May 2017 08:38:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=hqx1hQhptGPV5zXs5ELZnULVRltYncg2PyfvkYZRbFY=; b=wMAlGjSwpaOtqCD/NtBxM2d+vFbkEC/dkvhoF/k25hDlomkn1YBcWQ9r6n+IIWk8jL 7id4zgWN/n8oMOoCgHh2kZKAsdDfSUFclUcVTALzMP8IMM7X5jowHLj6CI1D41Axuogf oQp640BtMYtVT152Wbyet8D2oqQCayp2cYQ3nQW/EHDDy5PJdvwvBr/NVAxXFfLw9X4P XVXHMHa4VRIWau11W6rT8ttmWgGczngIffLoCvQ4QitI2ARvcimc7o1rkujNl3IkekN5 MS/tS+qHBZSQl+GXEPXNIx6bgkB9DHMsIUWAyLRMqQbnJpTi19/0pUjqsX+WmVdDRphu e6Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=hqx1hQhptGPV5zXs5ELZnULVRltYncg2PyfvkYZRbFY=; b=aDlmqiiIntZw22gmEGANE7Q7nY0aADlEUThy1Q/9BE/Yj/dyxLNYdq+49PISpNxcNd 9ZGEzWbRaTUoYugu0iYg++Gd4WkOXf05zP+EfEFANmHG3/4KD+tz99i6v5LUAneqQP6e nKtGVIVpt5xiBVR/6q+4ZBVAgKO4CqsS1YA335xQrMFkIcrWxtp4DnkJz/mTldfWwXnB wTjks+gWi/Syn9QxI8BNt1nlP+ZHTNBfW/du3EaWW+n2S/nCB2AuhmbPoQGd/z0VPRaj pm+vyvgTArgj1bhKYbC/oxLzvxQ2LMC/vCVdDiaUOXf6vlUtIgLSfOJH6fNBO81yAZz0 oyEA== X-Gm-Message-State: AODbwcDRAF3V46lXiWly0jBLyczLa3T+mx8EO+/LTf+P6sqRnMd7CPdB EjxpYe/Fj5ueJIaDnyPJvEcfUvl0JzDsRvkiBUP72VibwhlYROUFM+6fHwDAAQEEFrNP4nlS1NW FqdGQdjXHHZymFeU/CAkoAihzXRAIXVwVQR3Uex+kA9HSMAJ+o8dF+ACTIPYj6g5xfVrrCw0kPd dErA== X-Received: by 10.223.169.230 with SMTP id b93mr25233699wrd.196.1495726696636; Thu, 25 May 2017 08:38:16 -0700 (PDT) Received: from [10.137.2.17] (ip-89-177-46-65.net.upcbroadband.cz. [89.177.46.65]) by smtp.gmail.com with ESMTPSA id a79sm420610wrc.12.2017.05.25.08.38.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 May 2017 08:38:15 -0700 (PDT) Subject: Re: FW: Re: Query is running very slow...... To: Dinesh Chandra 12108 Cc: "pgsql-performance@postgresql.org" References: <3152be5a435f45649cef6bf9321beb9a@cyient.com> <4caed7cc-07d8-e364-78ca-7a96b9780a9d@2ndquadrant.com> <39b30cfde27a48898340a69940581727@cyient.com> From: Tomas Vondra Message-ID: Date: Thu, 25 May 2017 17:38:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:54.0) Gecko/20100101 Thunderbird/54.0a2 MIME-Version: 1.0 In-Reply-To: <39b30cfde27a48898340a69940581727@cyient.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) 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 On 5/25/17 2:26 PM, Dinesh Chandra 12108 wrote: > Hi Tomas, > > Please find the below input for slow query. > > (a) something about the hardware it's running on > RAM-->64 GB, CPU->40core > > (b) amounts of data in the tables / databases > Database size :32GB > ----------------- > Tables size > ----------------- > Workflow.project : 8194 byte > workflow.tool_performance :175 MB > workflow.evidence_to_do :580 MB > > (c) EXPLAIN or even better EXPLAIN ANALYZE of the query > > "GroupAggregate (cost=16583736169.63..18157894828.18 rows=5920110 width=69)" > " -> Sort (cost=16583736169.63..16714893857.43 rows=52463075120 width=69)" > " Sort Key: tool_performance.project_id, project.project_name, tool_performance.step_id, (date_trunc('day'::text, tool_performance.insert_time)), tool_performance.user_id" > " -> Nested Loop (cost=2.42..787115179.07 rows=52463075120 width=69)" > " -> Seq Scan on evidence_to_do (cost=0.00..119443.95 rows=558296 width=0)" > " Filter: (status_id = ANY ('{15100,15150,15200,15300,15400,15500}'::bigint[]))" > " -> Materialize (cost=2.42..49843.24 rows=93970 width=69)" > " -> Hash Join (cost=2.42..49373.39 rows=93970 width=69)" > " Hash Cond: (tool_performance.project_id = project.project_id)" > " -> Seq Scan on tool_performance (cost=0.00..48078.88 rows=93970 width=39)" > " Filter: ((insert_time > '2017-05-01 00:00:00+05:30'::timestamp with time zone) AND (insert_time < '2017-05-02 00:00:00+05:30'::timestamp with time zone))" > " -> Hash (cost=1.63..1.63 rows=63 width=38)" > " -> Seq Scan on project (cost=0.00..1.63 rows=63 width=38)" > Are you sure this is the same query? The query you posted includes there two join conditions: evidence_to_do.project_id = tool_performance.project_id evidence_to_do.project_id = project.project_id But the plan only seems to enforce the equality between 'project' and 'tool_performance'. So when joining the evidence_to_do, it performs a cartesian product, producing ~52B rows (estimated). That can't be fast. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance