Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 08FE22E0057 for ; Thu, 8 May 2008 14:19:07 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 19865-01 for ; Thu, 8 May 2008 14:18:58 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from gateout02.mbox.net (gateout02.mbox.net [165.212.64.22]) by developer.postgresql.org (Postfix) with ESMTP id DEF0C2E002D for ; Thu, 8 May 2008 14:18:59 -0300 (ADT) Received: from gwsout02.mbox.net (gwsout02.mbox.net [165.212.64.25]) by gateout02.mbox.net (Postfix) with ESMTP id 40CEF6CC0C4; Thu, 8 May 2008 17:18:58 +0000 (GMT) Received: from GW2.EXCHPROD.USA.NET [165.212.116.254] by gwsout02.mbox.net via smtad (C8.MAIN.3.34P) with ESMTP id XID622meHRs71112Xo2; Thu, 08 May 2008 17:18:58 -0000 X-USANET-Source: 165.212.116.254 IN sthomas@leapfrogonline.com GW2.EXCHPROD.USA.NET X-USANET-MsgId: XID622meHRs71112Xo2 Received: from s1hub2.EXCHPROD.USA.NET ([10.120.220.32]) by GW2.EXCHPROD.USA.NET with Microsoft SMTPSVC(6.0.3790.1830); Thu, 8 May 2008 11:18:57 -0600 Received: from [192.168.50.198] (70.230.73.42) by exchange.postoffice.net (10.120.220.32) with Microsoft SMTP Server (TLS) id 8.1.263.0; Thu, 8 May 2008 18:18:57 +0100 Subject: Re: Creating a foreign key From: Shaun Thomas To: "Campbell, Lance" CC: "pgsql-performance@postgresql.org" In-Reply-To: References: Content-Type: text/plain; charset="utf-8" Organization: Leapfrog Online Date: Thu, 8 May 2008 12:18:59 -0500 Message-ID: <1210267139.25468.24.camel@berners-lee> MIME-Version: 1.0 X-Mailer: Evolution 2.12.1 Content-Transfer-Encoding: 8bit X-OriginalArrivalTime: 08 May 2008 17:18:57.0905 (UTC) FILETIME=[99A02E10:01C8B12F] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.778 tagged_above=0 required=5 tests=AWL=-0.653, DNS_FROM_OPENWHOIS=2.431, WHOIS_NETSOLPR=0.001 X-Spam-Level: * X-Archive-Number: 200805/127 X-Sequence-Number: 30095 On Thu, 2008-05-08 at 17:52 +0100, Campbell, Lance wrote: > Is there an index automatically created on field fk_a_id in table B > when I create a foreign key constraint? No. The problem with doing this is it assumes certain things about your infrastructure that may be entirely false. Indexes are to speed up queries by logarithmically reducing the result set to matched index parameters, and pretty much nothing else. Indexes are also not free, taking up both disk space and CPU time to maintain, slowing down inserts. Foreign keys are not bi-directional either. They actually check the index in the *source* table to see if the value exists. Having an index on a column referring to another table may be advantageous, but it's not always necessary. If you never use that column in a where clause, or it isn't restrictive enough, you gain nothing and lose speed in table maintenance. It's totally up to the focus of your table schema design, really. Only careful app management and performance analysis can really tell you where indexes need to go, beyond the rules-of-thumb concepts, anyway. > I also see “CREATE TABLE / PRIMARY KEY will create implicit index” > when creating a primary key but I don’t see any similar statement when > creating a foreign key. That's because the definition of a primary key is an index that acts as the primary lookup for the table. This is required to be an index, partially because it has an implied unique constraint, and also because it has a search-span of approximately 1 when locating a specific row from that table. But indexes aren't some kind of magical "make a query faster" sauce. With too many values, the cost of scanning them individually becomes prohibitive, and the database will fall-back to a faster sequence-scan, which can take advantage of the block-fetch nature of most storage devices to just blast through all the results for the values it's looking for. It's restrictive where clauses *combined* with well-chosen indexes that give you good performance, with a little tweaking here and there to make the query-planner happy. But that's the long version. Postgres is by no means bare-bones, but it assumes DBAs are smart enough to manage the structures they bolt onto the metal. :) -- Shaun Thomas Database Administrator Leapfrog Online 807 Greenwood Street Evanston, IL 60201 Tel. 847-440-8253 Fax. 847-570-5750 www.leapfrogonline.com