Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jniOl-0007FV-2r for pgsql-general@arkaria.postgresql.org; Tue, 23 Jun 2020 12:51:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jniOj-0002FI-Rs for pgsql-general@arkaria.postgresql.org; Tue, 23 Jun 2020 12:51:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jniOj-0002FA-Cy for pgsql-general@lists.postgresql.org; Tue, 23 Jun 2020 12:51:33 +0000 Received: from mail-oi1-x244.google.com ([2607:f8b0:4864:20::244]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jniOc-0003KK-9j for pgsql-general@lists.postgresql.org; Tue, 23 Jun 2020 12:51:32 +0000 Received: by mail-oi1-x244.google.com with SMTP id a21so18708128oic.8 for ; Tue, 23 Jun 2020 05:51:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=FgT8QjEGuvvFb840PQAANQDYdmQTjiJEjanfdDh/jLY=; b=mgjdtXpm2L6gNeAcV8PWNh91IRw6eMOq3MGOeD/25q5VxCTsHB1PcD6JCFYvhG6VjQ 1nEOKV1QK1dlq+3JFx2mMTz/qFBwyZi+Ce4WYKA8cuQL4Lj4j3TbugtKaCXaVM45pTT8 neMQ0iY1AgS/slgjDvReGiSv2DAvkV3GVBxY6NbmSCmnA9O5pFlIGB9KSo6nP6+FVopK zHbUgtF2LBEBXUc+S6nqdeLDUBW3OEOdXfNI6aSXuhjl52YhVcuUpm7mgzlgbAq6F7Up GCqecOsXccXXmWBmyoKtzfQ1o1TzWG40QzdUE+d++u+IWxfrPGqaNw6yQJlPh4HGGn4J 6oHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=FgT8QjEGuvvFb840PQAANQDYdmQTjiJEjanfdDh/jLY=; b=ea+LDNjkLUvATwAe4XUfCPfYZbpHjNQVo8TWtlCy7ONzaMcP1a8vGr/QPJsd2liCqu 5uD4jReAOqXHElb8aQQ/7099lL7zJmB7O3fEkzOLKmYLztm1pFEx6VRLTTVq4pB1iXV8 Vn2+qXt1qKEWbVgyN43JdxGy7rF0ZjP+zXUuQtJg3rFDE2m//Ijn1MvVnoyrKy8bH1w8 7s/RVc0rJ3Q6adxqH5IeXdniQPMtvF2s7YZNjmlM0EAr/KErpWP6C9ecKSJaT/7Xffai 1SdFK8rVJ6hZ9uLcbh4p52u5bD1k/t9dYKDEPUtfiX1cVyrynJdDK0MC1JhLJIA7wOeS zIIg== X-Gm-Message-State: AOAM532bWid6Ro+86kO8qCWUW18DWvJE8Dh9modBgGELhkJF4T7b13gn 6ujxGZfGxBpUjIbcrIq1CbrD/8QqE0mLEQ== X-Google-Smtp-Source: ABdhPJwfE5mYIMXlWh5cJRLBD6JPNKN2SrnBhbbNM3azKhMcKvaF8Dzt3183wZypPBHdIpOr7Ov+5g== X-Received: by 2002:a05:6808:7d1:: with SMTP id f17mr16038975oij.16.1592916685204; Tue, 23 Jun 2020 05:51:25 -0700 (PDT) Received: from [192.168.1.10] (ip70-186-126-197.no.no.cox.net. [70.186.126.197]) by smtp.googlemail.com with ESMTPSA id e68sm4016095ote.56.2020.06.23.05.51.24 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Jun 2020 05:51:24 -0700 (PDT) Subject: Re: n_distinct off by a factor of 1000 To: pgsql-general@lists.postgresql.org References: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com> From: Ron Message-ID: <7879cb87-5a68-edec-9ec6-3f082d78ed86@gmail.com> Date: Tue, 23 Jun 2020 07:51:23 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com> Content-Type: multipart/alternative; boundary="------------84A3431975CA19C7CAD32CFA" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------84A3431975CA19C7CAD32CFA Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Maybe I missed it, but did you run "ANALYZE VERBOSE bigtable;"? On 6/23/20 7:42 AM, Klaudie Willis wrote: > Friends, > > I run Postgresql 12.3, on Windows. I have just discovered a pretty > significant problem with Postgresql and my data.  I have a large table, > 500M rows, 50 columns. It is split in 3 partitions by Year.  In addition > to the primary key, one of the columns is indexed, and I do lookups on this. > > Select * from bigtable b where b.instrument_ref in (x,y,z,...) > limit 1000 > > It responded well with sub-second response, and it uses the index of the > column.  However, when I changed it to: > > Select * from bigtable b where b.instrument_ref in (x,y,z,) > limit 10000 -- (notice 10K now) > > The planner decided to do a full table scan on the entire 500M row table! > And that did not work very well.  First I had no clue as to why it did so, > and when I disabled sequential scan the query immediately returned.  But I > should not have to do so. > > I got my first hint of why this problem occurs when I looked at the > statistics.  For the column in question, "instrument_ref" the statistics > claimed it to be: > > The default_statistics_target=500, and analyze has been run. > select * from pg_stats where attname like 'instr%_ref'; -- Result: *40.000* > select count(distinct instrumentid_ref) from bigtable -- Result: *33 385 > 922 (!!)* > > That is an astonishing difference of almost a 1000X. > > When the planner only thinks there are 40K different values, then it makes > sense to switch to table scan in order to fill the limit=10.000.  But it > is wrong, very wrong, an the query returns in 100s of seconds instead of a > few. > > I have tried to increase the statistics target to 5000, and it helps, but > it reduces the error to 100X.  Still crazy high. > > I understand that this is a known problem.  I have read previous posts > about it, still I have never seen anyone reach such a high difference factor. > > I have considered these fixes: > - hardcode the statistics to a particular ratio of the total number of rows > - randomize the rows more, so that it does not suffer from page > clustering.  However, this has probably other implications > > Feel free to comment :) > > > K > -- Angular momentum makes the world go 'round. --------------84A3431975CA19C7CAD32CFA Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit Maybe I missed it, but did you run "ANALYZE VERBOSE bigtable;"?

On 6/23/20 7:42 AM, Klaudie Willis wrote:
Friends,

I run Postgresql 12.3, on Windows. I have just discovered a pretty significant problem with Postgresql and my data.  I have a large table, 500M rows, 50 columns. It is split in 3 partitions by Year.  In addition to the primary key, one of the columns is indexed, and I do lookups on this.

Select * from bigtable b where b.instrument_ref in (x,y,z,...)
limit 1000

It responded well with sub-second response, and it uses the index of the column.  However, when I changed it to:

Select * from bigtable b where b.instrument_ref in (x,y,z,)
limit 10000 -- (notice 10K now)

The planner decided to do a full table scan on the entire 500M row table! And that did not work very well.  First I had no clue as to why it did so, and when I disabled sequential scan the query immediately returned.  But I should not have to do so.

I got my first hint of why this problem occurs when I looked at the statistics.  For the column in question, "instrument_ref" the statistics claimed it to be:

The default_statistics_target=500, and analyze has been run.
select * from pg_stats where attname like 'instr%_ref'; -- Result: 40.000
select count(distinct instrumentid_ref) from bigtable -- Result: 33 385 922 (!!)

That is an astonishing difference of almost a 1000X. 

When the planner only thinks there are 40K different values, then it makes sense to switch to table scan in order to fill the limit=10.000.  But it is wrong, very wrong, an the query returns in 100s of seconds instead of a few.

I have tried to increase the statistics target to 5000, and it helps, but it reduces the error to 100X.  Still crazy high.

I understand that this is a known problem.  I have read previous posts about it, still I have never seen anyone reach such a high difference factor.

I have considered these fixes:
- hardcode the statistics to a particular ratio of the total number of rows
- randomize the rows more, so that it does not suffer from page clustering.  However, this has probably other implications

Feel free to comment :)


K


--
Angular momentum makes the world go 'round.
--------------84A3431975CA19C7CAD32CFA--