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 1fS2Ch-0000pd-BO for pgsql-general@arkaria.postgresql.org; Sun, 10 Jun 2018 15:24:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fS2Cf-0007MB-Vt for pgsql-general@arkaria.postgresql.org; Sun, 10 Jun 2018 15:24:25 +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_SHA384:256) (Exim 4.89) (envelope-from ) id 1fS2Ce-0007M0-1b for pgsql-general@lists.postgresql.org; Sun, 10 Jun 2018 15:24:25 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fS2CY-00074m-67 for pgsql-general@lists.postgresql.org; Sun, 10 Jun 2018 15:24:23 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id C214920FCB; Sun, 10 Jun 2018 11:24:15 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sun, 10 Jun 2018 11:24:15 -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=q5n82sNEEy4ByZkOf/wslbSne/U0D /c7Ke8AQGyKmh0=; b=oG0irJGmB/swg5DX/mlLvF00gwjzuoH7QhM7YuX+iQfwj GhFHGmoMwOAAfR00iHhKgUpqImAeD7iPNBJLIUJGhoYjQGm0BqgC78lymGMprpYD 9a0pVP+jOd8IN+yzStJATpxr+JUSW7O4BjRYGGidfbFf8TlsW2Q99jMuKRtg50kp UrWE9g3LZ9E00GTXnsrywgvNqUjDi7l8K/MzupZ+dD0XlR4Fir27fkFUwSwIz/t3 qWwvPMaw99pgUykCOWJ0crD9AXLGh3sUItcrPL+n8d5BirMqgRZ5OoWBGnrcoBhQ FKtk2Re+6k4aCLxiqMXXnnl6Th9p72XRC+wBclAxg== 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=fm3; bh=q5n82s NEEy4ByZkOf/wslbSne/U0D/c7Ke8AQGyKmh0=; b=dKg0t6KdJsLntex+hffAOm 3K1uA6sBDPoNx84P1zr/wDcqQzpDNseIXf5OvMhiwpwX18TjknECAWlzsLLWFQZJ ceCaJs6yLrLhatLYqrgMKcdlEfH03YUInZSOo40Df+nKkehbd/Uu8JG8YPkNX6Mv kAnf7pogYYO5BNPa9ekIJ5gzvy9+0y2BgH7jxaYUcloMTMyVY9Yjk9SPIg7k2KTY TV27/LaqTpd+miT7nU/Z2dFUvJTL5NLETWXRkMIm8tBIT09Ld95+KdND88kCWLFj LZUE73FXMAV1JdH1WwQHtI+Pbo6UINL+RG1SvKUBa3L9v9bNbx4nMxoeix/KU1kA == 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 E693410255; Sun, 10 Jun 2018 11:24:14 -0400 (EDT) Subject: Re: Sort is generating rows To: Nicolas Seinlet , pgsql-general@lists.postgresql.org References: <64f3353c-d9c3-69fc-2621-ba85096275fa@aklaver.com> <3851fe8c-8ff5-adfc-8fc0-f33b9a2be324@aklaver.com> From: Adrian Klaver Message-ID: <516671d6-13fd-2e24-2ec9-c7168d85229f@aklaver.com> Date: Sun, 10 Jun 2018 08:24:13 -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 06/05/2018 07:58 AM, Nicolas Seinlet wrote: > Hi, > > I've tried some tests, by generating various datas in > the res_currency_rate table. > > If I generate res_currency_rate rows for unsused currencies, this > doesn't influence the execution time. > if I generate more res_currency_rate for used currencies, this slower > the query. > If I generate 100 rates, on a one per day basis, I get an execution time > of 4.5 seconds > If I generate 100 rates, on a one per 3 days basis, execution time drops > to 4 seconds > If I generate 100 rates, on a one per 6 days basis, execution time drops > to 3.8 seconds. I took another look at the query and got to wondering about the snippet below: LEFT JOIN (SELECT r.currency_id, COALESCE(r.company_id, c.id) AS company_id, r.rate, r.name AS date_start, r.date_end FROM res_currency_rate r JOIN res_company c ON r.company_id IS NULL OR r.company_id = c.id ) cr ON cr.currency_id = pp.currency_id AND (cr.company_id = s.company_id OR cr.company_id IS NULL) AND daterange(cr.date_start, COALESCE(cr.date_end, now()::date)) @> COALESCE(s.date_order::timestamp with time zone, now())::date In particular: FROM res_currency_rate r JOIN res_company c ON r.company_id IS NULL OR r.company_id = c.id Are there NULL company_id values in res_currency_rate? If so I am trying to figure out how the JOIN to res_company would work in that situation. What happens if eliminate the r.company_id IS NULL? > > I've executed following tests many times, to avoid cache or buffers > related issues, each time after vacuuming table, ... > > The execution time with a join of type daterange : > res_currency_rate.currency_id = pp.currency_id AND > (res_currency_rate.company_id = s.company_id OR > res_currency_rate.company_id IS NULL) AND > daterange(res_currency_rate.name , > res_currency_rate.date_end) @> COALESCE(s.date_order::timestamp with > time zone, now())::date > > is slower than the date comparison equivalent: > res_currency_rate.currency_id = pp.currency_id AND > (res_currency_rate.company_id = s.company_id OR > res_currency_rate.company_id IS NULL) AND res_currency_rate.name > <= COALESCE(s.date_order::timestamp with > time zone, now()) AND (res_currency_rate.date_end IS NULL OR > res_currency_rate.date_end > COALESCE(s.date_order::timestamp with time > zone, now())) > > -- Adrian Klaver adrian.klaver@aklaver.com