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 1jvRAD-0001hk-LS for pgsql-general@arkaria.postgresql.org; Tue, 14 Jul 2020 20:04:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jvRAC-0007tJ-Gf for pgsql-general@arkaria.postgresql.org; Tue, 14 Jul 2020 20:04:28 +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 1jvRAC-0007t4-10 for pgsql-general@lists.postgresql.org; Tue, 14 Jul 2020 20:04:28 +0000 Received: from mail-qt1-x831.google.com ([2607:f8b0:4864:20::831]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jvRA9-0002IA-Hz for pgsql-general@postgresql.org; Tue, 14 Jul 2020 20:04:27 +0000 Received: by mail-qt1-x831.google.com with SMTP id j10so13845886qtq.11 for ; Tue, 14 Jul 2020 13:04:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PDeVMtJltulb7+14gG3sHKRRwtYPSzyZkjyKRWrGvlQ=; b=kfZv4m/U3nh1t0HkBDr8EogK3DDtEeiwlBGeyHmo+yqAjDBPmi6wZnoR/lRAccCHA0 p6lX+lH77HxRPuKe0lh3cpKWoLJql0WwxwtXIVVxm2ym9bkAcS4wW3X9GGtAdmoytg8P i7YJFLhfsw0npwwYJKq2eiOq+TidVJXMq9dCrnBP5P7xHZ6WQ2htIkOr8pbv9jXnO3vB GKY4KRBu+1jklRv7S5wUnUCxnrAjmg4l6G6Iag4p65YO9LXk0DHJXbGDp+uLPF8TCxhY GUqkrqrpE2Hj8SuagBEydNTYSRybzzo7h+8GLU6eVp8G+G0OCZLc65DDfOO8T/I2Q4kt aWmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PDeVMtJltulb7+14gG3sHKRRwtYPSzyZkjyKRWrGvlQ=; b=TaTS6U+BVhCXjihbck87gzPKxT5DtKnqeyT2f2y35uUNTTePq5En5Bsd1TbJdx+/F9 0SNlEPJbTMLwwTObL0984SLIV1HYPYBUkONleeCjodU6EhypY+HmeqDW3Pehw18y39qU /3DpfNSeCXa/H0Mi9WugOAqOCEzlsy/aju6TjCLLr+UTX88uU1EwudFoHA6wXCoByXjg t2m0vAqL+SRBzQO3j+Si2IZY8ekyRztrLF+/uInE78oWolnr6F/cEgoYw9COWGMY1gyH Zu5fpn6TpNkQRhRiodUTwEVyRZ52Dlcen7xgouvAWT3ut/WyBqbAe5gj/A+SLE48wJtg YYSA== X-Gm-Message-State: AOAM532ax1d8G3Rnw4+wNlBdolznCAPNXwbhft5HaC2tPCFyvZdODwjE QDFVnGbHtQkcNd82essud4ujVggR8lKZ+T6AerU= X-Google-Smtp-Source: ABdhPJwzxWbkbjNgJ9yXC9GLYEzrmHf75P0Dgzt2Af13k4lYsITHvW+uuUi+lI8aLB5YJaFUdqosV1RDSpKkO52zedc= X-Received: by 2002:ac8:87d:: with SMTP id x58mr6353884qth.28.1594757062641; Tue, 14 Jul 2020 13:04:22 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Radoslav Nedyalkov Date: Tue, 14 Jul 2020 23:04:11 +0300 Message-ID: Subject: Re: single table - fighting a seq scan To: Michael Lewis Cc: pgsql-general Content-Type: multipart/alternative; boundary="00000000000038cce405aa6c5025" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --00000000000038cce405aa6c5025 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Ah, I could have messed up the examples I gave. Row numbers are different. Once again the plans , sorry about that. -- 200 entries Gather (cost=3D1000.00..106905910.97 rows=3D7893 width=3D8) Workers Planned: 2 -> Parallel Seq Scan on te (cost=3D0.00..106904121.67 rows=3D3289 widt= h=3D8) Filter: (current_pid =3D ANY ('{240900026,240900027,240900028,240900029,240900030,240900031,240900032,24= 0900033,240900034,240900035,240900036,240900037,240900038,240900039,2409000= 40,240900041,240900042,240900043,240900044,240900045,240900046,240900047,24= 0900048,240900049,240900050,240900051,240900052,240900053,240900054,2409000= 55,240900056,240900057,240900058,240900059,240900060,240900061,240900062,24= 0900063,240900064,240900065,240900066,240900067,240900068,240900069,2409000= 70,240900071,240900072,240900073,240900074,240900075,240900076,240900077,24= 0900078,240900079,240900080,240900081,240900082,240900083,240900084,2409000= 85,240900086,240900165,240900087,240900166,240900088,240900167,240900089,24= 0900168,240900090,240900169,240900091,240900170,240900092,240900171,2409000= 93,240900172,240900094,240900173,240900905,240900174,240900175,240900176,24= 0900177,240900178,240900179,240900180,240900181,240900182,240900183,2409001= 84,240900185,240900186,240900187,240900188,240900189,240900190,240900191,24= 0900192,240900193,240900194,240900195,240900196,240900197,240900198,2409001= 99,240900906,240900907,240900908,240900909,240900910,240900911,240900912,24= 0900913,240900914,240900915,240900916,240900917,240900918,240900919,2409009= 20,240900921,240900922,240900923,240900924,240900925,240900926,240900927,24= 0900928,240901048,240901053,240901054,240901055,240901056,240901057,2409010= 58,240901059,240901060,240901061,240901062,240901063,240901064,240901065,24= 0901066,240901067,240901068,240901069,240901070,240901071,240901072,2409010= 73,240901074,240901075,240901076,240901077,240901078,240901079,240901080,24= 0901081,240901082,240901083,240901084,240901085,240901086,240901087,2409010= 88,240901089,240901090,240901091,240901092,240901093,240901094,240901095,24= 0901096,240901097,240901098,240901099,240901100,240901101,240901102,2409011= 03,240901104,240901105,240901106,240901107,240901108,240901109,240901110,24= 0901111,240901112,240901113,240901114,240901115,240901116,240901117,2409011= 18,240901119,240901120,240901121,240901122,240901123,240901124,240901125,24= 0901126,240901127,240901128,240901129}'::bigint[])) (4 rows) Time: 5.261 ms =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D -- 100 entries ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------- Index Scan using te_current_pid_idx on te (cost=3D0.57..731.26 rows=3D383= 2 width=3D8) (actual time=3D1.244..15.897 rows=3D600 loops=3D1) Index Cond: (current_pid =3D ANY ('{240900026,240900027,240900028,240900029,240900030,240900031,240900032,24= 0900033,240900034,240900035,240900036,240900037,240900038,240900039,2409000= 40,240900041,240900042,240900043,240900044,240900045,240900046,240900047,24= 0900048,240900049,240900050,240900051,240900052,240900053,240900054,2409000= 55,240900056,240900057,240900058,240900059,240900060,240900061,240900062,24= 0900063,240900064,240900065,240900066,240900067,240900068,240900069,2409000= 70,240900071,240900072,240900073,240900074,240900075,240900076,240900077,24= 0900078,240900079,240900080,240900081,240900082,240900083,240900084,2409000= 85,240900086,240900165,240900087,240900166,240900088,240900167,240900089,24= 0900168,240900090,240900169,240900091,240900170,240900092,240900171,2409000= 93,240900172,240900094,240900173,240900905,240900174,240900175,240900176,24= 0900177,240900178,240900179,240900180,240900181,240900182,240900183,2409001= 84,240900185,240900186,240900187,240900188,240900189,240900190,240900191,24= 0900192,240900193,240900194}'::bigint[])) Planning Time: 3.430 ms Execution Time: 15.954 ms (4 rows) Time: 20.257 ms On Tue, Jul 14, 2020 at 10:31 PM Radoslav Nedyalkov wrote: > Hi Michael, > full output from the query is attached. > here is the truncated lists version. > > frac_mcv | 0.00306267 > tablename | te > attname | current_pid > inherited | f > null_frac | 0.261823 > n_distinct | 1.59236e+07 > n_mcv | 560 > n_hist | 5001 > correlation | 0.995502 > schemaname | public > tablename | te > attname | current_pid > inherited | f > null_frac | 0.261823 > avg_width | 8 > n_distinct | 1.59236e+07 > most_common_vals | > {15026003,24951186,220707698,223344198,224236736,224355865,224359830,2243= 71584,224380154,224382722,224388639,224390209,224394943,224396835,228259607= ,232148477,232173137,232379194,232729185,232913953,236304699,236797618,2373= 55501,238860629,239082658} > most_common_freqs | > {4.46667e-05,1.93333e-05,4.66667e-06,4.66667e-06,...,4e-06,4e-06,4e-06,4e= -06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06= ,4e-06,4e-06,4e-06,4e-06} > histogram_bounds | > {13426761,13467316,13510844,..239215632,239302648,239371125,239466529,239= 532095,239571468,239611801,239634487} > correlation | 0.995502 > most_common_elems | (null) > most_common_elem_freqs | (null) > elem_count_histogram | (null) > > Thank You very much for the response. > I'll try with settings as you propose. > > Rado > > On Tue, Jul 14, 2020 at 9:46 PM Michael Lewis wrote: > >> rows=3D3832 >> rows=3D3870 >> >> Your estimate changed very little when you included 100 values vs 200 >> values. That is interesting to me. >> >> What does the below query give you? How many of those 200 values are >> found in the MCVs list? If n_distinct is low, and most of the values are >> NOT in the most common value list, and the fraction of the table covered= by >> the MCVs is also low, then the planner will expect that each of the 200 >> values represents some deceivingly high portion of the table. >> >> You said there are 80 million rows, yes? That seems likely that ndistinc= t >> and the MCVs list are not giving info very correlated with reality. You = may >> want to increase the minimum table size for sequential to kick in. I can= not >> recall the name of that setting at the moment. You may also want to >> increase stats target on that column at least, analyze, and explain the >> query again. >> >> SELECT >> ( SELECT SUM (x) FROM UNNEST (most_common_freqs) x ) frac_MCV, >> tablename, >> attname, >> inherited, >> null_frac, >> n_distinct, >> array_length(most_common_vals,1) n_mcv, >> array_length(histogram_bounds,1) n_hist, >> correlation, >> * >> FROM pg_stats >> WHERE >> schemaname =3D 'public' >> AND tablename=3D'te' >> AND attname=3D'current_pid'; >> >>> --00000000000038cce405aa6c5025 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Ah, I could have messed up the examples I gave. Row number= s are different.
Once=C2=A0again the=C2=A0plans , sorry about that.

-- 200 entries

=C2=A0Gather =C2=A0(cost=3D1000.00..106905910.97 rows=3D7893 width=3D8= )
=C2=A0 =C2=A0Workers Planned: 2
=C2=A0 =C2=A0-> =C2=A0Parallel S= eq Scan on te =C2=A0(cost=3D0.00..106904121.67 rows=3D3289 width=3D8)
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Filter: (current_pid =3D ANY ('{24090= 0026,240900027,240900028,240900029,240900030,240900031,240900032,240900033,= 240900034,240900035,240900036,240900037,240900038,240900039,240900040,24090= 0041,240900042,240900043,240900044,240900045,240900046,240900047,240900048,= 240900049,240900050,240900051,240900052,240900053,240900054,240900055,24090= 0056,240900057,240900058,240900059,240900060,240900061,240900062,240900063,= 240900064,240900065,240900066,240900067,240900068,240900069,240900070,24090= 0071,240900072,240900073,240900074,240900075,240900076,240900077,240900078,= 240900079,240900080,240900081,240900082,240900083,240900084,240900085,24090= 0086,240900165,240900087,240900166,240900088,240900167,240900089,240900168,= 240900090,240900169,240900091,240900170,240900092,240900171,240900093,24090= 0172,240900094,240900173,240900905,240900174,240900175,240900176,240900177,= 240900178,240900179,240900180,240900181,240900182,240900183,240900184,24090= 0185,240900186,240900187,240900188,240900189,240900190,240900191,240900192,= 240900193,240900194,240900195,240900196,240900197,240900198,240900199,24090= 0906,240900907,240900908,240900909,240900910,240900911,240900912,240900913,= 240900914,240900915,240900916,240900917,240900918,240900919,240900920,24090= 0921,240900922,240900923,240900924,240900925,240900926,240900927,240900928,= 240901048,240901053,240901054,240901055,240901056,240901057,240901058,24090= 1059,240901060,240901061,240901062,240901063,240901064,240901065,240901066,= 240901067,240901068,240901069,240901070,240901071,240901072,240901073,24090= 1074,240901075,240901076,240901077,240901078,240901079,240901080,240901081,= 240901082,240901083,240901084,240901085,240901086,240901087,240901088,24090= 1089,240901090,240901091,240901092,240901093,240901094,240901095,240901096,= 240901097,240901098,240901099,240901100,240901101,240901102,240901103,24090= 1104,240901105,240901106,240901107,240901108,240901109,240901110,240901111,= 240901112,240901113,240901114,240901115,240901116,240901117,240901118,24090= 1119,240901120,240901121,240901122,240901123,240901124,240901125,240901126,= 240901127,240901128,240901129}'::bigint[]))
(4 rows)

Time: 5.= 261 ms
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D



-- 100 entries
--------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= -----------------------------------------------------

=C2=A0Index Sc= an using te_current_pid_idx on te =C2=A0(cost=3D0.57..731.26 rows=3D3832 wi= dth=3D8) (actual time=3D1.244..15.897 rows=3D600 loops=3D1)
=C2=A0 =C2= =A0Index Cond: (current_pid =3D ANY ('{240900026,240900027,240900028,24= 0900029,240900030,240900031,240900032,240900033,240900034,240900035,2409000= 36,240900037,240900038,240900039,240900040,240900041,240900042,240900043,24= 0900044,240900045,240900046,240900047,240900048,240900049,240900050,2409000= 51,240900052,240900053,240900054,240900055,240900056,240900057,240900058,24= 0900059,240900060,240900061,240900062,240900063,240900064,240900065,2409000= 66,240900067,240900068,240900069,240900070,240900071,240900072,240900073,24= 0900074,240900075,240900076,240900077,240900078,240900079,240900080,2409000= 81,240900082,240900083,240900084,240900085,240900086,240900165,240900087,24= 0900166,240900088,240900167,240900089,240900168,240900090,240900169,2409000= 91,240900170,240900092,240900171,240900093,240900172,240900094,240900173,24= 0900905,240900174,240900175,240900176,240900177,240900178,240900179,2409001= 80,240900181,240900182,240900183,240900184,240900185,240900186,240900187,24= 0900188,240900189,240900190,240900191,240900192,240900193,240900194}'::= bigint[]))
=C2=A0Planning Time: 3.430 ms
=C2=A0Execution Time: 15.954= ms
(4 rows)

Time: 20.257 ms



On Tue, Jul 14, = 2020 at 10:31 PM Radoslav Nedyalkov <rnedyalkov@gmail.com> wrote:
Hi Michael,
full output from = the query is attached.
here is the truncated lists version.
=

frac_mcv =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 | 0.00306267
tablename =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0| te
attname =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0| current_pid
inherited =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0| f
null_frac =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0| 0.261823
n_distinct =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 | 1.59236e+07
n_mcv =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0| 560
n_hist =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 | 5001
correlation =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0| 0.995502
schemaname =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | pub= lic
tablename =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| te
at= tname =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| current_pid<= br>inherited =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| f
null_fr= ac =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| 0.261823
avg_width = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| 8
n_distinct =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | 1.59236e+07
most_common_vals =C2=A0= =C2=A0 =C2=A0 | {15026003,24951186,220707698,223344198,224236736,224355865= ,224359830,224371584,224380154,224382722,224388639,224390209,224394943,2243= 96835,228259607,232148477,232173137,232379194,232729185,232913953,236304699= ,236797618,237355501,238860629,239082658}
most_common_freqs =C2=A0 =C2= =A0 =C2=A0| {4.46667e-05,1.93333e-05,4.66667e-06,4.66667e-06,...,4e-06,4e-0= 6,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4e-06,4= e-06,4e-06,4e-06,4e-06,4e-06,4e-06}
histogram_bounds =C2=A0 =C2=A0 =C2= =A0 | {13426761,13467316,13510844,..239215632,239302648,239371125,239466529= ,239532095,239571468,239611801,239634487}
correlation =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0| 0.995502
most_common_elems =C2=A0 =C2=A0 =C2= =A0| (null)
most_common_elem_freqs | (null)
elem_count_histogram =C2= =A0 | (null)


Thank You very much for the response.
I'll try with= settings as you propose.

Rado

On Tue, Jul 14= , 2020 at 9:46 PM Michael Lewis <mlewis@entrata.com> wrote:
rows=3D3832
rows=3D3= 870

Your estimate changed very l= ittle when you included 100 values vs 200 values. That is interesting to me= .

What does the below quer= y give you? How many of those 200 values are found in the MCVs list? If=C2= =A0n_distinct is low, and most of the values are NOT in the most common val= ue list, and the fraction of the table covered by the MCVs is also low, the= n the planner will expect that each of the 200 values represents some decei= vingly high portion of the table.

You said there are 80 million rows= , yes? That seems likely that ndistinct and the MCVs list are not giving in= fo very correlated with=C2=A0reality. You may want to increase the minimum= =C2=A0table size for sequential to kick in. I cannot recall the name of th= at setting at the moment. You may also want to increase=C2=A0stats target o= n that column at least, analyze, and explain the query again.

SELECT
( SELECT SUM (x) FROM UNNEST (most_common_freqs) x = ) frac_MCV,
tablename,
attname,
inherited,
null_frac,
n= _distinct,
array_length(most_common_vals,1) n_mcv,
array_length(his= togram_bounds,1) n_hist,
correlation,
*
FROM pg_stats
WHERE schemaname =3D 'public'
AND tablename=3D'te'
AND = attname=3D'current_pid'= ;;
--00000000000038cce405aa6c5025--