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 1fq1e8-0001zv-HB for pgsql-general@arkaria.postgresql.org; Wed, 15 Aug 2018 19:39:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fq1e6-0001c4-MJ for pgsql-general@arkaria.postgresql.org; Wed, 15 Aug 2018 19:39:54 +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 1fq1e4-0001bx-Hw for pgsql-general@lists.postgresql.org; Wed, 15 Aug 2018 19:39:54 +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 1fq1e0-0000yr-7g for pgsql-general@postgresql.org; Wed, 15 Aug 2018 19:39:51 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 0386E21D4E; Wed, 15 Aug 2018 15:39:47 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 15 Aug 2018 15:39:47 -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=HaAcmKwmWtzZYwnm3H4MrWvpzv4p6 Rlm47azbIFAfMo=; b=OZIapRMLuKvaYXHq8dczVplARVMPJt7o+KxBQTVH+JK6i xJUKUDwI80hNQ35VtajM+5DnMdxnm+eGpPmRMYR+TLIbbdPI2XEBQw2J/VAQU6Qe /ZGGdSXkXRqVHbAKJWEA19B8hU8RvWZgBHw9PZ3TCt08hkSo3gC6N6vbgxCuUMhf mfyaaSvilc0E0NdTHCTGAyKGTCbop3RispSaJM+5RCSAWciZj9wtnlq44RF1tSyv U44Q8vzwbQln94ICVEBObcRXieZfwXgBmj+DTC95zbtM5pFmJzBOqsLCzAokn9co qejmv/qgZ+VtaYD5U/TLU8mmG8dt1Pgb+oHrRG6eg== 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=HaAcmK wmWtzZYwnm3H4MrWvpzv4p6Rlm47azbIFAfMo=; b=vK6XYml6R0a5CVSPn7bz4a tgbMtAfwud1KL9R4k19cKHdjmgas/hVIDc802Ddqp+5T7JI/CeuQ85+fg8u3DzHe uOsoGj25D1HNBKLfg0S/o8tfr1+Whxi5BTzlspByDGD+g1y7To3arzeDhevIZu7r aY9/5C0v4PsgGe1IIy1VUe3UUIb1bFQa8Gxid/YkiJwzqgN1x1bnnSQE6PI9CW4u BUrcsQ5Oxj4YFErySC/cyleCA0dDqKVWF49fC5ZBL9tLF7AkpYlse71htVfY3zDx ypD+xEs/ESoZEFtlo4aCFA6AN8aH5fRAn3kM63T4cRI9u+i8qlbqPBiv3rN0X/IQ == 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 4833DE44F3; Wed, 15 Aug 2018 15:39:46 -0400 (EDT) Subject: Re: Odd Row Estimates in Query Plan (rows=75) To: Don Seiler , Postgres General References: From: Adrian Klaver Message-ID: Date: Wed, 15 Aug 2018 12:39:45 -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 12:31 PM, Don Seiler wrote: > PostgreSQL 9.6.6 on CentOS. > > We have a report query that has gone from maybe a few seconds to run to > a few minutes to run since mid-July. Looking at the output of EXPLAIN > ANALYZE, the row count estimates are way off, even though this table was > just analyzed a day or so ago. What's more bizarre to me is that the row > count esimate is *always* 75 for every node of the plan, where the > actual rows is in the hundreds or thousands. This table is one of the > busiest tables in our production database (many inserts and updates). It > is autovacuumed and autoanalyzed a few times per week, although I'm > looking to change it to a nightly manual schedule to avoid daytime > autovacuums. > > Hash Join  (cost=1869142.34..1869146.15 rows=75 width=88) (actual > time=179877.869..179878.011 rows=759 loops=1) >    Hash Cond: (stores.pkey = lt.store_pkey) >    Buffers: shared hit=1654593 read=331897 dirtied=249 >    ->  Seq Scan on stores  (cost=0.00..2.77 rows=77 width=22) (actual > time=0.007..0.023 rows=78 loops=1) >          Buffers: shared hit=2 >    ->  Hash  (cost=1869141.40..1869141.40 rows=75 width=50) (actual > time=179877.847..179877.847 rows=759 loops=1) >          Buckets: 1024  Batches: 1  Memory Usage: 73kB >          Buffers: shared hit=1654591 read=331897 dirtied=249 >          ->  Subquery Scan on lt  (cost=1869138.59..1869141.40 rows=75 > width=50) (actual time=179875.976..179877.697 rows=759 loops=1) >                Buffers: shared hit=1654591 read=331897 dirtied=249 >                ->  GroupAggregate  (cost=1869138.59..1869140.65 rows=75 > width=50) (actual time=179875.976..179877.606 rows=759 loops=1) >                      Group Key: lts.store_pkey, lts.owner, > (date_trunc('minute'::text, lts.date_gifted)) >                      Filter: (count(*) IS NOT NULL) >                      Buffers: shared hit=1654591 read=331897 dirtied=249 >                      ->  Sort  (cost=1869138.59..1869138.78 rows=75 > width=42) (actual time=179875.961..179876.470 rows=6731 loops=1) >                            Sort Key: lts.store_pkey, > lts.entry_source_owner, (date_trunc('minute'::text, lts.date_gifted)) >                            Sort Method: quicksort  Memory: 757kB >                            Buffers: shared hit=1654591 read=331897 > dirtied=249 >                            ->  Index Scan using gifts_date_added on > gifts lts  (cost=0.56..1869136.25 rows=75 width=42) (actual > time=190.657..179870.165 rows=6731 loops=1) >                                  Index Cond: ((date_added > '2018-07-14 > 11:13:05'::timestamp without time zone) AND (date_added < '2018-08-13 > 14:14:21'::timestamp without time zone)) >                                  Filter: ((date_gifted >= '2018-08-13 > 11:13:05'::timestamp without time zone) AND (date_gifted < '2018-08-13 > 14:14:21'::timestamp without time zone)) >                                  Rows Removed by Filter: 938197 >                                  Buffers: shared hit=1654591 > read=331897 dirtied=249 >  Planning time: 0.426 ms >  Execution time: 179893.894 ms > > I don't have a version of this query from prior to this summer, but > getting explain plan for older data from older sandboxes show a similar > plan. I don't have an answer, just a question: Can you provide the actual query and the table schema? > > Sidenote: I am suggesting that an index be added on the date_gifted > field as that is far more selective and avoids throwing rows away. > However I'm very interested in why every node dealing with the gifts > table thinks rows=75 when the actual is much, much higher. And 75 seems > like too round of a number to be random? > > -- > Don Seiler > www.seiler.us -- Adrian Klaver adrian.klaver@aklaver.com