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 1tDLrk-00068F-Oc for pgsql-general@arkaria.postgresql.org; Tue, 19 Nov 2024 10:53:52 +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 1tDLrh-00CQ1Q-3g for pgsql-general@arkaria.postgresql.org; Tue, 19 Nov 2024 10:53:49 +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 1tDLrg-00CQ1B-Ox for pgsql-general@lists.postgresql.org; Tue, 19 Nov 2024 10:53:49 +0000 Received: from mail.evolu-s.it ([77.81.232.174]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tDLre-002jRQ-IN for pgsql-general@lists.postgresql.org; Tue, 19 Nov 2024 10:53:48 +0000 dkim-signature: v=1; a=rsa-sha256; d=evolu-s.it; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From:Subject:Date:Message-ID:To:MIME-Version:Content-Type:Content-Transfer-Encoding:In-Reply-To:References; bh=bgA+liWx9xwaK7Il7Z8Dvub904e+Eb1YwMLaOmI4S6s=; b=f4QyqhrIimeUOlmw+nJpguvTnzt1Yd4OFULHtMvjOwV9aPnlqHudAwg0iEUuL/bg0HyWwCW63JVbrz9kXiGzF9bv1TiBKWP6ovuPBc4HZim72qBRCuewh2O7e6/aVWZyNAiBTg0H1to9TdEOsf1zRJh3poLOit0wF3O8awzhfbg= Received: from [192.168.1.103] (res-129401d.ppp.twt.it [83.217.179.105]) by mail.evolu-s.it with ESMTPSA (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128) ; Tue, 19 Nov 2024 11:53:45 +0100 Message-ID: <14fa809c-a2b7-4d36-9382-d08a5df4718a@evolu-s.it> Date: Tue, 19 Nov 2024 11:53:44 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Functions and Indexes To: pgsql-general@lists.postgresql.org References: <2e36a2fe-b30f-46a5-937f-3b7d94e75cb6@evolu-s.it> <8dfd0bd5-87e5-4de7-9f60-8a680a32bc11@evolu-s.it> <61cb94a962667788c9c09107fa9937300e54d3cd.camel@cybertec.at> Content-Language: en-US, it From: Moreno Andreo In-Reply-To: <61cb94a962667788c9c09107fa9937300e54d3cd.camel@cybertec.at> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 18/11/24 20:05, Laurenz Albe wrote: > On Mon, 2024-11-18 at 15:37 +0100, Moreno Andreo wrote: >> I'm creating indexes for some tables, and I came across a doubt. >> >> If a column appears in the WHERE clause (and so it should be placed in >> index), in case it is "processed" in a function (see below), is it >> possible to insert this function to further narrow down things? >> >> Common index: >> SELECT foo1, foo2 FROM bar WHERE foo1 = 2 >> CREATE index bar1_idx ON bar USING btree(foo1); >> >> What about if query becomes >> SELECT foo1, foo2 FROM bar WHERE (POSITION(foo1 IN 'blah blah') >0) > You could create an index like > > CREATE INDEX ON bar (position(foo1 IN 'blah blah')); > > Alternatively, you could have a partial index: > > CREATE INDEX ON bar (foo1) INCLUDE (foo2) > WHERE position(foo1 IN 'blah blah') > 0; Interesting. Never seen this form, I'll look further on it. I stumbled into https://www.cybertec-postgresql.com/en/indexing-like-postgresql-oracle/ and discovered text_pattern_ops. I'm wondering if it can be of any use in my index, that should hold a WHERE condition with a combination of LIKE and the POSITION expression above. More docs to read ... :-) > >> Second question: I 've seen contrasting opinions about putting JOIN >> parameters (ON a.field1 = b.field2) in an index and I'd like to know >> your thoughts. > That depends on the join strategy PostgreSQL chooses. > You can use EXPLAIN to figure out the join strategy. > This article should explain details: > https://www.cybertec-postgresql.com/en/join-strategies-and-performance-in-postgresql/ Very nice article, clear and easy to understand! > > Yours, > Laurenz Albe > > Thanks, Moreno.