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 1qdzrP-005R3e-Cn for pgsql-general@arkaria.postgresql.org; Wed, 06 Sep 2023 21:14:51 +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 1qdzrN-00DH0Q-8L for pgsql-general@arkaria.postgresql.org; Wed, 06 Sep 2023 21:14:49 +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 1qdzrM-00DGzw-Nc for pgsql-general@lists.postgresql.org; Wed, 06 Sep 2023 21:14:48 +0000 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qdzrE-00385I-PS for pgsql-general@lists.postgresql.org; Wed, 06 Sep 2023 21:14:47 +0000 Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-52bca2e8563so258600a12.2 for ; Wed, 06 Sep 2023 14:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1694034878; x=1694639678; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=cesDuq1Z5F6fG1PshEhd922pArkIP0lus6sujsFa1ZU=; b=enRPlJttIZIJO6y/FO5pUogxYNOlcKDJjM4yz4z2/IFnX/Uxk38SoqyWC+l4Tu2UYv ARGx6WDY5usi6FsQG20wgLfaGlc66T8gAMiXFdeuC52JzTfSKtwqjD2zXJlZdIWnfDLu ogO56QZpRUDSjaZeqpO1LOuGprXxdzWxl5P5tXrU5ww6SXMhp6J0EG6njz8NhiDg8QjQ RGPS0JRwcS0uHCZl8UDSqi+QtLUkFLL5+hfVAXShCvwoV2A8YjUlZXeh88nRk9NUS/18 UiKu0pbBTDwNVoLk/fj0twmapESkWTFRpTHvGlGIMp1Nc1xwsrMx+XpyCBstka9cHIQM B+5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1694034878; x=1694639678; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cesDuq1Z5F6fG1PshEhd922pArkIP0lus6sujsFa1ZU=; b=GR1n4ZrV4hMKdb+cEhTT2SMUKxrqBJui4J3ME4YosvnuaO43W4xJMys9mWRZZWbm95 AC61a5p+V6FqE8hjTJx+sur5CiBe4vYbSGmdHD4trQ5Qb615tMC5zZrMqZ5yIln/saKL hPVXM92LCcPxaF9/dRJ8FmCrhIFUm/39ZiTfeh4DJhM+KMno3ts9qWdaM6rNkKvpheWr G2b7QL+CISNPsLPFUVkT9lNFdVDEgfGVJzkEMSk1pwpTaf2WSHYKz//ba2FfE9P0pcnL PXyQEuXY36fzpf2CTenYva/7NIYK+Ghn+uKXoTfkw/TmKK26L9nBlr5UmQQVZyloAhrt 9LKQ== X-Gm-Message-State: AOJu0Yy7h94K42tefLP4ujRrW7EArCS0byxVQsRhw9bmDqzQM3+T57B+ ePKzlDkDTig/8/GAaLzqeCPxEw== X-Google-Smtp-Source: AGHT+IFI/NxswiLzSV5/42YO6/OqMOJNWsZ2ZPbX6M6HFSHl1brB/ohRmCoSxP45P0vy3MKiu9kzIg== X-Received: by 2002:a05:6402:1ac4:b0:528:90d7:a1f with SMTP id ba4-20020a0564021ac400b0052890d70a1fmr3461917edb.10.1694034878584; Wed, 06 Sep 2023 14:14:38 -0700 (PDT) Received: from localhost.localdomain (62-46-253-73.adsl.highway.telekom.at. [62.46.253.73]) by smtp.gmail.com with ESMTPSA id c70-20020a509fcc000000b0052ea03b9d05sm1196688edf.85.2023.09.06.14.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Sep 2023 14:14:38 -0700 (PDT) Message-ID: Subject: Re: Query performance going from Oracle to Postgres From: Laurenz Albe To: "Dirschel, Steve" , "pgsql-general@lists.postgresql.org" Cc: "Wong, Kam Fook (TR Technology)" Date: Wed, 06 Sep 2023 23:14:37 +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 Wed, 2023-09-06 at 20:06 +0000, Dirschel, Steve wrote: > We are in the process of converting from Oracle to Postgres and I have a = query that is using > the same index in Postgres as is used in Oracle but in Postgres the query= does 16x more > buffer/logical reads.=C2=A0 I=E2=80=99d like to understand why. > =C2=A0 > explain (analyze, buffers) > select count(historyeve0_.HISTORY_EVENT_SID) as col_0_0_ > from hist28.history_event_display_timestamp_20230301 historyeve0_ where h= istoryeve0_.IS_DELETED=3D0 > and historyeve0_.PRISM_GUID=3D'i0accd6a20000018405f095ee669dc5b4' > and historyeve0_.PRODUCT_SID=3D'CARSWELL.WESTLAW' > and (historyeve0_.EVENT_TYPE not in=C2=A0 > ('versionsSearchWithinQueryEvent','notesOfDecisionsSearchWithinQueryEvent= ','citingReferencesSearchWithinQueryEvent','tocSearchWithinQueryEvent','sea= rchWithinDocumentEvent','coCitesSearchWithinQueryE > vent')) > and (historyeve0_.PRODUCT_VIEW in ('DefaultProductView','TNPPlus','PLCUS'= ,'Indigo','IndigoPremium','INDIGOCA') > or historyeve0_.PRODUCT_VIEW is null) > and historyeve0_.CLIENT_ID=3D'WLNCE_VNJXL7' > and (historyeve0_.DISPLAY_TIMESTAMP between '2022-03-01 00:00:00' and '20= 23-03-01 23:59:59.999'); > =C2=A0 > Aggregate=C2=A0 (cost=3D56.64..56.65 rows=3D1 width=3D8) (actual time=3D0= .930..0.931 rows=3D1 loops=3D1) > =C2=A0=C2=A0 Buffers:shared hit=3D341 > =C2=A0=C2=A0 ->=C2=A0 Index Scan using history_event_display_timesta_pris= m_guid_display_timestamp_idx1 on history_event_display_timestamp_20230301 h= istoryeve0_=C2=A0 (cost=3D0.42..56.64 rows=3D1 width=3D33) (actual > time=3D0.034..0.897 rows=3D332 loops=3D1) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Index Cond: (((prism_gui= d)::text =3D 'i0accd6a20000018405f095ee669dc5b4'::text) AND (display_timest= amp >=3D '2022-03-01 00:00:00'::timestamp without time zone) AND (display_t= imestamp <=3D '2023- > 03-01 23:59:59.999'::timestamp without time zone) AND ((product_sid)::tex= t =3D 'CARSWELL.WESTLAW'::text) AND (is_deleted =3D '0'::numeric) AND ((cli= ent_id)::text =3D 'WLNCE_VNJXL7'::text)) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0Filter: (((event_type)::= text <> ALL > ('{versionsSearchWithinQueryEvent,notesOfDecisionsSearchWithinQueryEvent,= citingReferencesSearchWithinQueryEvent,tocSearchWithinQueryEvent,searchWith= inDocumentEvent,coCitesSearchWithinQueryEvent}'::t > ext[])) AND (((product_view)::text =3D ANY ('{DefaultProductView,TNPPlus,= PLCUS,Indigo,IndigoPremium,INDIGOCA}'::text[])) OR (product_view IS NULL))) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Buffers: shared hit=3D34= 1 > Planning: > =C2=A0=C2=A0 Buffers: shared hit=3D6 > Planning Time: 0.266 ms > Execution Time: 0.965 ms > (10 rows) > =C2=A0 > *** 341 logical reads to find 332 rows. > =C2=A0 > Plan hash value: 3361538278 > =C2=A0 > -------------------------------------------------------------------------= --------------------------------- > | Id=C2=A0 | Operation=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 | Name=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 | Starts | = E-Rows | A-Rows |=C2=A0=C2=A0 A-Time=C2=A0=C2=A0 | Buffers | > -------------------------------------------------------------------------= --------------------------------- > |=C2=A0=C2=A0 0 | SELECT STATEMENT=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 1 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 1 |00:00:00.01 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A020 | > |=C2=A0=C2=A0 1 |=C2=A0 SORT AGGREGATE=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 1 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 1 |=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 1 |00:00:00.01 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 20 | > |*=C2=A0 2 |=C2=A0=C2=A0 FILTER=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 1 |=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0332 |00:00:00.01 |=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 20 | > |=C2=A0=C2=A0 3 |=C2=A0=C2=A0=C2=A0 PARTITION RANGE SINGLE|=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 1 |=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 1 |=C2=A0=C2=A0=C2=A0 332 |00:00:00.01 |=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 20 | > |*=C2=A0 4 |=C2=A0=C2=A0=C2=A0=C2=A0 INDEX RANGE SCAN=C2=A0=C2=A0=C2=A0= =C2=A0 |HISTORY_EVENT_IDX02 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 1 |=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 1 |=C2=A0=C2=A0=C2=A0 332 |00:00:00.01 |=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 20 | > -------------------------------------------------------------------------= --------------------------------- > =C2=A0 > Predicate Information (identified by operation id): > --------------------------------------------------- > =C2=A0 > =C2=A0=C2=A0=C2=A02 - filter(TO_TIMESTAMP(:SYS_B_16,:SYS_B_17)<=3DTO_TIME= STAMP(:SYS_B_18,:SYS_B_19)) > =C2=A0=C2=A0 4 - access("HISTORYEVE0_"."PRISM_GUID"=3D:SYS_B_01 AND > =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"HISTORYEVE0_"."DISPLAY_TIMESTAMP">=3DTO_TIMESTAMP(:SYS_B_16,:S= YS_B_17) AND > =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"HISTORYEVE0_"."PRODUCT_SID"=3D:SYS_B_02 AND "HISTORYEVE0_"."IS= _DELETED"=3D:SYS_B_00 AND > =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"HISTORYEVE0_"."CLIENT_ID"=3D:SYS_B_15 AND "HISTORYEVE0_"."DISP= LAY_TIMESTAMP"<=3DTO_TIMESTAMP(:SYS_B_1 > =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 8,:SYS_B_19)) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 filter(("HISTORYEVE0_"."CLIENT_ID"= =3D:SYS_B_15 AND "HISTORYEVE0_"."PRODUCT_SID"=3D:SYS_B_02 > =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 "HISTORYEVE0_"."EVENT_TYPE"<>:SYS_B_03 AND "HISTORYEVE0_"."= EVENT_TYPE"<>:SYS_B_04 AND > =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"HISTORYEVE0_"."EVENT_TYPE"<>:SYS_B_05 AND "HISTORYEVE0_"."EVEN= T_TYPE"<>:SYS_B_06 AND > =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"HISTORYEVE0_"."EVENT_TYPE"<>:SYS_B_07 AND "HISTORYEVE0_"."EVEN= T_TYPE"<>:SYS_B_08 AND > =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"HISTORYEVE0_"."IS_DELETED"=3D:SYS_B_00 AND (INTERNAL_FUNCTION(= "HISTORYEVE0_"."PRODUCT_VIEW") OR > =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"HISTORYEVE0_"."PRODUCT_VIEW" IS NULL))) > =C2=A0 > It finds the same 332 rows but it does only 20 logical reads. In PostgreSQL, the 332 matching rows seem to me stored mostly on different = pages, while they are better clustered in your Oracle table. If it is really very important for you, and the 57 milliseconds for the ind= ex scan is too much, you can reorganize the table with CLUSTER hist28.history_event_display_timestamp_20230301 USING history_even= t_display_timesta_prism_guid_display_timestamp_idx1; That should reduce the number of pages read. Yours, Laurenz Albe