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 1nC3Wh-00015u-GF for pgsql-performance@arkaria.postgresql.org; Mon, 24 Jan 2022 17:53:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nC3Wg-0004WI-FY for pgsql-performance@arkaria.postgresql.org; Mon, 24 Jan 2022 17:53:10 +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 1nC3Wg-0004W9-5m for pgsql-performance@lists.postgresql.org; Mon, 24 Jan 2022 17:53:10 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nC3Wd-00020s-K1 for pgsql-performance@postgresql.org; Mon, 24 Jan 2022 17:53:09 +0000 Received: by mail-pl1-x62d.google.com with SMTP id u11so16328639plh.13 for ; Mon, 24 Jan 2022 09:53:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=entrata.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VQo/Un3sNZ14uUBWOc5T9ckgJUtBN1Mw7no69oRzGY8=; b=ec/wc2sFlf4klxfVLS8ZxBwEiLTUjmIpF7esxa9QkeHXXzim5+o6tTMiE8gkUnYuCK JG6kdhQDmBT0m3BPYcKuyCpsG+WreXaZWVnLsT1OHuCVugsL1ek1O23+xtdMXNNjofqP XQIF2FSU2r/ojlR1n4cJCCT8X26zl2n2RL5Z+f/JGOVhfASy+POErqZtEzSt1C8OiCiN 17qED0pZ99tulxN9XaiefDz/KyHFR8/yXT6RuP26rWIdf/LRsbROynOvL/1iMywRpy53 X/iQydaiH8NnSjiomsJxZUFQOgymb9FM99j3lWrR2PIzTOQgP0ytMHK53fzHdbpaA9Ux caGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VQo/Un3sNZ14uUBWOc5T9ckgJUtBN1Mw7no69oRzGY8=; b=Iwf1ia8/fqG4mpqVsE1sckhkbrpUeqsW2H0p/RCECZ7Bp2L+LXppcqvM4TjSdijyNv 5Uf5hY6MxsaLSnBTYm/MFTrTs4+mNvCC0UZ8kSiGLwwG6tAzN9j47u0JphMCbuq/YO5S q8fNRy7EI4lWxsoPzbKJVjN9JPNRZns3LhFQSOuKvQnmNzWdn1PqF4+efsFhkhR8oX84 2PlRsTUo97BL0+oPpNVMFeaibLIoul5wPfSNwZ+TEUrt2e1QYJxdArEmQaJEfe8hVoYH ZU+0jzXLm4OTCBcnOycNvr/eC3g+GsGA8nuIDBYC8CGu0EgCC8PGslOaCaRa2IBneTV6 WbGA== X-Gm-Message-State: AOAM530VeavEBugie8DMgLCcuTADyMokiUk3kFsdvmEPG93RNBrGbNn3 oNNFk4uvGVqNPYdMC2GSs8DA+8nCvDkyEh6IxQRs/Q== X-Google-Smtp-Source: ABdhPJxczlpDjYLlUo9oMU9vQAezIwds8yQgm81OkusfGbLIPsc39/48KJZgG8rgpHZYQU5BpRsYFbPrm8Lk5v4s0qc= X-Received: by 2002:a17:902:e80b:b0:14b:4648:f6d7 with SMTP id u11-20020a170902e80b00b0014b4648f6d7mr7328687plg.124.1643046786586; Mon, 24 Jan 2022 09:53:06 -0800 (PST) MIME-Version: 1.0 References: <003801d80ebb$54ac97a0$fe05c6e0$@gmail.com> In-Reply-To: From: Michael Lewis Date: Mon, 24 Jan 2022 10:52:40 -0700 Message-ID: Subject: Re: Slow query fixed by replacing equality with a nested query To: Valentin Janeiko Cc: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary="00000000000010735905d657a4fc" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000010735905d657a4fc Content-Type: text/plain; charset="UTF-8" On Mon, Jan 24, 2022 at 6:22 AM Valentin Janeiko wrote: > I have rewritten the query using JOINs. I had to make one of them a > FULL JOIN, but otherwise JOINs seem like a good idea. > I have added the new query to the (same) gist: > > https://gist.github.com/valeneiko/89f8cbe26db7ca2651b47524462b5d18#file-queryoptimized-sql > The query plan is much better with just a few small index scans which > completes in under a millisecond: https://explain.depesz.com/s/vBdG Glad to hear it, but as best as I can figure, that right join is actually an inner join because of the where clause meaning that cte2Source must not be null and therefore cte2.resource_surrogate_id must not be null. *RIGHT* JOIN fhir.reference_search_param AS cte2 ON cte2.is_history = false AND cte2.search_param_id = 561 AND cte2.resource_type_id IN (42) AND cte2.reference_resource_type_id = r.resource_type_id AND cte2.reference_resource_id_hash = r.resource_id_hash INNER JOIN fhir.resource AS cte2Source ON cte2Source.is_history = false AND cte2Source.resource_type_id IN (42) * AND cte2Source.resource_surrogate_id = cte2.resource_surrogate_id* WHERE cte1.start_date_time <= '2022-01-12 12:13:21.969000Z' AND r.resource_type_id IN (10, 52, 95, 119, 60) * AND cte2Source.resource_id_hash IN ('df26ca5a-d2e2-1576-2507-815d8e73f15e'::uuid)* --00000000000010735905d657a4fc Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Mon, Jan 24, 2022 at 6:22 AM Valentin = Janeiko <val.janeiko@gmail.com<= /a>> wrote:
I have rewritten the query using JOINs. I had to = make one of them a
FULL JOIN, but otherwise JOINs seem like a good idea.
I have added the new query to the (same) gist:
https://gi= st.github.com/valeneiko/89f8cbe26db7ca2651b47524462b5d18#file-queryoptimize= d-sql
The query plan is much better with just a few small index scans which
completes in under a millisecond: https://explain.depesz.com/s/vBdG=

Glad to hear it, but as best as I can = figure, that right join is actually an inner join because of the where clau= se meaning that=C2=A0cte2Source must not be null and therefore cte2.resourc= e_surrogate_id must not be null.

RIGHT JOIN fhir.reference_se= arch_param AS cte2 ON
cte2.is_history =3D false
AND cte2.search_par= am_id =3D 561
AND cte2.resource_type_id IN (42)
AND cte2.reference_= resource_type_id =3D r.resource_type_id
AND cte2.reference_resource_id_= hash =3D r.resource_id_hash

INNER JOIN fhir.resource AS cte2S= ource ON
=C2=A0 =C2=A0cte2Source.is_history =3D false
=C2=A0 =C2=A0AN= D cte2Source.resource_type_id IN (42)
=C2=A0 =C2=A0AND cte2Source.res= ource_surrogate_id =3D cte2.resource_surrogate_id

WHE= RE cte1.start_date_time <=3D '2022-01-12 12:13:21.969000Z'
A= ND r.resource_type_id IN (10, 52, 95, 119, 60)
AND cte2Source.resour= ce_id_hash IN ('df26ca5a-d2e2-1576-2507-815d8e73f15e'::uuid)
--00000000000010735905d657a4fc--