Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpxlk-0004sA-2z for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Feb 2019 16:03:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gpxli-0003cK-Sx for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Feb 2019 16:03:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpxbt-0005tl-JX for pgsql-bugs@lists.postgresql.org; Sat, 02 Feb 2019 15:53:37 +0000 Received: from sonic306-21.consmr.mail.ne1.yahoo.com ([66.163.189.83]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpxbq-0006T0-3G for pgsql-bugs@lists.postgresql.org; Sat, 02 Feb 2019 15:53:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1549122812; bh=YZxv7sAs4K9+AaGLfYFIB4VtKP4gX2UR/Z/hkjNe99A=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From:Subject; b=TaRKwbT4lOWb7ol5VokaWa3NnmtAQIDndecSJzz+v8vA8Cf6lTDToiaThgQQ4w04zhhlZj0XLgA6n/PYg0e4Uu8rNZ0mskOK8rpvc/MLw+5eE7w9esICdieG6KD5Hz0JCM03rWbl7EmA2KdEaj3GaMs9pFPjC1gprlF844jJXfIGYVNiJQ5ugWQ27XmWLraDTk32y72p9gwh0rOonDgtlJSntYKUxb0q8WCzRB4l36v2gvvaK34MvDvPLIdisk8Ry2yiqO9pdVacNRHEkMfqmCh8VrVudtFvL6sRdpN1K8dsFD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Zx4AZPa4AJQoylei_6VURtUc5mq8bk8FBYQ2KQ8QBO U1PkBO3yuaMlIy8WadxdZAeLh5nL8cq8Qq7i76R2E8Zzrs1DZG6SENQQ9vL8NXudHkO8E1btceek J8Q-- Received: from sonic.gate.mail.ne1.yahoo.com by sonic306.consmr.mail.ne1.yahoo.com with HTTP; Sat, 2 Feb 2019 15:53:32 +0000 Date: Sat, 2 Feb 2019 15:53:30 +0000 (UTC) From: Alex To: Tom Lane Cc: pgsql-bugs@lists.postgresql.org, Pavel Stehule Message-ID: <602024371.2009661.1549122810083@mail.yahoo.com> In-Reply-To: <23837.1549120085@sss.pgh.pa.us> References: <15618-5357e448d7cbeb27@postgresql.org> <378349148.1969803.1549109284822@mail.yahoo.com> <23837.1549120085@sss.pgh.pa.us> Subject: Re: BUG #15618: Duplicating a join clause breaks index use MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2009660_2034538936.1549122810081" X-Mailer: WebService/1.1.13027 YMailNorrin Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0 Content-Length: 7580 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_2009660_2034538936.1549122810081 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Indeed, increasing those two parameter values results in a fast query. With default values, the planning time is ~2ms but execution time is ~4600m= s.With increased values, the planning time increases by 0.5ms (2.5ms) but e= xecution time drops to 0.7ms (aprox x6000 faster). As the impact on the execution time is very high, shouldn't another mechani= sm be used instead of parameters join_collapse_limit or from_collapse_limit= , such as some maximum planning time?=20 With a maximum planning time, faster machines will benefit from better quer= y planning. Another suggestion is to run a benchmark on the machine to automatically de= termine some good values, and attach hardware info to the results, to recom= pute in case hardware changes (although hdd performance may degrade with fr= agmentation and ssd with space occupied). On Saturday, February 2, 2019, 5:08:15 PM GMT+2, Tom Lane wrote: =20 =20 Alex writes: > select > ... > from > =C2=A0=C2=A0=C2=A0 product p > =C2=A0=C2=A0=C2=A0 left outer join product_property pp on p.id=3Dpp.produ= ct_id > =C2=A0=C2=A0=C2=A0 left outer join product_property_value ppv on pp.value= _id=3Dppv.id > =C2=A0=C2=A0=C2=A0 left outer join description_element de on pp.descripti= on_source=3Dde.id > =C2=A0=C2=A0=C2=A0 left outer join description_value dev on de.descriptio= n_value_id=3Ddev.id > =C2=A0=C2=A0=C2=A0 left outer join description_element dep on de.parent= =3Ddep.id > =C2=A0=C2=A0=C2=A0 left outer join description_value depv on dep.descript= ion_value_id=3Ddepv.id > =C2=A0=C2=A0=C2=A0 left outer join shop_category sc_dup on p.shop_categor= y=3Dsc_dup.id=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -= -duplicate join > =C2=A0=C2=A0=C2=A0 left outer join shop_category sc on p.shop_category=3D= sc.id > =C2=A0=C2=A0=C2=A0 left outer join category c on sc.category=3Dc.id > where > =C2=A0=C2=A0=C2=A0 c.id=3D8511 I suspect the real issue here has little to do with the "duplicate" nature of the extra join, and a lot to do with the fact that your query exceeds join_collapse_limit.=C2=A0 That causes the planner to break it up in a semi-arbitrary way to limit planning time.=C2=A0 Adding one more join moves where the break happens, and evidently is preventing the planner from discovering a good join order. You want join_collapse_limit to be at least 10 to handle this query well, since 10 tables are mentioned.=C2=A0 The default value, 8, is kinda tuned f= or older and slower machines than most of us use today.=C2=A0 I wouldn't make = it enormous, because it does prevent exponential planning time growth for many tables, but you could likely get away with 15 or so.=C2=A0 You should likely keep from_collapse_limit in sync with that too. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 regards, tom lane = =20 ------=_Part_2009660_2034538936.1549122810081 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Indeed, increasing those two parameter values results in a fas= t query.

With default values, the planning tim= e is ~2ms but execution time is ~4600ms.
With increased values, t= he planning time increases by 0.5ms (2.5ms) but execution time drops to 0.7= ms (aprox x6000 faster).

As the impact on the exec= ution time is very high, shouldn't another mechanism be used instead of par= ameters join_collapse_limit or from_collapse_limit, such as s= ome maximum planning time?
With a maximum planning time, fas= ter machines will benefit from better query planning.

Another suggestion is to run a benchmark on the machine to automati= cally determine some good values, and attach hardware info to the re= sults, to recompute in case hardware changes (although hdd performance may = degrade with fragmentation and ssd with space occupied).
=20
=20
On Saturday, February 2, 2019, 5:08:15 PM GMT+2, Tom La= ne <tgl@sss.pgh.pa.us> wrote:


Alex <cdalxndr@yah= oo.com> writes:

> select
> ...
> = from
>     product p
= >     left outer join product_property pp on p.id=3Dpp.pr= oduct_id
>     left outer join product_= property_value ppv on pp.value_id=3Dppv.id
>  &nb= sp;  left outer join description_element de on pp.description_source= =3Dde.id
>     left outer join descript= ion_value dev on de.description_value_id=3Ddev.id
> &n= bsp;   left outer join description_element dep on de.parent=3Ddep= .id
>     left outer join description_v= alue depv on dep.description_value_id=3Ddepv.id
> &nbs= p;   left outer join shop_category sc_dup on p.shop_category=3Dsc= _dup.id           --dupli= cate join
>     left outer join shop_ca= tegory sc on p.shop_category=3Dsc.id
>   &nb= sp; left outer join category c on sc.category=3Dc.id
>= where
>     c.id=3D8511


I suspect the real issue here has little to do= with the "duplicate" nature
of the extra join, and a lot= to do with the fact that your query exceeds
join_collaps= e_limit.  That causes the planner to break it up in a
semi-arbitrary way to limit planning time.  Adding one more join mov= es
where the break happens, and evidently is preventing t= he planner from
discovering a good join order.

You want join_collapse_limit to be at least 10= to handle this query well,
since 10 tables are mentioned= .  The default value, 8, is kinda tuned for
older an= d slower machines than most of us use today.  I wouldn't make it
enormous, because it does prevent exponential planning time gr= owth for
many tables, but you could likely get away with = 15 or so.  You should
likely keep from_collapse_limi= t in sync with that too.

  &= nbsp;         regards, tom lane
------=_Part_2009660_2034538936.1549122810081--