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 1fFr7q-0006Ne-Oz for pgsql-performance@arkaria.postgresql.org; Tue, 08 May 2018 01:09:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fFr7o-0005Qo-6f for pgsql-performance@arkaria.postgresql.org; Tue, 08 May 2018 01:09:04 +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 1fFr7n-0005Qf-Nm for pgsql-performance@lists.postgresql.org; Tue, 08 May 2018 01:09:04 +0000 Received: from mail-io0-x232.google.com ([2607:f8b0:4001:c06::232]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fFr7j-0004ll-Qw for pgsql-performance@lists.postgresql.org; Tue, 08 May 2018 01:09:02 +0000 Received: by mail-io0-x232.google.com with SMTP id r9-v6so36438549iod.6 for ; Mon, 07 May 2018 18:08:59 -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=XQ1Jj8PmZBMXdIXOcgZEQQcpqUxuG7p0Ilt3TT4yG84=; b=KPgT7TJ+0USaDhSjrWGG97CE1mczc8HwGGuDSDV5JdJ3QxnSXkzbWYyHgHyNRNbjSV iJl98OFybNg2obR3maMDivMPAe7xlgIPkA66YjOL9lppGcLTJiztacCB+CYkYQXEpCzN 8YOk92P5ULC7Mv9eepl0oIn+e/V7RF/VMYvBYV35oG8RYObwglZcZNcxr1lSxU+Te3nA TsFD6WcHxODtZNylGYCeK8DtQ9tgr+JGSUdvjsztaPvwJ954p7SiFMkOaAuIVH1wzd/h 2zXs8/RnWweA8BTqzTkMmx6WOYps/2Z7kj32cKYh5m5j3YUa+1bfBNac4874lhiQq+l0 6BlA== 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=XQ1Jj8PmZBMXdIXOcgZEQQcpqUxuG7p0Ilt3TT4yG84=; b=gT3+BsiIk9Zg3bmupuZ4boyU5fWyBoc4/7+0/iijYJNB/g8qenrL20RR5C64tjjBFj mWOXotnyxDibAExBpe0gaTnBNSxAcQxEFcDjkpMGK1yMkgtKcFG05Kk1KnoE4ZCB4uv/ 5DYgpSSEkU4HjGtbUXMwRxO4j9GPZfPZmfI9vNzcjKZGnltX7YohfZHigay9xLrhmhzT FwAIUij+JHhxbLMn7eQYStKehWTThtEEL/sHBact5SZMWCu4rkE+K1VAyopA8ZC5McTs dvx18D875vTXuv1cXGD5eyCSllwv1PkRL89JiB+4V5gSea3J4iNC4pia0DSKaIvFAJmL ug+w== X-Gm-Message-State: ALQs6tAOQosy8+81rrThP2vl8+8q8XQzp/cD7KALVi2suTRGwJmgw3zf guUuNLczi4J3xcucpowDlPrjNA== X-Google-Smtp-Source: AB8JxZq0dqB3WCdU7H6rlEMUIYlvflX+QRZQFlaYYeCcUPaOcUODzYHbhgFsZFYCCkLZGzrbVB1YZg== X-Received: by 2002:a6b:9dc1:: with SMTP id g184-v6mr40288697ioe.41.1525741738546; Mon, 07 May 2018 18:08:58 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id r1-v6sm11919403ioc.57.2018.05.07.18.08.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 May 2018 18:08:58 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 5194B8021B7; Mon, 7 May 2018 20:08:57 -0500 (CDT) Date: Mon, 7 May 2018 20:08:57 -0500 From: Justin Pryzby To: Julian Wilson Cc: pgsql-performance@lists.postgresql.org Subject: Re: Time bucketing query performance Message-ID: <20180508010857.GI5192@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 On Mon, May 07, 2018 at 07:33:17PM -0400, Julian Wilson wrote: > Hi, > > I'm trying to debug improve the performance of my time bucketing query. > What I'm trying to do is essentially bucket by an arbitrary interval and > then do some aggregations within that interval (min,max,sum, etc). I am > using a `max` in the query I posted. For context in the data, it is 1 > minute candles of cryptocurrency data (open price, high price, low price, > close price, volume, for an interval). I want to transform this to a 10 > minute interval, on demand, and that is what this query is meant to do. > > I understand the slow part of my query is in the LEFT JOIN, but I just > can't quite figure out how to do it without the LEFT JOIN. > > Here is my pastebin with all the details so I don't clutter the message. I > tried to follow everything in the 'Slow Query Questions' WIKI page. There > is also a depesz link there. > > https://ybin.me/p/9d3f52d88b4b2a46#kYLotYpNuIjjbp2P4l3la8fGSJIV0p+opH4sPq1m2/Y= Thsse may not be a substantial part of the issue, but I have some suggestions: 0) You're using CTE, which cannot have stats (unlike temporary table). Can you rewrite without, perhaps with GROUP BY date_trunc('hour', time_open) ? 1) you're querying on start_time AND end_time, and I believe the planner thinks those conditions are independent, but they're not. Try getting rid of the frame_end and move the "5 months" into the main query using BETWEEN or two ANDed conditions on the same variable. See if the rowcount estimate is more accurate: -> Index Scan using historical_ohlcv_pkey on historical_ohlcv ohlcv (cost=0.56..2488.58 ROWS=12110 width=22) (actual time=3.709..4.403 ROWS=9 loops=3625) Index Cond: ((exchange_symbol = 'BINANCE'::text) AND (symbol_id = 'ETHBTC'::text) AND (time_open >= g.start_time)) Filter: (time_close < g.end_time) Alternately, you could try: CREATE STATISTICS (dependencies) ON (time_open,time_close) FROM historical_ohlcv ; ANALYZE historical_ohlcv; 2) Is your work_mem really default? 64kb? Recommend changing it to see if the plan changes (although it looks like that's not the issue). 3) If you have SSD, you should probably CREATE TABLESPACE tmp LOCATION /srv/pgsql_tmp and ALTER SYSTEM SET temp_tablespaces='tmp' and SELECT pg_reload_conf(); 4) If those don't help, as a test, try running with SET enable_nestloop=off. I'm guessing that fixing rowcount estimate in (1) will be sufficient. 5) May not be important, but rerun with explain (ANALYZE,BUFFERS) and show the results. Justin