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 1qCgIi-0002rK-GU for pgsql-general@arkaria.postgresql.org; Fri, 23 Jun 2023 12:54:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qCgHi-00005C-O8 for pgsql-general@arkaria.postgresql.org; Fri, 23 Jun 2023 12:53:06 +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 1qCgHi-000051-7d for pgsql-general@lists.postgresql.org; Fri, 23 Jun 2023 12:53:06 +0000 Received: from mail-lf1-x136.google.com ([2a00:1450:4864:20::136]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qCgHc-0000qB-Dy for pgsql-general@lists.postgresql.org; Fri, 23 Jun 2023 12:53:05 +0000 Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-4f122ff663eso737386e87.2 for ; Fri, 23 Jun 2023 05:53:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20221208.gappssmtp.com; s=20221208; t=1687524779; x=1690116779; 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=jL4Vgu8wY1WZNTlrBo50ECK/ZDoJMWf+lc3XUpyMp0Y=; b=KfEuHV6PWxP1ERF3Vg4yRJfAJhN5uZKhQGtQotUXvTmB8k9OqP7ctGow3XHPLkt/nA 3TEA4ppMsVFZ4lcXZsEkmISgx/6ghtRgSgPcWsudrWwT34LlheW1XzDVQILZsDE2NsW5 1dSBlsXI66TTZBY6bdw8IWHIQPrdGEzG3rvinV8xyO599ff4qp1Sp4LOFuWEubFTA5P5 881WG6loZFoziPsy2sWbzEQWFwzfOkf4Hn5GvvGLw2maTQeOj4PVlIBDMpTwGviwrsYm L9KAz0J+QPoAD5spaXnA0qFdJ0Xb1p04646twzw+bBQisG/lLah5FGCljnrSi6SHiLGL qPjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687524779; x=1690116779; 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=jL4Vgu8wY1WZNTlrBo50ECK/ZDoJMWf+lc3XUpyMp0Y=; b=aIiOsJIXyP3iDUpNGUOXt+SDkDhUyOs3GklvKWzINoZeoB40irNmhvPGMf3Tq+k8Ks Hd+V3msHKB4EBkxdJYID/jW7menhSoHa5AjiLOOlmJD8s8MT/73SiMj32IMNWKBy59ov jEdXWQ2IR8M8oGz9EqcD7FwVYdRKCtX5vvXAw8n3VxuCjq+U/d0eGXh2smHVWm/ZPB+J RpT2GxN13XSVxJ8GKQlnfq8BjufPhTCzWeC/tN6oS7Qtb2dkAeDOlvFdorZhDgUmXr4Q q49Azujfcc65TiUhk/ST2gyx4LOnWdKJ8GkFI7eQ99/Q+q1CqOlAqt4ViZjHf8XNGp5A +vVQ== X-Gm-Message-State: AC+VfDw7yzz930N5+Z+QDCAOnXimWpf+i5a0Dr2Z18QfgDkRKXC0gC8y 3G5hxj0sJYZGAj7EKj9GXZye5Q== X-Google-Smtp-Source: ACHHUZ5996R4NXZiS1MJD9pA6UnSn7WFbGZ5/wgazDiXtAOg8XhNiE6zjloBAi142uCVV6Plkfv9iw== X-Received: by 2002:a19:f24a:0:b0:4f7:940f:913 with SMTP id d10-20020a19f24a000000b004f7940f0913mr12080178lfk.50.1687524779221; Fri, 23 Jun 2023 05:52:59 -0700 (PDT) Received: from localhost.localdomain (62-46-254-185.adsl.highway.telekom.at. [62.46.254.185]) by smtp.gmail.com with ESMTPSA id n12-20020a1c720c000000b003f90b58df65sm2251441wmc.29.2023.06.23.05.52.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 05:52:58 -0700 (PDT) Message-ID: <15daa64906d5dcf840e25af2121a21b9f151aa5d.camel@cybertec.at> Subject: Re: plan using BTree VS GIN From: Laurenz Albe To: Nicolas Seinlet , "pgsql-general@lists.postgresql.org" Date: Fri, 23 Jun 2023 14:52:57 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4 (3.46.4-1.fc37) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2023-06-23 at 12:08 +0000, Nicolas Seinlet wrote: > we faced an issue with a select query on a relatively large table on our = database. > The query involves one single table. The table has more than 10 million r= ecords. > It's mainly composed of varchar fields, have a primary key (id) of type s= erial, > and when records of this table are shown to users, they are sorted users = 2 fields, > display_name (varchar) and id (the primary key). Because this table is he= avily used > in various contexts in our application, we have multiple indexes on it. A= mong other > index, we have gin index on some fields of the table. >=20 > The btree index res_partner_displayname_id_idx have been added lately and= perfectly > match a criteria (where active) and sorting (display_name, id) we have in= quite all > our queries on this table. >=20 > The query that cause the issue is this one: > SELECT "res_partner"."id" > =C2=A0 FROM "res_partner" > =C2=A0WHERE (("res_partner"."active" =3D true) 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 =C2=A0 =C2=A0( > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0((unaccent("res_partner".= "display_name"::text) ilike unaccent('%nse%')) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0OR (unaccent("res_partner"."emai= l"::text) ilike unaccent('%nse%'))) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 OR (unaccent("res_partner"."ref"::text) ilike= unaccent('%nse)%'))) > =C2=A0 =C2=A0 =C2=A0OR (unaccent("res_partner"."vat"::text) ilike unaccen= t('%nse%'))) > =C2=A0 =C2=A0OR (unaccent("res_partner"."company_registry"::text) ilike u= naccent('%nse)%')))) >=20 > =C2=A0AND ((("res_partner"."type" !=3D 'private') OR "res_partner"."type"= IS NULL) OR "res_partner"."type" IS NULL ) >=20 > ORDER BY "res_partner"."display_name" ,"res_partner"."id" =C2=A0 > =C2=A0 =C2=A0LIMIT 100 >=20 > We have the common criteria =C2=A0(active=3Dtrue), the common sorting, a = limit, and a search > on various fields. The fields on which we're searching with criteria like= '% whatever%' are gin indexed. >=20 > =C2=A0Here is the query plan: > =C2=A0Limit =C2=A0(cost=3D0.56..10703.36 rows=3D100 width=3D25) (actual t= ime=3D56383.794..86509.036 rows=3D1 loops=3D1) > =C2=A0 =C2=A0Output: id, display_name > =C2=A0 =C2=A0Buffers: shared hit=3D4322296 read=3D1608998 dirtied=3D1 wri= tten=3D1247 > =C2=A0 =C2=A0-> =C2=A0Index Scan using res_partner_displayname_id_idx on = public.res_partner =C2=A0(cost=3D0.56..1200212.37 rows=3D11214 width=3D25) = (actual time=3D56383.793..86509.022 rows=3D1 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Output: id, display_name > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Filter: ((((res_partner.type)::text <> = 'private'::text) OR (res_partner.type IS NULL) OR (res_partner.type IS NULL= )) AND ((unaccent((res_partner.display_name)::text) ~~* '%nse%'::text) OR > (unaccent((res_partner.email)::text) ~~ > * '%nse%'::text) OR (unaccent((res_partner.ref)::text) ~~* '%nse%'::text)= OR (unaccent((res_partner.vat)::text) ~~* '%nse%'::text) OR (unaccent((res= _partner.company_registry)::text) ~~* > '%nse%'::text))) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Rows Removed by Filter: 6226870 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Buffers: shared hit=3D4322296 read=3D16= 08998 dirtied=3D1 written=3D1247 > =C2=A0Planning Time: 0.891 ms > =C2=A0Execution Time: 86509.070 ms > (10 rows) >=20 > It's not using our gin index at all, but the btree one. The problem is that PostgreSQL estimates that the index scan will return 11= 214 rows, when it is actually one. This makes the plan to scan the table using an index that matches the ORDER BY clause appealing: we might find 100 rows quickly and avoid a sort. You can try to improve the estimates with more detailed statistics, but if that doesn't do the job, you can modify the ORDER BY clause so that it cannot use the bad index: ORDER BY res_partner.display_name ,res_partner.id + 0 Yours, Laurenz Albe