Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qfVWx-00BXYg-5V for pgsql-performance@arkaria.postgresql.org; Mon, 11 Sep 2023 01:15:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qfVWu-00CT2W-50 for pgsql-performance@arkaria.postgresql.org; Mon, 11 Sep 2023 01:15:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qfVWt-00CT2G-Gx for pgsql-performance@lists.postgresql.org; Mon, 11 Sep 2023 01:15:55 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qfVWl-003sGh-IK for pgsql-performance@postgresql.org; Mon, 11 Sep 2023 01:15:53 +0000 Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-9aa2468bdb4so87947766b.0 for ; Sun, 10 Sep 2023 18:15:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1694394945; x=1694999745; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=nw+F/yq4t3VJMh60fZx4U+WYpjXaXimkdaq8Eb26tUc=; b=aHDDal8ZCV0AI3oH99nkEcEmddbxOGSCmJbkrgQouOLEEN5P+MXaWKvhRrj4h9fN5g uv2si6SapjW57IoblQs1lFOtNNRJwXFsPr5EcMUIXYuiNQSJUer5tvvMQDAd+WeBaUGk eGTxbZP2iBxCM1SUKm+hicN3aMzyGmOj8i7cjsxgdfY5w4SnkwdRIm78GTcMUl6XgKhI NPR357SKkeM/p1KMPPGLYmyqOqqR7etfRkLSzngRz61nz6TZrbSGBcK+j8hviE1bxTNz nbj+dAKwemIgCjquoKz6k1zdy0D7aXrU6bqcO8OpwkCf4pRNlx4tuzXBClxU/0gFQmj2 viBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694394945; x=1694999745; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=nw+F/yq4t3VJMh60fZx4U+WYpjXaXimkdaq8Eb26tUc=; b=suw4naqx48ljR/8PPyJN1TBll3BcsRc+nR7/TKWAiEt/IUivvnJ1BRJMBpFWTfB1g0 91Ws5aLDXLMa/W5OP4yq8OBMPtiqzLTsUr5D7HTv7r4sGrWNO5wiFjJrLibY2d9qCwOj tE9PcAEJNdgA94T7LowVRGD9voSewrGDg2rJsTlVLDi1KGahmEFZ9Bz7Ado9d6Aw4lm5 zcOj+SG8nSSKIBhGp8x8DbcHrplVSX21VCH8VpLBKGaDa7/CVVc2XfIwyI2G+Gt2LpMw Z6ikaJSJGDacuCTrcNBpXOzxT0Ufw2tgZWKLyoP3kHWpPjbFWRvA0HHiIZU3ZVgMxtAs xBhg== X-Gm-Message-State: AOJu0YzckESkX7iv8XCVwQZYPWVS+W4QNgsrEKI2zL1Vp7ILmOrk4aWY aAUHiuX1599Bipp3twG9urU78w== X-Google-Smtp-Source: AGHT+IELp5S+yxMsqhDQX+d9k33C4BQz7Zzaah9p1pJsj4MJbyG3V60pX/t7vVJSr3QoVTi9CGxbtw== X-Received: by 2002:a17:907:62a5:b0:99d:f2dc:97e3 with SMTP id nd37-20020a17090762a500b0099df2dc97e3mr8175368ejc.20.1694394944991; Sun, 10 Sep 2023 18:15:44 -0700 (PDT) Received: from localhost.localdomain (62-46-250-68.adsl.highway.telekom.at. [62.46.250.68]) by smtp.gmail.com with ESMTPSA id lz5-20020a170906fb0500b0098e78ff1a87sm4486754ejb.120.2023.09.10.18.15.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Sep 2023 18:15:44 -0700 (PDT) Message-ID: <5ef3acf5733d5a8584b09eb5dd107e59aa87a075.camel@cybertec.at> Subject: Re: Planning time is time-consuming From: Laurenz Albe To: Mikhail Balayan , pgsql-performance@postgresql.org Date: Mon, 11 Sep 2023 03:15:43 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.48.4 (3.48.4-1.fc38) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2023-09-08 at 18:51 +0800, Mikhail Balayan wrote: > I have three tables: > =C2=A0 =C2=A0 - test_db_bench_1 > =C2=A0 =C2=A0 - test_db_bench_tenants > =C2=A0 =C2=A0 - test_db_bench_tenant_closure >=20 > And the query to join them: > SELECT "test_db_bench_1"."id" id, "test_db_bench_1"."tenant_id" > =C2=A0 FROM "test_db_bench_1" > =C2=A0 JOIN "test_db_bench_tenants" AS "tenants_child" ON (("tenants_chil= d"."uuid" =3D "test_db_bench_1"."tenant_id")=20 > =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=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0AND ("tenants_child"."is_deleted" !=3D true)) > =C2=A0 JOIN "test_db_bench_tenant_closure" AS "tenants_closure" ON (("ten= ants_closure"."child_id" =3D "tenants_child"."id") > =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=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 AND ("tenants_closu= re"."barrier" <=3D 0)) > =C2=A0 JOIN "test_db_bench_tenants" AS "tenants_parent" ON (("tenants_par= ent"."id" =3D "tenants_closure"."parent_id") > =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=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 AND ("tenants_parent"."uuid" IN ('4c79c1c5-21ae= -45a0-8734-75d67abd0330')) > =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=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 AND ("tenants_parent"."is_deleted" !=3D true)) > =C2=A0LIMIT 1 >=20 >=20 > With following execution plan: >=20 > =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=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=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=A0QUERY PLAN > -------------------------------------------------------------------------= ---------------------------------------------------------------------------= -------------------------------------------------- > --------------- > =C2=A0Limit =C2=A0(cost=3D1.56..1.92 rows=3D1 width=3D44) (actual time=3D= 0.010..0.011 rows=3D0 loops=3D1) > =C2=A0 =C2=A0-> =C2=A0Nested Loop =C2=A0(cost=3D1.56..162.42 rows=3D438 w= idth=3D44) (actual time=3D0.009..0.009 rows=3D0 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-> =C2=A0Nested Loop =C2=A0(cost=3D1.13= ..50.27 rows=3D7 width=3D36) (actual time=3D0.008..0.009 rows=3D0 loops=3D1= ) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-> =C2=A0Nested Lo= op =C2=A0(cost=3D0.84..48.09 rows=3D7 width=3D8) (actual time=3D0.008..0.00= 9 rows=3D0 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0-> =C2=A0Index Scan using test_db_bench_tenants_uuid on test_db_bench_te= nants tenants_parent =C2=A0(cost=3D0.41..2.63 rows=3D1 width=3D8) (actual t= ime=3D0.008..0.008 rows=3D0 loops=3D1) > =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: ((uuid)::text =3D '4c79c1c5-21ae-45a0-8= 734-75d67abd0330'::text) > =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: (NOT is_deleted) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0-> =C2=A0Index Scan using test_db_bench_tenant_closure_pkey on test_db_b= ench_tenant_closure tenants_closure =C2=A0(cost=3D0.42..45.06 rows=3D40 wid= th=3D16) (never executed) > =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: (parent_id =3D tenants_parent.id) > =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: (barrier <=3D 0) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-> =C2=A0Index Sca= n using test_db_bench_tenants_pkey on test_db_bench_tenants tenants_child = =C2=A0(cost=3D0.29..0.31 rows=3D1 width=3D44) (never executed) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0Index Cond: (id =3D tenants_closure.child_id) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0Filter: (NOT is_deleted) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-> =C2=A0Index Scan using test_db_bench= _1_idx_tenant_id_3 on acronis_db_bench_heavy =C2=A0(cost=3D0.43..14.66 rows= =3D136 width=3D44) (never executed) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Index Cond: ((tena= nt_id)::text =3D (tenants_child.uuid)::text) > =C2=A0Planning Time: 0.732 ms > =C2=A0Execution Time: 0.039 ms >=20 >=20 > Where the planning time gets in the way as it takes an order of magnitude= more time than the actual execution. >=20 > Is there a possibility to reduce this time? And, in general, to understan= d why planning takes so much time. You could try to VACUUM the involved tables; indexes with many entries poin= ting to dead tuples can cause a long planing time. Also, there are quite a lot of indexes on "test_db_bench_1". On a test dat= abase, drop some indexes and see if that makes a difference. Finally, check if "default_statistics_target" is set to a high value, or if= the "Stats target" for some column in the "\d+ tablename" output is set higher than 100. Yours, Laurenz Albe