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 1r2vfm-001Qi7-3k for pgsql-general@arkaria.postgresql.org; Tue, 14 Nov 2023 15:49:54 +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 1r2vfk-008Wez-RR for pgsql-general@arkaria.postgresql.org; Tue, 14 Nov 2023 15:49:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r2vfk-008Weq-FC for pgsql-general@lists.postgresql.org; Tue, 14 Nov 2023 15:49:52 +0000 Received: from csmtpq2-prd-nl1-vfz.edge.unified.services ([84.116.50.17]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r2vfh-006PXm-Sb for pgsql-general@lists.postgresql.org; Tue, 14 Nov 2023 15:49:51 +0000 Received: from csmtp3-prd-nl1-vfz.nl1.unified.services ([100.107.80.136] helo=csmtp3-prd-nl1-vfz.edge.unified.services) by csmtpq2-prd-nl1-vfz.edge.unified.services with esmtp (Exim 4.93) (envelope-from ) id 1r2vff-00FBIo-EU for pgsql-general@lists.postgresql.org; Tue, 14 Nov 2023 16:49:47 +0100 Received: from [10.224.60.100] ([80.112.113.103]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 256/256 bits) (Client did not present a certificate) by csmtp3-prd-nl1-vfz.edge.unified.services with ESMTPSA id 2vffr0Q3pYkqS2vffr52QZ; Tue, 14 Nov 2023 16:49:47 +0100 X-Env-Mailfrom: dld@casema.nl X-Env-Rcptto: pgsql-general@lists.postgresql.org X-SourceIP: 80.112.113.103 X-CNFS-Analysis: v=2.4 cv=IeSBW3qa c=1 sm=1 tr=0 ts=6553971b cx=a_exe a=pVbCT6cejT8MIlhhO9MO2A==:117 a=pVbCT6cejT8MIlhhO9MO2A==:17 a=IkcTkHD0fZMA:10 a=t1umn8f2fqU7N5YIttsA:9 a=QEXdDO2ut3YA:10 X-Authenticated-Sender: dld@casema.nl DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=casema.nl; s=202002corplgsmtpnl; t=1699976987; bh=cOHr4uT3f4NHqxEuf1GrTRMuAWVJ1fGzV+Cu4lvjsgs=; h=Date:To:From:Subject; b=IBC57i6VytTnHanqLNC+VXpU+jLzc6BLdAaxIUz1dCjqnUvoZuO+L0JlkJJmHwCps IPCguDdqQkhVbuxk+Hl9fgh1plNrrV1bLtBlg5GVDFcZukaX2kIOvs/PFqQVOyg5AK VNDTHjFjkmh73pFdRC7w7Elmz2v6UxeVc40ILU2334wbecEcoTi6+EYpGzbotxsk3g wutBRqunFBoRI3T8XnL+KBc/MbspNIvGurc72YkCwKlqfgXrhWg9bFvX70CnKqdqpm Wn0NJCL3SWL4KitnyARH/BrIHFNAUIx8KuCHyyEw9JML+QWf7OyW+6D7FMTzhfXXwc crtfY7KnHra8w== Message-ID: Date: Tue, 14 Nov 2023 16:49:46 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: pgsql-general@lists.postgresql.org From: dld Subject: Re: Indexing fragments of a column's value ? Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CMAE-Envelope: MS4xfKD1j8DhGlfD3juIKzfqhAn8PBG2hl6ShGeVj70Addhre6UpDYYcQEKchezgQJBDntx+2aWZX+HIINbQcEJOni0lSsK5leutVCeiaoINs79cCDQ1eE7q oLC1AXPgvnJOVghqjIOBxCm1O8AlbzaUqmyxj6q+P3LCrAai34yhhp8JeyZpKUEyO/X0aTEdn1EQWhJ/VOVqXpwnYEx22D08oFA= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk You can index on expressions, and these will be recognised by the query generator. drop schema tmp CASCADE; create schema tmp; set search_path = tmp; CREATE TABLE bagger         ( eight CHAR(8) NOT NULL PRIMARY KEY         , more text         ); CREATE INDEX bagger_idx_12 ON bagger (substr(eight,1,2)); CREATE INDEX bagger_idx_34 ON bagger (substr(eight,3,2)); CREATE INDEX bagger_idx_58 ON bagger (substr(eight,5,4)); INSERT INTO bagger(eight, more) SELECT translate(to_hex( gs), ' ' , '0')         , gs::text FROM generate_series(0,4000000000, 64999) gs         ; VACUUM ANALYZE bagger; EXPLAIN ANALYZE SELECT * FROM bagger WHERE 1=1         -- AND eight >= '00' AND eight < '05'         AND substr(eight, 1,2) >= '30' AND substr(eight,1,2) < '05'         AND substr(eight, 3,2) >= '90' AND substr(eight,3,2) < 'A5'         AND substr(eight, 5,4) >= '3333' AND substr(eight,5,4) < '4444'         ; /*** The optimiser is smart enough to ignore one of the indexes. */ QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  Bitmap Heap Scan on bagger  (cost=41.46..59.62 rows=1 width=19) (actual time=0.049..0.056 rows=0 loops=1)    Recheck Cond: ((substr((eight)::text, 1, 2) >= '30'::text) AND (substr((eight)::text, 1, 2) < '05'::text) AND (substr((eight)::text, 3, 2) >= '90'::text) AND (substr((eight)::text, 3, 2) < 'A5'::text))    Filter: ((substr((eight)::text, 5, 4) >= '3333'::text) AND (substr((eight)::text, 5, 4) < '4444'::text))    ->  BitmapAnd  (cost=41.46..41.46 rows=16 width=0) (actual time=0.042..0.047 rows=0 loops=1)          ->  Bitmap Index Scan on bagger_idx_12 (cost=0.00..4.47 rows=308 width=0) (actual time=0.039..0.039 rows=0 loops=1)                Index Cond: ((substr((eight)::text, 1, 2) >= '30'::text) AND (substr((eight)::text, 1, 2) < '05'::text))          ->  Bitmap Index Scan on bagger_idx_34 (cost=0.00..36.74 rows=3205 width=0) (never executed)                Index Cond: ((substr((eight)::text, 3, 2) >= '90'::text) AND (substr((eight)::text, 3, 2) < 'A5'::text))  Planning Time: 5.487 ms  Execution Time: 0.310 ms (10 rows) HTH, AvK