Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id BFCEB476AD8 for ; Wed, 11 Sep 2002 11:31:09 -0400 (EDT) Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 5B30D476AD3 for ; Wed, 11 Sep 2002 11:31:09 -0400 (EDT) Received: by news.hub.org (Postfix, from userid 8) id 0AF8C381998; Wed, 11 Sep 2002 11:31:07 -0400 (EDT) From: gar8@pitt.edu (Tony Reina) X-Newsgroups: comp.databases.postgresql.admin Subject: Primary key order matters? Date: 11 Sep 2002 07:55:17 -0700 Organization: http://groups.google.com/ Lines: 17 Message-ID: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: groups-abuse@google.com To: pgsql-admin@postgresql.org X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/169 X-Sequence-Number: 5708 I've been tinkering with my database design over the last few months and today have found something significant that I wanted to share. It looks as though the order of the primary key index matters. I had originally had the order set the way I would typically think of the data if I were to look it up by hand (subject, arm, repetition). But then, I switched the order so that the field "repetition" was first. My queries sped up several orders of magnitude! (The cost went from something like 295,000 to 295). I think this is because there are many more repetitions than subjects. So at least for me, the primary key should be order so that the fields with more variance are listed first. Perhaps, this is known already to SQL gurus, but I hadn't come across it in reading about database optimization. -Tony