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 1oxnTl-0001aQ-A5 for pgsql-general@arkaria.postgresql.org; Wed, 23 Nov 2022 10:59:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oxnTi-000597-EJ for pgsql-general@arkaria.postgresql.org; Wed, 23 Nov 2022 10:59:42 +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 1oxnTi-00058x-09 for pgsql-general@lists.postgresql.org; Wed, 23 Nov 2022 10:59:42 +0000 Received: from smtp-outgoing-1902.laposte.net ([160.92.124.106]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oxnTf-0005aK-LU for pgsql-general@postgresql.org; Wed, 23 Nov 2022 10:59:41 +0000 X-mail-filterd: {"version":"1.5.1","queueID":"4NHJ6d0cQDz1qqhq","contextId":"60e56ffd-7187-4c1c-a5de-564378466dea"} Received: from outgoing-mail.laposte.net (localhost.localdomain [127.0.0.1]) by mlpnf0119.laposte.net (SMTP Server) with ESMTP id 4NHJ6d0cQDz1qqhq for ; Wed, 23 Nov 2022 11:59:37 +0100 (CET) X-mail-filterd: {"version":"1.5.1","queueID":"4NHJ6d0BKFz1qqhn","contextId":"65ebc786-8335-4f00-b1bf-43ec9d595255"} X-lpn-mailing: LEGIT X-lpn-spamrating: 40 X-lpn-spamlevel: not-spam Received: from wlpnf0212.sys.meshcore.net (wlpnf0212.sys.meshcore.net [81.80.43.240]) by mlpnf0119.laposte.net (SMTP Server) with ESMTPA id 4NHJ6d0BKFz1qqhn for ; Wed, 23 Nov 2022 11:59:36 +0100 (CET) Date: Wed, 23 Nov 2022 11:59:36 +0100 (CET) From: benj.dev@laposte.net Reply-To: benj.dev@laposte.net To: Pgsql-general Message-ID: <846261823.2789764.1669201176926@wlpnf0212> Subject: Planner choose to use big index instead of smaller one MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2789763_698439300.1669201176926" X-WUM-THEME: 0 X-Message-Size: X-National-Code: Message-Context: email-message X-SAVECOPY: true X-Country-Code: X-Cache-Entry: X-Wum-ChannelType: X-Cache-ID: X-ORIGINATING-IP: 81.80.43.240 X-Wum-Nature: EMAIL-NATURE X-WUM-FROM: |~| X-WUM-TO: |~| X-WUM-REPLYTO: |~| DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=laposte.net; s=lpn-wlmd; t=1669201178; bh=zCBpCBdbbauVRT//S9lo6XiTJsZQHLfEwBAGPfiY2tc=; h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version:Content-Type; b=KEw+HejZuDtOZH4uyvD9VnkTA6QLebvsIEQyoRx4BharZF6PUBNTqFB5qQhMVsLFes9l+F2Qb5utcWSsWC0Jp4C1Rq+HQKske7R94DqIzzTabHUy/Pj7C/zF7MbW4F3vdmro+6XQLdJ6oBuso5Nt3lE5cYdCjc3UWNXb/QozJWzYXzC2kVGFX79G2Pz2Ml3tTDIByy+IEt+fAEPxXuTPGDHHJ7+dl9pMCJdzPJXXpACbJfhWIpwNXoLNjZlhBgxdw5TafdCutZnv9yW7ZZE4BHhSDAapTG8BvTN/01CgGs/FO0Z9c6/xZYXzaXZxvhN0EkJmI4Mf/MBc3c27ItmEGw==; List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------=_Part_2789763_698439300.1669201176926 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, I have a table tpoint near that 820Mo with 700K lignes I have created 2 index on this table : CREATE INDEX idx_small_index ON public.tpoint USING btree (match_id); -- Size : 4560 kB CREATE INDEX idx_big_index=C2=A0=C2=A0 ON public.tpoint USING btree (versio= n, match_id, playnum, code_action, num_balle ); -- Size : 34 MB If I execute this request =C2=A0 EXPLAIN(analyse, buffers) SELECT count(*) FROM tpoint WHERE match_id =3D 'SM001' The query planner use the idx_small_index as expected =C2=A0 ->=C2=A0 Index Only Scan using idx_small_index on tpoint=C2=A0 (cost= =3D0.42..507.10 rows=3D624 width=3D0) (actual time=3D0.025..0.160 rows=3D10= 17 loops=3D1) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Index Cond: (match_id =3D 'SM001= '::bpchar) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Heap Fetches: 199 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Buffers: shared hit=3D45 =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2= =A0 But if I execute this other request EXPLAIN(analyse, buffers) SELECT count(*) FROM tpoint WHERE match_id LIKE 'SM001%' The query planner use idx_big_index =C2=A0 ->=C2=A0 Index Only Scan using idx_big_index on tpoint=C2=A0 (cost= =3D0.42..73940.37 rows=3D5191 width=3D0) (actual time=3D111.014..143.379 ro= ws=3D1017 loops=3D1) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Filter: (match_id ~~ 'SM001%'::t= ext) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Rows Removed by Filter: 636819 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Heap Fetches: 132426 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Buffers: shared hit=3D473963 =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2= =A0 I really don't understand why the planner prefers to use the=C2=A0 "big ind= ex" instead of the "small" one that containt all needed element to do an IN= DEX ONLY SCAN. =C2=A0 If I deactivate the "big index" UPDATE pg_index SET indisvalid =3D false WHERE indexrelid =3D 'idx_big_inde= x'::regclass =C2=A0 I can see that for a first explain : =C2=A0=C2=A0 ->=C2=A0 Index Only Scan using idx_small_index on tpoint=C2=A0= (cost=3D0.42..92107.62 rows=3D5191 width=3D0) (actual time=3D59.980..78.68= 3 rows=3D1017 loops=3D1) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Filter: (match_id ~~ 'SM001%'::t= ext) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Rows Removed by Filter: 636819 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Heap Fetches: 132426 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Buffers: shared hit=3D27668 read= =3D564 =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2= =A0 or that for a second explain (No read):=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 Index Only Scan using idx_small_index on tpoint=C2=A0 (cost= =3D0.42..92107.62 rows=3D5191 width=3D0) (actual time=3D64.012..81.122 rows= =3D1017 loops=3D1) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Filter: (match_id ~~ 'SM001%'::t= ext) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Rows Removed by Filter: 636819 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Heap Fetches: 132426 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Buffers: shared hit=3D28232 Tests realized on a docker postgres version =3D PostgreSQL 15.0 (Debian 15.0-1.pgdg110+1) on x86_64-pc-linux-gn= u, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit SET max_parallel_workers_per_gather TO 0 Thanks for any explanation=E2=80=8C regards =E2=80=8C ------=_Part_2789763_698439300.1669201176926 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi,

I have a table tpoint near that 820Mo with 700K lignes
I have created 2 index on this table :

CREATE INDEX idx_small_index ON public.tpoint USING btree (match_id);
-- Size : 4560 kB

CREATE INDEX idx_big_index   ON public.tpoint USING btree (versio= n, match_id, playnum, code_action, num_balle );
-- Size : 34 MB

If I execute this request
 
EXPLAIN(analyse, buffers)
SELECT count(*) FROM tpoint
WHERE match_id =3D 'SM001'

The query planner use the idx_small_index as expected

  ->  Index Only Scan using idx_small_index on tpoint  (c= ost=3D0.42..507.10 rows=3D624 width=3D0) (actual time=3D0.025..0.160 rows= =3D1017 loops=3D1)
        Index Cond: (match_id =3D 'SM001= '::bpchar)
        Heap Fetches: 199
        Buffers: shared hit=3D45
                = ;
But if I execute this other request

EXPLAIN(analyse, buffers)
SELECT count(*) FROM tpoint
WHERE match_id LIKE 'SM001%'

The query planner use idx_big_index

  ->  Index Only Scan using idx_big_index on tpoint  (cos= t=3D0.42..73940.37 rows=3D5191 width=3D0) (actual time=3D111.014..143.379 r= ows=3D1017 loops=3D1)
        Filter: (match_id ~~ 'SM001%'::t= ext)
        Rows Removed by Filter: 636819         Heap Fetches: 132426
        Buffers: shared hit=3D473963
                = ;
I really don't understand why the planner prefers to use the  "big ind= ex" instead of the "small" one that containt all needed element to do an IN= DEX ONLY SCAN.
 
If I deactivate the "big index"
UPDATE pg_index SET indisvalid =3D false WHERE indexrelid =3D 'idx_big_inde= x'::regclass
 
I can see that for a first explain :
   ->  Index Only Scan using idx_small_index on tpoint&nb= sp; (cost=3D0.42..92107.62 rows=3D5191 width=3D0) (actual time=3D59.980..78= .683 rows=3D1017 loops=3D1)
        Filter: (match_id ~~ 'SM001%'::t= ext)
        Rows Removed by Filter: 636819         Heap Fetches: 132426
        Buffers: shared hit=3D27668 read= =3D564
                = ;
or that for a second explain (No read):       &nbs= p;        
  ->  Index Only Scan using idx_small_index on tpoint  (c= ost=3D0.42..92107.62 rows=3D5191 width=3D0) (actual time=3D64.012..81.122 r= ows=3D1017 loops=3D1)
        Filter: (match_id ~~ 'SM001%'::t= ext)
        Rows Removed by Filter: 636819         Heap Fetches: 132426
        Buffers: shared hit=3D28232

Tests realized on a docker postgres
version =3D PostgreSQL 15.0 (Debian 15.0-1.pgdg110+1) on x86_64-pc-linux-gn= u, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
SET max_parallel_workers_per_gather TO 0

Thanks for any explanation=E2=80=8C
regards
=E2=80=8C
------=_Part_2789763_698439300.1669201176926--