Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 65B2863238F for ; Sun, 17 Jan 2010 18:00:13 -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 96616-09 for ; Sun, 17 Jan 2010 21:59:58 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pw0-f44.google.com (mail-pw0-f44.google.com [209.85.160.44]) by mail.postgresql.org (Postfix) with ESMTP id A17036322FD for ; Sun, 17 Jan 2010 18:00:02 -0400 (AST) Received: by pwi15 with SMTP id 15so1463351pwi.3 for ; Sun, 17 Jan 2010 14:00:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=G/iAC4nNCzUZU31/A3sqv1htrvICz1Og/wxkPMxGzWk=; b=P/Y9Duk8IVrKu0iXcZJ3MOdIqe946OmM0HmqDy0WcNJbxiop+EdMBJ3ioJrvYasZRU 0GiZd6GZ2ESbsgRKu89yZMXt2z7t7wltI72Sb6TEDNvyWHRxVgbu9QyKF6qwztNTHe3D PhXB2aeXBNYV9/rV+B5h6mVePZvr+UR69RG48= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=j0zrUKH5aujokOZdbwT6YErAhpTw43fjhxD5Bx9jBs1WLG/7fdpvw2CTtMZ0gHhczT Jjv4nGpdjPNoAIgqTjuhH9Tl9m9uXmGb4P61XKQn2N9JYnMWwO4veRpS0A4QSPe+W6F/ 4jqExJCAvWpG1RSqVNFwz8/8uacCtbdOUwGVg= Received: by 10.114.4.11 with SMTP id 11mr3637065wad.2.1263765601133; Sun, 17 Jan 2010 14:00:01 -0800 (PST) Received: from ?192.168.2.65? (office.squiz.net [203.206.128.174]) by mx.google.com with ESMTPS id 22sm3700638pzk.14.2010.01.17.13.59.59 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 17 Jan 2010 14:00:00 -0800 (PST) Message-ID: <4B53885C.3060706@gmail.com> Date: Mon, 18 Jan 2010 08:59:56 +1100 From: Chris 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> In-Reply-To: <87iqb3z74n.fsf@kolob.sebmags.homelinux.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.825 tagged_above=-10 required=5 tests=AWL=0.774, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/31 X-Sequence-Number: 33567 Seb wrote: > Hi, > > I have some views and queries that take a bit too long to return, so > perhaps some judicious indexes might help, but I don't know much about > how to use them. The PostgreSQL manual has a good section on indexes, > but I can't find guidance on (unless I missed something): > > o How to decide what columns need an index? I wrote something a little while ago about this: http://www.designmagick.com/article/16/ (comments welcome!) > 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. > o Naming conventions? That comes down to personal or project preference - there's no particular convention used anywhere. > o Does PostgreSQL use available indexes that can be useful in any query, > without the user having to do anything in particular? Yes - though just because an index is present doesn't mean postgres will use it, in some cases it's better for it to ignore the index altogether and use some other method to perform your query. -- Postgresql & php tutorials http://www.designmagick.com/