X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 88345D1B4FD for ; Sat, 11 Oct 2003 00:24:33 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 35198-04 for ; Fri, 10 Oct 2003 21:23:46 -0300 (ADT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 715BDD1B4EF for ; Fri, 10 Oct 2003 21:23:43 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 95B6F35203; Fri, 10 Oct 2003 17:23:31 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 943383514B; Fri, 10 Oct 2003 17:23:31 -0700 (PDT) Date: Fri, 10 Oct 2003 17:23:31 -0700 (PDT) From: Stephan Szabo To: David Busby Cc: PgSQL Performance ML Subject: Re: Index/Foreign Key Question In-Reply-To: <009c01c38f76$02150230$1100000a@busbydev> Message-ID: <20031010171953.B18529@megazone.bigpanda.com> References: <006a01c38f72$25461720$1100000a@busbydev> <1065821515.16433.10.camel@haggis> <009c01c38f76$02150230$1100000a@busbydev> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/377 X-Sequence-Number: 4125 On Fri, 10 Oct 2003, David Busby wrote: > ----- Original Message ----- > From: "Ron Johnson" > > On Fri, 2003-10-10 at 16:04, David Busby wrote: > > > List, > > > I'm creating this multi company POS database. > > > My inventory table looks like (all items are unique): > > > > > > id,category_id,invoice_id,x,y,z,gid,uid > > > > > > I have a primary key on id, and then an foreign keys on category_id and > > > invoice_id. > > > GID is the group ID of the company, UID is the companies user, they are > also > > > connected via foreign key to the respective tables. My question is > this: Do > > > I need to create more indexes on this table when inventory selects look > like > > > > > > select * from inventory where > > > category_id = 1 and invoice_id is null and gid = 2 > > > > > > So where would the indexes need to be placed? Or since I have the FK > setup > > > are the indexes already in place? I expect to soon have >500K items in > the > > > inventory table and don't want it to slow down. I'll have the same type > of > > > issue with clients, invoices, purchase_orders and perhaps more > > > > I'd make a multi-segment (non-unique?) index on: > > GID > > CATEGORY_ID > > INVOICE_ID > > > > So the multi column index would be better than the three individual indexes? For the query in question, yes. However, you probably want a category_id index and an invoice_id index if you are going to change the related tables because the (gid, category_id, invoice_id) isn't good enough for the foreign key checks (and the fk columns aren't automatically indexed because it can easily become a pessimization depending on the workload that's being done).