Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id CB5EC9FB259 for ; Thu, 1 Feb 2007 15:02:48 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 74012-07 for ; Thu, 1 Feb 2007 15:02:40 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id 1197F9F9F10 for ; Thu, 1 Feb 2007 15:02:42 -0400 (AST) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 2679573C60; Thu, 1 Feb 2007 19:02:41 +0000 (GMT) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 00C15274041; Thu, 1 Feb 2007 19:02:40 +0000 (GMT) Message-ID: <45C23950.4070803@archonet.com> Date: Thu, 01 Feb 2007 19:02:40 +0000 From: Richard Huxton User-Agent: IceDove 1.5.0.9 (X11/20061220) MIME-Version: 1.0 To: Michael Artz Cc: pgsql-performance@postgresql.org Subject: Re: Subselect query enhancement References: <45C22223.1090909@archonet.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/8 X-Sequence-Number: 23062 Michael Artz wrote: >> > I have primary table that holds ip information >> > and two other tables that hold event data for the specific IP in with >> > a one-to-many mapping between them, ie: >> [snip] >> > There is quite a bit of commonality between the network_events and >> > host_events schemas, but they do not currently share an ancestor. >> > ip_info has about 13 million rows, the network_events table has about >> > 30 million rows, and the host_events table has about 7 million rows. >> > There are indexes on all the rows. >> >> What indexes though. Do you have (name,ip) on the two event tables? > > All the columns are indexed individually. The tables are completely > static, as I reload the whole DB with new data every day. The point of a (name,ip) index would be to let you read off ip numbers in order easily. >> How selective is "name" - are there many different values or just a few? >> If lots, it might be worth increasing the statistics gathered on that >> column (ALTER COLUMN ... SET STATISTICS). >> http://www.postgresql.org/docs/8.2/static/sql-altertable.html > > I guess that is the heart of my question. "name" is not very > selective (there are only 20 or so choices) however other columns are > fairly selective for certain cases, such as 'port'. When querying on > and unusual port, the query is very fast, and the single UNIONed > subselect returns quickly. When 'port' is not very selective (like > port = '80', which is roughly 1/2 of the rows in the DB), the dual > subselect query wins, hands-down. > > And I have altered the statistics via the config file: > default_statistics_target = 100 > Perhaps this should be even higher for certain columns? You're probably better off leaving it at 10 and upping it for the vital columns. 25 for names should be a good choice. You could try partial indexes for those cases where you have particularly common values of name/port: CREATE INDEX idx1 ON host_events (ip) WHERE port=80; -- Richard Huxton Archonet Ltd