Return-Path: Delivered-To: apmail-db-derby-dev-archive@www.apache.org Received: (qmail 79054 invoked from network); 29 Jun 2009 14:20:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 29 Jun 2009 14:20:05 -0000 Received: (qmail 10520 invoked by uid 500); 29 Jun 2009 14:20:11 -0000 Delivered-To: apmail-db-derby-dev-archive@db.apache.org Received: (qmail 10493 invoked by uid 500); 29 Jun 2009 14:20:11 -0000 Mailing-List: contact derby-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: Delivered-To: mailing list derby-dev@db.apache.org Received: (qmail 10466 invoked by uid 99); 29 Jun 2009 14:20:11 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Jun 2009 14:20:11 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Jun 2009 14:20:08 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 4ED91234C045 for ; Mon, 29 Jun 2009 07:19:47 -0700 (PDT) Message-ID: <729976565.1246285187322.JavaMail.jira@brutus> Date: Mon, 29 Jun 2009 07:19:47 -0700 (PDT) From: "Dag H. Wanvik (JIRA)" To: derby-dev@db.apache.org Subject: [jira] Updated: (DERBY-2204) Foreign key constraint backing index creation can be smarter when foreign key is a subset of the table's primary key In-Reply-To: <500256.1167787947664.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/DERBY-2204?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Dag H. Wanvik updated DERBY-2204: --------------------------------- Derby Categories: [Performance] > Foreign key constraint backing index creation can be smarter when foreign key is a subset of the table's primary key > -------------------------------------------------------------------------------------------------------------------- > > Key: DERBY-2204 > URL: https://issues.apache.org/jira/browse/DERBY-2204 > Project: Derby > Issue Type: Improvement > Components: SQL > Reporter: Daniel John Debrunner > > When a foreign key's columns is a subset (or matches) the table's primary key then there is no need to create an additional backing index. Such an index will occupy disk space and decrease insert/update/delete performance. > Two typical situations where this is a problem are shown in the schema for the oe system test. > 1) A multiple column primary key has a sub-set of columns which are a primary key in another table. > ALTER TABLE CUSTOMER ADD CONSTRAINT > CUSTOMER_PK PRIMARY KEY(C_W_ID, C_D_ID, C_ID); > ALTER TABLE CUSTOMER ADD CONSTRAINT > C_D_FK_DISTRICT FOREIGN KEY (C_W_ID,C_D_ID) REFERENCES DISTRICT; > Derby will create two backing indexes here, one for the primary key and one for the foreign key. Derby could be improved > so that no index is created for the foreign key, since Derby can perform an index scan on a sub-set of the first N columns in an index. > Not sure how easy fixing the runtime portion of constraint manipulation is, if the constraint enforcement is via compiled SQL queries then it should be easier than if they are direct scans against the access api. > 2) A primary key in one table matches a primary key in another table, typically when the "sub" table contains additional optional information related to the main table. > ALTER TABLE NEWORDERS ADD CONSTRAINT > NEWORDERS_PK PRIMARY KEY(NO_W_ID, NO_D_ID, NO_O_ID); > ALTER TABLE NEWORDERS ADD CONSTRAINT > NO_O_FK FOREIGN KEY (NO_W_ID, NO_D_ID, NO_O_ID) REFERENCES ORDERS; > Derby will create two identical backing indexes here, one for the primary key and one for the foreign key. > Fixing this case might be easier than 1) since Derby already supports having mutliple logical indexes map to a single physical index. > In both cases work is needed to handle dropping of the primary key while the foreign key constraint is kept: > - in case 1) a new index will need to be built using the sub-set of the columns, thus dropping a primary key constraint could take time. > - in case 2) possibly the backing index can be re-used but it needs to be converted from a unique index to a non-unique one. > Also the case where the primary key is created after the foreign key needs to be covered, though that could be a follow on. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.