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 1oNVUH-0007Vd-NT for pgsql-general@arkaria.postgresql.org; Mon, 15 Aug 2022 08:30:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oNVUE-0003u5-TZ for pgsql-general@arkaria.postgresql.org; Mon, 15 Aug 2022 08:30:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oNVUE-0003tw-Eq for pgsql-general@lists.postgresql.org; Mon, 15 Aug 2022 08:30:14 +0000 Received: from mx2.intershop.de ([195.110.60.62]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oNVUB-0003MK-Ec for pgsql-general@lists.postgresql.org; Mon, 15 Aug 2022 08:30:13 +0000 Received: from mymail.intershop.de (jenmbs02.ad.intershop.net [10.0.87.122]) by mx2.intershop.de (Postfix) with ESMTPS id B965260636 for ; Mon, 15 Aug 2022 10:30:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intershop.de; s=2017020601; t=1660552208; bh=34s2/dfwdaqubZIFQuQ16KQl0BHzm5LPvyt66xSsuPk=; h=From:To:Subject:Date:From; b=NvRqlNXMTYWj2C5CbXtxIBOGtN0ZdBUbqvTKOKqL7581a1+5UFeGf6fLWeCZWysYD 9wF/o1gEeytAMUKL8mKwiWAiMN/PsnBi/PgprVu0DdRre7+H5dphWOLYC8lKjSQ46i FYEYLrYakTzLZlcjEiZbOJM1qcI6H6ktnMzvikxHIflp5/feHTjDyCc9U25QW6vfum 1MZfQRo3/r7Gz8e2NwRsXP76lJVcU0tkUFRadGg5RMn1Y5GitSGB2lSBfZlKydCU+x ioCMl1lzVozRiQwXWOg0zcpgqGWp4XEGGk0jiWW0fdKqPPEjyFEnvEAfIyJxen+9tC BbdQo6UHDo5McZQwfQ7KAigNw6YEZpMcal8/zgE8X8zkYd6qqaER4xgSkuWgAbBgxj kAiYjhRQLEozX14lSzt/xJEa0QDlMHd13XK+rMvBcW47dYY5QdbQiYbSS2907GO601 mF0vPRHXv8lLV6f2dTHueg0lqHep+q3tT7m5AhMPSEWBorxEuiBwsSfSlXxgZh+X5n koMgrzHRXxM+gLKGjpsU5WuN+kqH7cz8wL/4jU97tpMH+jUVuGtw/TJK9L1L3F7rjR 80EOA2wyHFFBOc38oydHp1m7IDZxBzA0OkXZOB1EkyW/f5DfRtyBEX+1EqTB6MiMgB 2RBtRugC65e0oREUtQi5q8UY= Received: from JENEX19-1.ad.intershop.net (10.0.87.121) by JENEX19-2.ad.intershop.net (10.0.87.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.9; Mon, 15 Aug 2022 10:30:08 +0200 Received: from JENEX19-1.ad.intershop.net ([fe80::fca3:adda:19c9:b9ff]) by JENEX19-1.ad.intershop.net ([fe80::fca3:adda:19c9:b9ff%13]) with mapi id 15.02.1118.009; Mon, 15 Aug 2022 10:30:08 +0200 From: Marc Mamin To: "pgsql-general@lists.postgresql.org" Subject: limits, indexes, views and query planner Thread-Topic: limits, indexes, views and query planner Thread-Index: AdiwgRsoBznBXHZcSRyHOSAF/cEyhw== Date: Mon, 15 Aug 2022 08:30:08 +0000 Message-ID: Accept-Language: de-DE, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [195.110.60.24] Content-Type: multipart/alternative; boundary="_000_c7e760258c544fb59591c0eea61de3c8intershopde_" MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.103.6 at mx2.intershop.de X-Virus-Status: Clean List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --_000_c7e760258c544fb59591c0eea61de3c8intershopde_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable hello, in the example below, we can see that the view test_ab prevents the usage o= f the index to retrieve the top last rows. This is a special case, as the where clause excludes data from the second t= able, and the explain output do not references it at all. I wonder if the planner could be able to exclude the table_b earlier in its= plan and to possibly fallback to a plan equivalent to the first one. with a view on a single table (test_av), the index is used. An oddity in the plan is the expected row count in "Append"(100001) ... ( tested on postgres 14 ) Regards, Marc Mamin create temp table table_a as (select * from generate_series(1,100000)x); create temp table table_b as (select * from generate_series(1,100000)x); create index i_a on table_a (x); create index i_b on table_b (x); analyze table_a; analyze table_b; CREATE VIEW test_ab AS select 'a' as src, x from table_a UNION select 'b' as src, x from table_b ; explain analyze select * from table_a order by x desc limit 10; Limit (cost=3D0.29..0.60 rows=3D10 width=3D4) (actual time= =3D0.056..0.060 rows=3D10 loops=3D1) -> Index Only Scan Backward using i_a on table_a (cost=3D0= .29..3050.29 rows=3D100000 width=3D4) (actual time=3D0.055..0.058 rows=3D10= loops=3D1) Heap Fetches: 10 explain analyze select * from test_ab where src=3D'a' order by x desc limit= 10; Limit (cost=3D17895.92..17895.94 rows=3D10 width=3D36) (actual time=3D89.6= 78..89.681 rows=3D10 loops=3D1) -> Sort (cost=3D17895.92..18145.92 rows=3D100001 width=3D36) (actual ti= me=3D89.677..89.679 rows=3D10 loops=3D1) Sort Key: table_a.x DESC Sort Method: top-N heapsort Memory: 25kB -> Unique (cost=3D13984.92..14734.92 rows=3D100001 width=3D36) (a= ctual time=3D47.684..75.574 rows=3D100000 loops=3D1) -> Sort (cost=3D13984.92..14234.92 rows=3D100001 width=3D36= ) (actual time=3D47.682..60.869 rows=3D100000 loops=3D1) Sort Key: ('a'::text), table_a.x Sort Method: external merge Disk: 1768kB -> Append (cost=3D0.00..2943.01 rows=3D100001 width= =3D36) (actual time=3D0.012..21.268 rows=3D100000 loops=3D1) -> Seq Scan on table_a (cost=3D0.00..1443.00 ro= ws=3D100000 width=3D36) (actual time=3D0.011..14.078 rows=3D100000 loops=3D= 1) -> Result (cost=3D0.00..0.00 rows=3D0 width=3D3= 6) (actual time=3D0.001..0.002 rows=3D0 loops=3D1) One-Time Filter: false Planning Time: 0.107 ms Execution Time: 90.139 ms CREATE VIEW test_av AS select 'a' as src, x from table_a; explain analyze select * from test_av order by x desc limit 10; -> Index Only Scan Backward using i_a on table_a (cost=3D0.29..3= 050.29 rows=3D100000 width=3D36) (actual time=3D0.017..0.019 rows=3D10 loop= s=3D1) --_000_c7e760258c544fb59591c0eea61de3c8intershopde_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

 

hello,

in the example below, we can se= e that the view test_ab prevents the usage of the index to retrieve the top= last rows.

This is a special case, as the = where clause excludes data from the second table, and the explain output do= not references it at all.

I wonder if the planner could b= e able to exclude the table_b earlier in its plan and to possibly fallback = to a plan equivalent to the first one.

with a view on a single table (= test_av), the index is used.

 

An oddity in the plan is the ex= pected row count in "Append"(100001) ...

 

( tested on postgres 14 )<= /o:p>

 

Regards,
Marc Mamin

 

 

create temp table table_a as (s= elect * from generate_series(1,100000)x);

create temp table table_b as (s= elect * from generate_series(1,100000)x);

create index i_a on table_a (x)= ;

create index i_b on table_b (x)= ;

 

analyze table_a;

analyze table_b;

 

CREATE VIEW test_ab AS

  <= /p>

  select 'a' as src, = x from table_a

  UNION<= /p>

  select 'b' as src, x fro= m table_b

;

 

 

explain analyze select * from t= able_a order by x desc limit 10;

     &= nbsp;          Limit  (co= st=3D0.29..0.60 rows=3D10 width=3D4) (actual time=3D0.056..0.060 rows=3D10 = loops=3D1)

     &= nbsp;         ->  Index Onl= y Scan Backward using i_a on table_a  (cost=3D0.29..3050.29 rows=3D100= 000 width=3D4) (actual time=3D0.055..0.058 rows=3D10 loops=3D1)<= /span>

     &= nbsp;  Heap Fetches: 10

     &= nbsp; 

     &= nbsp;  

explain analyze select * from t= est_ab where src=3D'a' order by x desc limit 10;

 

Limit  (cost=3D17895.92..1= 7895.94 rows=3D10 width=3D36) (actual time=3D89.678..89.681 rows=3D10 loops= =3D1)

  ->  Sort  (= cost=3D17895.92..18145.92 rows=3D100001 width=3D36) (actual time=3D89.677..= 89.679 rows=3D10 loops=3D1)

     &= nbsp;  Sort Key: table_a.x DESC

     &= nbsp;  Sort Method: top-N heapsort  Memory: 25kB

     &= nbsp;  ->  Unique  (cost=3D13984.92..14734.92 rows=3D1000= 01 width=3D36) (actual time=3D47.684..75.574 rows=3D100000 loops=3D1)<= /o:p>

     &= nbsp;        ->  Sort  (cos= t=3D13984.92..14234.92 rows=3D100001 width=3D36) (actual time=3D47.682..60.= 869 rows=3D100000 loops=3D1)

     &= nbsp;           &nbs= p;  Sort Key: ('a'::text), table_a.x

     &= nbsp;           &nbs= p;  Sort Method: external merge  Disk: 1768kB

     &= nbsp;           &nbs= p;  ->  Append  (cost=3D0.00..2943.01 rows=3D100001 width= =3D36) (actual time=3D0.012..21.268 rows=3D100000 loops=3D1)

     &= nbsp;           &nbs= p;        ->  Seq Scan on table_= a  (cost=3D0.00..1443.00 rows=3D100000 width=3D36) (actual time=3D0.01= 1..14.078 rows=3D100000 loops=3D1)

     &= nbsp;            &nb= sp;       ->  Result  (cost= =3D0.00..0.00 rows=3D0 width=3D36) (actual time=3D0.001..0.002 rows=3D0 loo= ps=3D1)

     &= nbsp;           &nbs= p;            &= nbsp; One-Time Filter: false

Planning Time: 0.107 ms

Execution Time: 90.139 ms =  

 

CREATE VIEW test_av AS

  select 'a' as src, = x from table_a;

 

explain analyze select * from t= est_av order by x desc limit 10;

     &= nbsp;   ->  Index Only Scan Backward using i_a on table_a=   (cost=3D0.29..3050.29 rows=3D100000 width=3D36) (actual time=3D0.017= ..0.019 rows=3D10 loops=3D1)

 

--_000_c7e760258c544fb59591c0eea61de3c8intershopde_--