Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T1KqT-0007uL-Fc for pgsql-general@postgresql.org; Tue, 14 Aug 2012 17:23:57 +0000 Received: from smtp101.prem.mail.ac4.yahoo.com ([76.13.13.40]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1T1KnA-0002VB-5D for pgsql-general@postgresql.org; Tue, 14 Aug 2012 17:20:34 +0000 Received: (qmail 21766 invoked from network); 14 Aug 2012 17:20:30 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=SVDQPqNmiW3ZBI2CCcvELLBEx6tr7oRjq4mJQRd1Yai90k/EC1sUduMqCR4SLjwfNxTtbhG/Si2uWs2dvUbf5DNcCXPi20IG8V91+cEgDG5XXE30TN6XopkA5YngTidCk5cl/4oltdN3KsrcmP9uHehw8T/KJOUgKIeCz5okjm0= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1344964830; bh=iCxknM9X3p9/6koqeyKQufMrNObpBfGAGHIeLRcDOf0=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language:> -----Original Message-----:> From:> owner@postgresql.org] On Behalf Of amit sehas:> Sent:> To:> Subject:> In SQL, given a table T, with two fields f1, f2,:> is it possible to create an index such that the same record is indexed in:the:> index, once with field f1 and once with field f2. (I am not looking for a:> compound index in which the key would look like , instead there:> should be two entries in the index and ).:> we have a few use cases for the above, perhaps we need to alter the:> schema somehow to accommodate the above,:> any advice is greatly appreciated ..:> thanks; b=rKXIwFXZyD1OXadfQw6ehWpMcRVJiBtMmdq1SQGYq48Wirfhg1UQuKgXFxEKtN1+AK0WIbsvF+Uh9kym+SiWFQf4cfJEDpFnpPg27dcKHuiZfn+cvl9vs1vgPjGmZjWDSAYIGVUYt0Vg96MI988VlEpCej+AMx5nBXlqh4lnHUw= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: p0wEkVoVM1nBGG1i1Ns3zUZexCRD8VBnSZm7tnw188c2HoY PUBsMy_iwyvxVLTR6SxnT1LzhajH3tVudrWiJO7CICmElmC7Ib3SxC2O90Z. wsgQbDF_RnMMF7_E6JEsv6nsJVrBMOqDWCa.awR6vuUXFQIjquIXwkRG_Eqn Bq9NKTuovI9Gcg0rRyNNrPo47UVJZtoAm0stinXC69ciYtFofqiz7hwVKuXe kssv6VjKkeS4oqJYlyb1MPo6kujs9OMQcilyGlfPJntFV3FTGhfi8SeMRJoS FGh9fAVXAT58Dx53kbV4QPBM.HvG2.4jJi692sS_.kGV40wK_lPOKk_nQU4o FUwX.2lT3bXOEgyO53c14VPG66icYyVR7DztATa7uUsFw.eclfP6draFBhrO OLJN9YTiQIgZ0x7bJMv.jWUHCaROERXFpW5mg X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp101.prem.mail.ac4.yahoo.com with SMTP; 14 Aug 2012 10:20:30 -0700 PDT From: "David Johnston" To: "'amit sehas'" , , References: <1344963288.3544.YahooMailClassic@web125305.mail.ne1.yahoo.com> In-Reply-To: <1344963288.3544.YahooMailClassic@web125305.mail.ne1.yahoo.com> Subject: Re: Indexing question Date: Tue, 14 Aug 2012 13:19:47 -0400 Message-ID: <022201cd7a41$0182e1b0$0488a510$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQJJSSSi0+Avoo4MEBqFPv1ubyhR9pZh7zNg Content-Language: en-us X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201208/287 X-Sequence-Number: 189464 > -----Original Message----- > From: pgsql-general-owner@postgresql.org [mailto:pgsql-general- > owner@postgresql.org] On Behalf Of amit sehas > Sent: Tuesday, August 14, 2012 12:55 PM > To: pgsql-sql@postgresql.org; pgsql-general@postgresql.org > Subject: [GENERAL] Indexing question > > In SQL, given a table T, with two fields f1, f2, > > is it possible to create an index such that the same record is indexed in the > index, once with field f1 and once with field f2. (I am not looking for a > compound index in which the key would look like , instead there > should be two entries in the index and ). > > we have a few use cases for the above, perhaps we need to alter the > schema somehow to accommodate the above, > > any advice is greatly appreciated .. > > thanks > In short: No, you cannot create an index on T in the way you describe. You need to create a new table: TF, with columns {T(id), f}, and having rows 1 and 2 with the same T(id) value; An index over "f" on table TF will then contain both values. Slightly longer: This seems like a classic case of column duplication. I am assuming that the columns in question are, say, phone1 and phone2 an you want to be able to search by phone number without having to specify the two fields separately. The correct way to do this is to create a "phone" table and add a single line for each phone number you want to store (along with the corresponding FK value of the original table) - with possibly a "phone_type" column. If this is not what you are after then you should be more explicit in your requirements. Why is creating two separate indexes (on f1 and f2) not acceptable? If indeed you are dealing with variations of the above example you really want to consider modifying your schema to use two tables with a one-to-many relationship because the current scenario begs the question(s): "why only f1 and f2? Why isn't there an f3?". The idea is that there are generally 3 separate cardinalities {0, 1, >1}. Zero you ignore, 1 you generally put on the same table - though not always, and more-than-one you create a separate table and store multiple values as separate rows instead of as columns. David J.