Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 26F4E63238F for ; Sun, 17 Jan 2010 21:22:25 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 30362-06 for ; Mon, 18 Jan 2010 01:22:07 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id F40A0632281 for ; Sun, 17 Jan 2010 21:22:13 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 913FA1E0449; Mon, 18 Jan 2010 09:22:09 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id MJNsaQqGYIYw; Mon, 18 Jan 2010 09:22:08 +0800 (WST) Received: from [192.168.3.115] (unknown [192.168.3.115]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 1356D1E0446; Mon, 18 Jan 2010 09:22:08 +0800 (WST) Message-ID: <4B53B7BF.9050907@postnewspapers.com.au> Date: Mon, 18 Jan 2010 09:22:07 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Seb CC: pgsql-sql@postgresql.org Subject: Re: indexes References: <87iqb3z74n.fsf@kolob.sebmags.homelinux.org> <4B53885C.3060706@gmail.com> <87fx64jk0f.fsf@kolob.sebmags.homelinux.org> In-Reply-To: <87fx64jk0f.fsf@kolob.sebmags.homelinux.org> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.018 tagged_above=-10 required=5 tests=AWL=0.581, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/33 X-Sequence-Number: 33569 Seb wrote: > On Mon, 18 Jan 2010 08:59:56 +1100, > Chris wrote: > >>> o Should all foreign keys have an index? > >> Not necessarily, you might just want the db to enforce the restriction >> but not actually use the data in it. For example, keep a userid (and >> timestamp) column of the last person to update a row. You may need it >> to say "aha - this was last changed on this date and by person X", but >> you'll never generally use it. > >> If you never have a where clause with that column, no need to index >> it. If you're using it in a join all the time, then yes it would be >> better to index it. > > Thanks for all your pointers! > > Do views use the indexes in the underlying tables, whenever say a SELECT > operation is called on the view? If so, indexes on views don't make any > sense right? A view WILL use an index(es) on the underlying table(s) if the planner thinks the use of those indexes will be helpful. In general it's as if you substituted the text of the view's query into the query using the view and executed that composite query. Sometimes views are very expensive to compute, and avoiding computing values you're not interested in for a particular query would be very handy. It's not always possible to tack on a WHERE clause that gets applied as part of a big complex join; sometimes you land up computing a lot of data then throwing the vast majority of it away. That's not really desirable. If you have very expensive views, a good way to handle this is to maintain a materialized view and index the materialized view. Pg doesn't currently have any support for creating and maintaining materialized views automatically, but it's easy enough to do it with a few triggers (and you can often do it more efficiently/intelligently than the database could do in the general case). I have a couple of materialized views in my schema that make queries several orders of magnitude faster (!) when querying for current data by eliminating the need for tree-like multiple self joins. Updates to these views are cheap, because the triggers on the source tables can selectively update only the mat.view entries they know are affected by a given update/insert/delete. -- Craig Ringer