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 1lRbad-00020o-6E for pgsql-general@arkaria.postgresql.org; Wed, 31 Mar 2021 14:12:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lRbac-0002db-4l for pgsql-general@arkaria.postgresql.org; Wed, 31 Mar 2021 14:12:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRbab-0002dU-Th for pgsql-general@lists.postgresql.org; Wed, 31 Mar 2021 14:12:57 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRbaZ-0007oh-Sv for pgsql-general@lists.postgresql.org; Wed, 31 Mar 2021 14:12:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 12VECrGT3168687; Wed, 31 Mar 2021 10:12:53 -0400 From: Tom Lane To: Mohan Radhakrishnan cc: pgsql-general@lists.postgresql.org Subject: Re: Primary keys and composite unique keys(basic question) In-reply-to: References: Comments: In-reply-to Mohan Radhakrishnan message dated "Wed, 31 Mar 2021 14:06:06 +0530" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3168685.1617199973.1@sss.pgh.pa.us> Date: Wed, 31 Mar 2021 10:12:53 -0400 Message-ID: <3168686.1617199973@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Mohan Radhakrishnan writes: > We have UUIDs in our tables which are primary keys. But in > some cases > we also identify a composite unique key apart from the primary key. > My assumption is that there should be a unique key index created by us > using the composite key. And when we fetch using this composite key instead > of the primary key we have a performance boost due to the index. You haven't provided a lot of detail, but use-a-UUID-as-a-primary-key is often an antipattern. The UUIDs are quasi-random, meaning there's no locality of reference in the primary key index, resulting in inefficiency in searches and insertions. If the composite key you mention has some actual relationship to your application's usage patterns, it could be winning as a result of better locality of access to that index. regards, tom lane