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 1nChBL-0004rr-Qh for pgsql-general@arkaria.postgresql.org; Wed, 26 Jan 2022 12:13:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nChAo-0002dE-89 for pgsql-general@arkaria.postgresql.org; Wed, 26 Jan 2022 12:13: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 1nChAn-0002d3-S3 for pgsql-general@lists.postgresql.org; Wed, 26 Jan 2022 12:13:13 +0000 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nChAl-0001JY-3h for pgsql-general@lists.postgresql.org; Wed, 26 Jan 2022 12:13:13 +0000 Received: by mail-pf1-x42d.google.com with SMTP id u130so18205919pfc.2 for ; Wed, 26 Jan 2022 04:13:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Iv0gFp/ClWfJKn2/nCeFgSgYpt7wdnfM+f0jhvKxd+4=; b=EaVLqrjSOTEKYcrnuWXAIwXFpwLWJITkwAXznIJ85olaFtyQCIRI81XsknLdUcTDn6 F7aBYb72nd7kAHIK3DRiJDZrc2860jo0pG9FRsoTrOiUAOkZzUHFnWpuEyys6dPefzNi +YDWWh8aR5/okr2X0Zl0Notne/ikfuhBM9VenoJcJ9BMYGvvhY+PNQoW1jHzz+g9BK/P qOSr28NpkEc/thdwazLVBRLDkyxKpjwz5zVAKdZFhZMSgxLlrembDvFWTaR9iMK78aK3 jP7hn85OH/mffS+73bitohFEBmPKxQ0eO6WzKLnjmlaJAdjnIFR4A0GGgyhOQ3OHo7Vz 0Mig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Iv0gFp/ClWfJKn2/nCeFgSgYpt7wdnfM+f0jhvKxd+4=; b=QGyi+H4AKugUWNr1SRy5+O1uL8Oqwo2vXYH4nZDUgCzyz815+oiiECwe+a2DMT1RZQ DTSBdMZsiuMwoIjslMtifQtmAVdWb2imVb7hy9WK/mQoFns2Wug4nUtEw1g7qbeuqho6 oLtha9s1h6Xe6QtQ01B6xgyOaYf3RO1/dV5Xc1ezBsyAAWM6Zq8ldV7oGyfMOkzUuo8h TWrXPl7BuM60MCZfubNbKjDJxstpBZtjnsDd1GMVESU57cEUJ3BWhNxM+kDD6pAwm7o6 ghV55egfbU+npEDRl3paKs92TiMjIqnResKqrw+djJWpfBmQ1Oq0GvfFqUN6pahz/KND NCng== X-Gm-Message-State: AOAM531/Oea5sA+goERdX9kkvSWZyg1z/+kJVJCHy0TJu+sYA6VGbV5Y fdgwKbKYSWnEEMs9V7E+f4g= X-Google-Smtp-Source: ABdhPJyJ6EzB1IOD3r7pVAjMxlrB7kmi4ATo03dSRva25e8LicspR3p+ZnNGDfwToH7pcgDlCiTuQA== X-Received: by 2002:a63:b145:: with SMTP id g5mr18586725pgp.113.1643199188801; Wed, 26 Jan 2022 04:13:08 -0800 (PST) Received: from jrouhaud (1-171-59-176.dynamic-ip.hinet.net. [1.171.59.176]) by smtp.gmail.com with ESMTPSA id s15sm2149795pfg.145.2022.01.26.04.13.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 04:13:08 -0800 (PST) Date: Wed, 26 Jan 2022 20:13:06 +0800 From: Julien Rouhaud To: Matthias Apitz Cc: Josef =?utf-8?B?xaBpbcOhbmVr?= , pgsql-general Subject: Re: SELECT with LIKE clause makes full table scan Message-ID: <20220126121306.k6ilsxl7vy2ppmjn@jrouhaud> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Wed, Jan 26, 2022 at 12:39:25PM +0100, Matthias Apitz wrote: > > > > sisis=# explain (analyze, buffers) select * from d01buch where d01ort like 'Z 9610%' ; > > > QUERY PLAN > > > ------------------------------------------------------------------------------------------------------------------------------- > > > Gather (cost=1000.00..680262.71 rows=510 width=952) (actual time=1324.096..1349.429 rows=1 loops=1) > > > Workers Planned: 4 > > > Workers Launched: 4 > > > Buffers: shared hit=102040 read=560674 > > > -> Parallel Seq Scan on d01buch (cost=0.00..679211.71 rows=128 width=952) (actual time=1117.663..1315.062 rows=0 loops=5) > > > Filter: (d01ort ~~ 'Z 9610%'::text) > > > Rows Removed by Filter: 1055853 > > > Buffers: shared hit=102040 read=560674 > > > Planning Time: 2.028 ms > > > Execution Time: 1349.593 ms > > > (10 Zeilen) > > > > > > Why is this (ignoring the Index) and what could be done? > [...] > create INDEX d01ort on d01buch(d01ort) ;/* D01ORT */ That index can't be used with a LIKE that has a trailing wildcard. You need to either create an index with text_pattern_ops operator class (see https://www.postgresql.org/docs/current/indexes-opclass.html), or a GIN index using pg_trgm (which will also work with non-trailing wildcards), see https://www.postgresql.org/docs/current/pgtrgm.html.