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 1fq2Ry-0004Yv-QZ for pgsql-general@arkaria.postgresql.org; Wed, 15 Aug 2018 20:31:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fq2Rx-0002Eh-AK for pgsql-general@arkaria.postgresql.org; Wed, 15 Aug 2018 20:31:25 +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 1fq2Rv-0002EC-Co for pgsql-general@lists.postgresql.org; Wed, 15 Aug 2018 20:31:25 +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 1fq2Rs-0002DB-JB for pgsql-general@postgresql.org; Wed, 15 Aug 2018 20:31:22 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id EF761216C5; Wed, 15 Aug 2018 16:31:19 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 15 Aug 2018 16:31:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc: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=geDjBe6u0oMGVuykRTJ/9TxqhlhNu vFqt0LM5KlGI08=; b=W2XozfeO4c27OxrhzYWkmyrhyDE6w1SxELEwTFlnFYOb7 Tj0Jx4vnz1qlh3gYeK6dpnP7LgLeWQv3mWzYjBi+dzU8iHblH3J+KxjXRlrWgFA7 vbw55gMFKwtQvn7GX5FdzDR0g9SCDizOYNXSewr1I1dnw310sP/vfpKodZCzY8bQ 3e+GcYZSsewGa9IFQ+hkM56pXkXnQFB5/1lA6E+4w/eBLANyxDz4mfVu7fyF4Yo2 DSp5/GfaB5PPsV+No8x1lJju4CFhX5lmct0WKlepSd6FQnNP6lDdA3XOFxK4RHLW TRNikwiqiRyO/1haozdadOpTUfpE0/I71kKUeA0Yw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=geDjBe 6u0oMGVuykRTJ/9TxqhlhNuvFqt0LM5KlGI08=; b=I4aRNRGLV99wK3FPR+LCgd ji08p4wGGbIU70puKLR4FeH0JXM3nQAUpShM/rhFoOVnDzoMMexAbuZZ65ft8XfN SUKRDT6vS4vID5fYmXD89af9pgW54AoEFQgQ9q/BwMlcdEpuwWPXag/Jlm0jPCgW eWQHqAj0BmxnkKdSnLJ6zxN06xxoaw8h4JCm2wOvGksJ0dpH1OXlMNG9YapV7yhn 141IdeokiySHwMSXEyjUUSVn3Uv3lYfczZj9TFXVTOidtybxPyp68TL9By4Xmur7 a6qegqlRsDYUBpwuBdOY0DieFg+otUpVQVpWqeDKWLuNolq1WNGe0048ct1p8oPQ == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 1A10BE4696; Wed, 15 Aug 2018 16:31:19 -0400 (EDT) Subject: Re: Odd Row Estimates in Query Plan (rows=75) To: Don Seiler Cc: Postgres General References: From: Adrian Klaver Message-ID: <483d3681-5764-0e8d-a40a-d4cbd488a161@aklaver.com> Date: Wed, 15 Aug 2018 13:31:17 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 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 08/15/2018 01:03 PM, Don Seiler wrote: > Here's the query, obfuscated manually by me: > > SELECT >         'Foo' as system_function, > stores.name as store, >         lt.owner, >         lt.minute_of_day, >         lt.records >         FROM >         foo.stores >         LEFT OUTER JOIN >             (SELECT >                 lts.store_pkey, >                 lts.owner, >                 date_trunc('minute', lts.date_gifted) as minute_of_day, >                 count(*) as records >             FROM foo.gifts lts >             WHERE >                 lts.date_added  > '2017-07-14 11:13:05' >             AND lts.date_added  < '2017-08-13 14:14:21' >             AND lts.date_gifted >= '2017-08-13 11:13:05' >             AND lts.date_gifted <  '2017-08-13 14:14:21' >             GROUP BY 1,2,3 >             ORDER BY 1 >             ) lt ON lt.store_pkey = stores.pkey >         WHERE lt.records IS NOT NULL; > > The foo.gifts table is pretty much the core table of our database. It's > big and very active. There is an index on date_added but not yet on > date_gifted. > > I'm working to re-write the query while the dev sees if we even need > this query anymore. > I agree the issue seems to be in the index/filter of the dates. That leads me to another question: Why in: WHERE lts.date_added > '2017-07-14 11:13:05' AND lts.date_added < '2017-08-13 14:14:21' AND lts.date_gifted >= '2017-08-13 11:13:05' AND lts.date_gifted < '2017-08-13 14:14:21' is lts.date_added > '2017-07-14 11:13:05' and lts.date_gifted >= '2017-08-13 11:13:05' ? In other words one '>' and the other '>=' ? -- Adrian Klaver adrian.klaver@aklaver.com