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 1fOWFx-0008AD-Aw for pgsql-general@arkaria.postgresql.org; Thu, 31 May 2018 22:41:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fOWFv-0000gw-7G for pgsql-general@arkaria.postgresql.org; Thu, 31 May 2018 22:41:15 +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 1fOWFt-0000gm-Mx for pgsql-general@lists.postgresql.org; Thu, 31 May 2018 22:41:15 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fOWFq-0001W1-30 for pgsql-general@lists.postgresql.org; Thu, 31 May 2018 22:41:12 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 1449D21C86; Thu, 31 May 2018 18:32:53 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 31 May 2018 18:32:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=Lo4VnQ6YOh/AtsZdagq55RaHXUAt9 a86FKxKoUk+pX0=; b=gv7IwbfXpCN3E0/T9n17VmrDOH1Z1CyiQGRFVw3bb0vQy 7W6ELtln2ZPPZEN4G2UBWO3Xrij2PYC2ujtkBvM+eQtWnsdaEuOqSaFOSBHoWgsU KMO27pxZBDg5Eo/3dXsphiNS3ds9Ib0VJFwIUKD8N11cdUYD9lDVFiFXekd2geEQ Xd9C2xBilrXUxfVITY/W2+MwglK8yjanHR1j/gdW9jnT07Sl1TG89f1JrzpFCZYS TwnOppulucBOgL0SZ01Rl8IOqnOHJnMjBC8YeDC0fEpvxBksx5K3lQALO2I15hg8 FrYtL79oZXslo9FXUZSu6VVbFrtAM25ss6OPxO1lg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=Lo4VnQ 6YOh/AtsZdagq55RaHXUAt9a86FKxKoUk+pX0=; b=CqGJlfVLWnawPzg/qyRx/4 wInMWbw4FPHATLHxM6Tl3NBhXm0iZcpqCYjM/NqxHj6IpFlT8xKze4q9fuLOczLE V59ljwk5rq5WGT/mlA8TgjSFJTdpfeep9JcYZhQZDhNCDFyrq8flvbLzdnBcnNik f2AnxJECBaoQPREaMZNrYHypZ2yS+VBUpBBi11SmZa0ezMk5P/yPmtXTt3eGQ84a BtFcmaCeur3aoX4cEnYWKC6F8v7TTOd+o6fQkEzU0nmsP7SNeiB4HXhXRIfJZuNN Ma/ftiycdPdlIVndr+yUQBBhcl9/FjEc4i4b4BMAkvbK+5yAa1QSLfTa8akPn8yg == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.159.209]) by mail.messagingengine.com (Postfix) with ESMTPA id D69D610262; Thu, 31 May 2018 18:32:51 -0400 (EDT) Subject: Re: Sort is generating rows To: Nicolas Seinlet , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com> Date: Thu, 31 May 2018 15:32:51 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/31/2018 02:09 AM, Nicolas Seinlet wrote: > Hi, > > I have a query with a strange query plan. > > This query is roughly searching for sales, and convert them with a > currency rate. As currency rate changes from time to time, table > contains the currency, the company, the rate, the start date of > availability of this rate and the end date of availability. My guess is to get a complete answer you are going to need to provide: 1) The complete query. 2) The complete EXPLAIN ANALYZE. More comments inline below. > > The join is done using : >     left join currency_rate cr on (cr.currency_id = pp.currency_id and >           cr.company_id = s.company_id and >           cr.date_start <= coalesce(s.date_order, now()) and >          (cr.date_end is null or cr.date_end > coalesce(s.date_order, > now()))) > > The tricky part is the date range on the currency rate, which is not an > equality. > > My question is then , is there a better way to join a table to another > using a date range, knowing that there's no overlap between date ranges? Use date ranges?: https://www.postgresql.org/docs/10/static/functions-range.html > Should we generate a virtual table with rates for all dates, and joining > using an equality? > > For now, the more currency rates, the slowest the query. There's not > that much currency rates (1k in this case), as you can only have one > rate per day per currency. > > Have a nice day, > > Nicolas. -- Adrian Klaver adrian.klaver@aklaver.com