Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gsWF8-0001mw-8H for pgsql-performance@arkaria.postgresql.org; Sat, 09 Feb 2019 17:16:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gsWF6-0001b1-TK for pgsql-performance@arkaria.postgresql.org; Sat, 09 Feb 2019 17:16:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gsWF6-0001aZ-Ic for pgsql-performance@lists.postgresql.org; Sat, 09 Feb 2019 17:16:40 +0000 Received: from mail-it1-x141.google.com ([2607:f8b0:4864:20::141]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gsWF3-0004H5-8I for pgsql-performance@lists.postgresql.org; Sat, 09 Feb 2019 17:16:40 +0000 Received: by mail-it1-x141.google.com with SMTP id b5so16810320iti.2 for ; Sat, 09 Feb 2019 09:16:36 -0800 (PST) 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=y5ZRM9P2aHfRb6aiGsFH+1wqdtKzH7owNrunADSzxWw=; b=op9pZw+a4acp/xHLe0d0TRwnMhjDcEMNiwhUo3hTALETdctIcjQ7G9D/h5qlEojefu tWqoNwAU3ha4j1Tfz8FNeqhT3zT0l7m/u+fSqxMGSNXQ5JEmNcjvKvMZJk6WDDnp1v9X SiPhtPIj7bVPjTAE3abGgQLoXJEh5U3Xn2n3HWuAoysMzZEHDGPOXzvD8fPcmRpzYdxR ZmWU+dhyK7iM+RhELZUIDEXgh8btTfscK9KvRyjc1JOaWAKk88Cyesxk+bpcWLZotKnm pjP9x0jyNw4/ZVjfkz8MHcknnM3VlAg6jThIBx4aTItfSdj4uQo6TMe4Is9fLg/TApsS K6Bw== 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=y5ZRM9P2aHfRb6aiGsFH+1wqdtKzH7owNrunADSzxWw=; b=XaeClqLoqm95tpMKRZ1eWgwzjLPntkNqdZ8WnO0ycHCS+tMYv1u0dKCpqAzclwICSn mQtQ98y0c8Te6upaCea/jCEq6Ue8DI9vJdoy+RcRrF0XNA2mvFJJBtdqLiF9auKNXqlz y2/QvVwjMi72SOSMzBasOPh2SDT8MZKsrI1NLgMk5hr0C+ZnhvZGUzloFs4Xtxl53YfT Kl4w3EQjlcjQD3T9JZw7Zg+ht7QoA8O26PDNN7mCVEq2Z+Uh1pLVGLTPzX0oW+USIhua hpNQDOBYeO19B/c3CRW/OVEvqnJnNVVcNtrNaUGbqdSCIa3DbAoLltiD0KjZOP/52x6F zDTA== X-Gm-Message-State: AHQUAuaMvXxP2GoTjZaCdzedFVz97wnufiieM/XKLT3tp2zMa1n0qM20 R1mR1X/stzRdCLF8MOPOIzAe5g== X-Google-Smtp-Source: AHgI3IbvBMjG7XstkNNvFwKeHw2Qp3ZSb8GWZ8HEteOtn072ZnFyulriYqLDsgS1H5VwsOF7wM+/kw== X-Received: by 2002:a24:7b0e:: with SMTP id q14mr1994663itc.82.1549732595174; Sat, 09 Feb 2019 09:16:35 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id b9sm739527iod.18.2019.02.09.09.16.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 09 Feb 2019 09:16:34 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 81136800BD7; Sat, 9 Feb 2019 11:16:33 -0600 (CST) Date: Sat, 9 Feb 2019 11:16:33 -0600 From: Justin Pryzby To: Evandro Abreu Cc: pgsql-performance@lists.postgresql.org Subject: Re: slow to run query 5000 times Message-ID: <20190209171633.GO31721@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, Please don't send images to the list, you can send a link to one of the image host websites if you need to describe something graphical. But here, you could just send the queries and \d for the tables. On Sat, Feb 09, 2019 at 01:45:50PM -0300, Evandro Abreu wrote: > I have a report that takes about 20 minutes to generate. It is generated > from 3 tables: according to image. > The report input parameter is a date range. So to generate it I select all > records in Table A and run them > in loop-for. For each record in Table A I make a query Table B join with > Table C where I filter the records through the date field and make the sum > of the value field. So you're running query 5000 times ? Do you really need a for loop ? Could you just join the 3 tables together and GROUP BY a.id ? Please send "explain analyze" for the queries, or a link to the output on depesz site. https://wiki.postgresql.org/wiki/Slow_Query_Questions#Information_You_Need_To_Include Also, are they all taking about the same amount of time ? Justin