Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c6fwV-0000Kx-2M for pgsql-performance@arkaria.postgresql.org; Tue, 15 Nov 2016 15:46:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c6fwU-0003Oi-Jg for pgsql-performance@arkaria.postgresql.org; Tue, 15 Nov 2016 15:46:38 +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 1c6fun-0005cm-CS for pgsql-performance@postgresql.org; Tue, 15 Nov 2016 15:44:53 +0000 Received: from lb3-smtp-cloud2.xs4all.net ([194.109.24.29]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1c6fuk-0003zZ-Ef for pgsql-performance@postgresql.org; Tue, 15 Nov 2016 15:44:52 +0000 Received: from webmail.xs4all.nl ([194.109.20.212]) by smtp-cloud2.xs4all.net with ESMTP id 8Fkn1u00C4aYjWA01Fkn0Y; Tue, 15 Nov 2016 16:44:48 +0100 Received: from mail.genj.nl ([77.242.116.162]) by webmail.xs4all.nl with HTTP (HTTP/1.1 POST); Tue, 15 Nov 2016 16:44:47 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Tue, 15 Nov 2016 16:44:47 +0100 From: vinny To: henrik@ekenberg.pw Cc: pgsql-performance , pgsql-performance-owner@postgresql.org Subject: Re: Sql Query :: Any advice ? In-Reply-To: <20161115153053.Horde.FdPqz_uJLMknz4L8pIlTdDQ@box1242.bluehost.com> References: <20161115142713.Horde.2yANpKWbi2sgTkv2Qzw7bVs@box1242.bluehost.com> <7cecad43ac720668a96cb2fc112cac9b@xs4all.nl> <20161115153053.Horde.FdPqz_uJLMknz4L8pIlTdDQ@box1242.bluehost.com> Message-ID: <1fd2cd0b983d53719d7a0f50855303a6@xs4all.nl> X-Sender: vinny@xs4all.nl (pxkr5iK/XqAIvM/YbaBd2w==) User-Agent: XS4ALL Webmail 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 Are the forex and options in the hist_account_balance table? The sequential scan is on that table so if they are, so I'm guessing they should probably by in the index. On 2016-11-15 15:30, Henrik Ekenberg wrote: > Here are the indexes I have for those queries > > Indexes: > > hist_account_balance :: "hist_account_balance_ix1" btree (trade_no) > > trades :: "trades_pkey" PRIMARY KEY, btree (trade_no) > "trades_trade_date_index" btree (trade_date) > > //H > > Quoting vinny : > >> On 2016-11-15 14:27, Henrik Ekenberg wrote: >> >>> Hi, >>> >>> I have some data to join and I want to get som advice from you. >>> >>> Any tips ? Any comments are apreciated >>> >>> //H >>> >>> select trade_no >>> from >>> forecast_trades.hist_account_balance >>> left join trades using (trade_no) >>> where trade_date > current_date - 120 >>> and trade_date < current_date - 30 >>> and forex = 'f' >>> and options = 'f' >>> group by trade_no >>> having max(account_size) > 0 >>> ; >>> >>> ( Query Plan : https://explain.depesz.com/s/4lOD ) >>> >>> QUERY PLAN >>> >>> >> > ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- >>> HashAggregate (cost=34760605.76..34773866.26 rows=1060840 >>> width=15) >>> (actual time=1142816.632..1150194.076 rows=2550634 loops=1) >>> Group Key: hist_account_balance.trade_no >>> Filter: (max(hist_account_balance.account_size) > 0::numeric) >>> Rows Removed by Filter: 18240023 >>> -> Hash Join (cost=3407585.35..34530512.29 rows=46018694 >>> width=15) (actual time=60321.201..1108647.151 rows=44188963 >>> loops=1) >>> Hash Cond: (hist_account_balance.trade_no = >>> trades.trade_no) >>> -> Seq Scan on hist_account_balance >>> (cost=0.00..14986455.20 >>> rows=570046720 width=15) (actual time=0.016..524427.140 >>> rows=549165594 >>> loops=1) >>> -> Hash (cost=3159184.13..3159184.13 rows=19872098 >>> width=12) (actual time=60307.001..60307.001 rows=20790658 loops=1) >>> Buckets: 2097152 Batches: 1 Memory Usage: 913651kB >>> -> Index Scan using trades_trade_date_index on >>> trades >>> (cost=0.58..3159184.13 rows=19872098 width=12) (actual >>> time=0.078..52213.976 rows=20790658 loops=1) >>> Index Cond: ((trade_date > >>> (('now'::cstring)::date - 120)) AND (trade_date < >>> (('now'::cstring)::date - 30))) >>> Filter: ((NOT forex) AND (NOT options)) >>> Rows Removed by Filter: 2387523 >>> Planning time: 2.157 ms >>> Execution time: 1151234.290 ms >>> (15 rows) >> What kind of indexes have you created for those tables? >> >> -- >> Sent via pgsql-performance mailing list >> (pgsql-performance@postgresql.org) >> To make changes to your >> subscription:http://www.postgresql.org/mailpref/pgsql-performance -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance