Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1duhhD-0002EC-7m for pgsql-performance@arkaria.postgresql.org; Wed, 20 Sep 2017 16:17:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1duhhC-0007Kc-M4 for pgsql-performance@arkaria.postgresql.org; Wed, 20 Sep 2017 16:17: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.84_2) (envelope-from ) id 1duhfO-0004Fx-VT for pgsql-performance@postgresql.org; Wed, 20 Sep 2017 16:16:03 +0000 Received: from mail-io0-x234.google.com ([2607:f8b0:4001:c06::234]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1duhfH-0000O2-T0 for pgsql-performance@postgresql.org; Wed, 20 Sep 2017 16:16:01 +0000 Received: by mail-io0-x234.google.com with SMTP id 21so4700815iof.6 for ; Wed, 20 Sep 2017 09:15:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Q7sH0np9wORfq5QhC7YD7k9jxBuiF4hVoZDPcwOgDAs=; b=tO3L6EmYI0K+RnpB2aLMlEVDyDwLr41yfPmFG/BWBp0Q3NZytS8Hg5vmqmK4QZz8ni DYPdw7JBXRI6w4DoAPcVw7hUAk8FP1AmvfYreHKWQDvEa9j1iU6zdQ0r0yYbZJODHCu/ OIcSORLSCJW9NhYJXTk+EPK0fwH18oL8Tc8aHAOd0/TZPZajQTWxOoNmZ2S5lzJ3oVGE J0FZmp+COm5HfSDKbSVtTY7RTZegh3ue6BzF3B9SS60ctlmERTYj20JFU2/cZil2INrz X6g7K4u5JCgiuzd/mxpsD2xHn9xca1Y3i6OLNlugpQ9ynXRcAmhK+ixv59nGYn401yIA nUOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Q7sH0np9wORfq5QhC7YD7k9jxBuiF4hVoZDPcwOgDAs=; b=FwUvh3c1e++EiBbBtUZb4PaYVQFIGYeQqgvloZyIM159ZhdYorqlUR+OpVeRDWL/Qx iQhg9VDLASrhNK06Oo4J+bRciVWKh/DYTvcRHBqRi88dTTkpV+hpVAu87XxQlUKrk3YH WUC5EVMkYQ6/hDClek4vcYTlpJDup4LhA7wD+Jq2feBobsRRKQ7jzk0nYGwgAW64BQzR uVyV6OnZ2gCT5yeHPSEubsNmkSQ85Th0x3XN2trCpS+PRzdhtEytOHFYRoBq3crb/lj/ KhcdB4BrZRXrdpDug11lF4koiixOHMOEw9KQbTWuSl8ZNn5fodLYtKOt2TNEGzOhQuZ3 Bclg== X-Gm-Message-State: AHPjjUiqTdlv5ZhDrGZLFOJIa4fhqZ8XEGtNc+0CIxI4txaQUqZAssfF W6abvrAJ0XRwGXtpcS9tVTVczS4ZOckA6fR4vvs= X-Google-Smtp-Source: AOwi7QC+RKsCZxsSb8oAhcsDTIQYK8XzJVI6z9Ra0FK05/dm1FpBvTi6JDdiSSnJqSw5ndyDJZD1dqiCt5P9bWPc79E= X-Received: by 10.202.68.134 with SMTP id r128mr6541832oia.266.1505924154504; Wed, 20 Sep 2017 09:15:54 -0700 (PDT) MIME-Version: 1.0 Received: by 10.157.52.73 with HTTP; Wed, 20 Sep 2017 09:15:53 -0700 (PDT) In-Reply-To: References: <3138.1505508143@sss.pgh.pa.us> From: Mike Broers Date: Wed, 20 Sep 2017 11:15:53 -0500 Message-ID: Subject: Re: query of partitioned object doesnt use index in qa To: Tom Lane Cc: David Rowley , postgres performance list Content-Type: multipart/alternative; boundary="001a1147d64e49bba50559a14a25" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org --001a1147d64e49bba50559a14a25 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I was able to add the suggested indexes like stage.event__00075000((body->>'SID'::text)); and indeed these helped the QA environment use those indexes instead of sequence scanning. I'm still perplexed by my original question, why production uses the partition index and qa does not? Index Scan using ix_event__00014695_landing_id on event__00014695 e_3 (cost=3D0.56..39137.89 rows=3D37697 width=3D564) =E2=94=82 =E2=94=82 Index Cond: (landing_id =3D t_sap.landing_id) Ultimately I think this is just highlighting the need in my environment to set random_page_cost lower (we are on an SSD SAN anyway..), but I dont think I have a satisfactory reason by the row estimates are so bad in the QA planner and why it doesnt use that partition index there. On Fri, Sep 15, 2017 at 3:59 PM, Mike Broers wrote: > That makes a lot of sense, thanks for taking a look. An index like you > suggest would probably further improve the query. Is that suggestion > sidestepping the original problem that production is evaluating the > landing_id bit with the partition index and qa is sequence scanning inste= ad? > > AND exists (select 1 from t_sap where e.landing_id =3D t_sap.landing_id))= as > rankings; > > Based on the difference in row estimate I am attempting an analyze with a > higher default_statistic_target (currently 100) to see if that helps. > > > > > On Fri, Sep 15, 2017 at 3:42 PM, Tom Lane wrote: > >> Mike Broers writes: >> > If Im reading this correctly postgres thinks the partition will return >> 6.5 >> > million matching rows but actually comes back with 162k. Is this a ca= se >> > where something is wrong with the analyze job? >> >> You've got a lot of scans there that're using conditions like >> >> > =E2=94=82 -> Seq Scan on event__99999999 e_= 1 >> (cost=3D0.00..2527828.05 rows=3D11383021 width=3D778) (actual >> time=3D25522.389..747238.885 rows=3D42 loops=3D1) >> > =E2=94=82 Filter: (((body ->> 'SID'::t= ext) IS >> NOT NULL) AND (validation_status_code =3D 'P'::bpchar)) >> > =E2=94=82 Rows Removed by Filter: 1217= 2186 >> >> While I'd expect the planner to be pretty solid on estimating the >> validation_status_code condition, it's not going to have any idea about >> that JSON field test. That's apparently very selective, but you're just >> getting a default estimate, which is not going to think that a NOT NULL >> test will exclude lots of rows. >> >> One thing you could consider doing about this is creating an index >> on (body ->> 'SID'::text), which would prompt ANALYZE to gather statisti= cs >> about that expression. Even if the index weren't actually used in the >> plan, this might improve the estimates and the resulting planning choice= s >> enough to make it worth maintaining such an index. >> >> Or you could think about pulling that field out and storing it on its ow= n. >> JSON columns are great for storing random unstructured data, but they ar= e >> less great when you want to do relational-ish things on subfields. >> >> regards, tom lane >> > > --001a1147d64e49bba50559a14a25 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I was able to add the suggested indexes like=C2=A0stage.ev= ent__00075000((body->>'SID'::text)); and indeed these helped = the QA environment use those indexes instead of sequence scanning.=C2=A0
I'm still perplexed by my original question, why produ= ction uses the partition index and qa does not?

Index Scan using ix_event__00014695_lan= ding_id on event__00014695 e_3=C2=A0=C2=A0(cost=3D0.56..39137.89 rows=3D= 37697 width=3D564)=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0<= /span>=E2=94=82

=E2=94=82=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0Index Cond= : (landing_id =3D t_sap.landing_id)=C2=A0=C2=A0 =C2=A0=C2=A0

<= p class=3D"gmail-m_5399471546080563555gmail-p1" style=3D"font-size:12.8px">=

Ultimately I think this is just highlighting the need in my enviro= nment to set random_page_cost lower (we are on an SSD SAN anyway..), but I = dont think I have a satisfactory reason by the row estimates are so bad in = the QA planner and why it doesnt use that partition index there.




On Fri,= Sep 15, 2017 at 3:59 PM, Mike Broers <mbroers@gmail.com> wr= ote:
That makes a lot of= sense, thanks for taking a look.=C2=A0 An index like you suggest would pro= bably further improve the query. =C2=A0 Is that suggestion sidestepping the= original problem that production is evaluating the landing_id bit with the= partition index and qa is sequence scanning instead?
=
AND exists (select 1 from t_sap where e.landing_id =3D t_sap= .landing_id)) as rankings;=C2=A0

Based on the differen= ce in row estimate I am attempting an analyze with a higher default_statist= ic_target (currently 100) to see if that helps.

=

<= /span>


On Fri, Sep 15, 2017 a= t 3:42 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Mike Broers <mbroers@gmail.com> writes:
> If Im reading this correctly postgres thinks the partition will return= 6.5
> million matching rows but actually comes back with 162k.=C2=A0 Is this= a case
> where something is wrong with the analyze job?

You've got a lot of scans there that're using conditions lik= e

> =E2=94=82=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0->=C2=A0 Seq Scan on event__999999= 99 e_1 (cost=3D0.00..2527828.05 rows=3D11383021 width=3D778) (actual time= =3D25522.389..747238.885 rows=3D42 loops=3D1)
> =E2=94=82=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Filter: (((body = ->> 'SID'::text) IS NOT NULL) AND (validation_status_code =3D= 'P'::bpchar))
> =E2=94=82=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Rows Removed by = Filter: 12172186

While I'd expect the planner to be pretty solid on estimating th= e
validation_status_code condition, it's not going to have any idea about=
that JSON field test.=C2=A0 That's apparently very selective, but you&#= 39;re just
getting a default estimate, which is not going to think that a NOT NULL
test will exclude lots of rows.

One thing you could consider doing about this is creating an index
on (body ->> 'SID'::text), which would prompt ANALYZE to gath= er statistics
about that expression.=C2=A0 Even if the index weren't actually used in= the
plan, this might improve the estimates and the resulting planning choices enough to make it worth maintaining such an index.

Or you could think about pulling that field out and storing it on its own.<= br> JSON columns are great for storing random unstructured data, but they are less great when you want to do relational-ish things on subfields.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane


--001a1147d64e49bba50559a14a25--