X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1A08A3A4A88 for ; Mon, 15 Nov 2004 21:01:15 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 47829-07 for ; Mon, 15 Nov 2004 21:00:59 +0000 (GMT) Received: from davinci.ethosmedia.com (server226.ethosmedia.com [209.128.84.226]) by svr1.postgresql.org (Postfix) with ESMTP id C3CFA3A4A5C for ; Mon, 15 Nov 2004 21:00:59 +0000 (GMT) Received: from [64.81.245.111] (account josh@agliodbs.com HELO temoku.sf.agliodbs.com) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.1.8) with ESMTP id 6664842; Mon, 15 Nov 2004 13:02:28 -0800 From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: pgsql-performance@postgresql.org, Alexandre Leclerc Subject: Re: Question on pgsql optimisation of SQL and structure (index, etc) Date: Mon, 15 Nov 2004 13:03:37 -0800 User-Agent: KMail/1.6.2 References: <1dc7f0e3041115094846c08c34@mail.gmail.com> In-Reply-To: <1dc7f0e3041115094846c08c34@mail.gmail.com> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200411151303.37551.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.3 tagged_above=0.0 required=5.0 tests=UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200411/213 X-Sequence-Number: 9167 Alexandre, > -- Assuming those tables (not original, but enought to get the point): > > CREATE TABLE prod.jobs ( > job_id serial PRIMARY KEY, > order_id integer NOT NULL REFERENCES sales.orders, > ); > > CREATE TABLE design.products ( > product_id serial PRIMARY KEY, > company_id integer NOT NULL REFERENCES sales.companies ON > UPDATE CASCADE, > product_code varchar(24) NOT NULL, > CONSTRAINT product_code_already_used_for_this_company UNIQUE > (company_id, product_code) > ); > > CREATE TABLE prod.jobs_products ( > product_id integer REFERENCES design.products ON UPDATE CASCADE, > ) INHERITS (prod.jobs); First off, let me say that I find this schema rather bizarre. The standard way to handle your situation would be to add a join table instead of inheritance for jobs_products: CREATE TABLE jobs_products ( job_id INT NOT NULL REFERENCES prod.jobs(job_id) ON DELETE CASCADE, product_id INT NOT NULL REFERENCES design.products(product_id) ON UPDATE CASCADE, CONSTRAINT jobs_products_pk PRIMARY KEY (job_id, product_id) ); Then this view: > CREATE VIEW prod.orders_jobs_view AS > SELECT job_id, order_id, product_code > FROM ( > SELECT *, NULL AS product_id FROM ONLY prod.jobs > UNION > SELECT * FROM prod.jobs_products > ) AS alljobs LEFT JOIN design.products ON alljobs.product_id = > products.product_id; Becomes much simpler, and better performance: CREATE VIEW prod.orders_jobs_view AS SELECT job_id, order_id, product_code FROM prod.jobs LEFT JOIN prod.jobs_products ON prod.jobs.job_id = prod.jobs_products.job_id LEFT JOIN design.products ON prod.jobs_products.product_id = design.products.product_id; > I imagine that somewhere down the road, this will get slow since there > is no index on the order_id. I tought of creating two indexes... With > the previous VIEW and database schema, will the following boost the > DB; as I don't know how PostgreSQL works internally: Yes. Any time you have a foreign key, you should index it unless you have a really good reason not to. -- --Josh Josh Berkus Aglio Database Solutions San Francisco