Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 8E1A56321F1 for ; Wed, 6 Jan 2010 16:03:33 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 41909-02 for ; Wed, 6 Jan 2010 20:03:19 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from n22.bullet.mail.mud.yahoo.com (n22.bullet.mail.mud.yahoo.com [68.142.206.161]) by mail.postgresql.org (Postfix) with SMTP id 8D673631E46 for ; Wed, 6 Jan 2010 16:03:22 -0400 (AST) Received: from [68.142.200.221] by n22.bullet.mail.mud.yahoo.com with NNFMP; 06 Jan 2010 20:03:21 -0000 Received: from [68.142.201.71] by t9.bullet.mud.yahoo.com with NNFMP; 06 Jan 2010 20:03:21 -0000 Received: from [127.0.0.1] by omp423.mail.mud.yahoo.com with NNFMP; 06 Jan 2010 20:03:21 -0000 X-Yahoo-Newman-Id: 70002.51052.bm@omp423.mail.mud.yahoo.com Received: (qmail 98388 invoked from network); 6 Jan 2010 20:03:20 -0000 Received: from adsl-76-200-182-62.dsl.pltn13.sbcglobal.net (rm_pg@76.200.182.62 with plain) by smtp107.biz.mail.sp1.yahoo.com with SMTP; 06 Jan 2010 12:03:20 -0800 PST X-Yahoo-SMTP: vGlHhPCswBDHoDrPQ1hdAE8TsaGS X-YMail-OSG: CNUXlRcVM1n25g3M24IIGVivia9rHJ9h6_sTq.cIHRCo8.BqZCGN5cbCVxqb7vfqUbq2cSex58AETOp_QJKy6c0Glas8eudQGkfIGeRWvdU8Yb6gQUuXOyRrzbjvMof5JdzKDn2IoDLbX1T8V0sK2OD9RYMLciu_qtbZNlGcpoxSAlknq_H00h2FAqCQyOfHMZBzG0ZsSSYyItXvvHsV2qsBnhePg.rv9wsLsRJjCGd9bQlMFYQ2yWJMzDVOFJK36x3xy51_0IojmXq0tqJKKmfv4wC4alsE2nfn8iSnLWtA82RHcoCcQ0E- X-Yahoo-Newman-Property: ymail-3 Message-ID: <4B44EC89.7060200@cheapcomplexdevices.com> Date: Wed, 06 Jan 2010 12:03:21 -0800 From: Ron Mayer User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Jesper Krogh CC: pgsql-performance@postgresql.org Subject: Re: Digesting explain analyze References: <4B44E02A.6080109@krogh.cc> In-Reply-To: <4B44E02A.6080109@krogh.cc> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.512 tagged_above=-10 required=5 tests=AWL=-2.678, BAYES_50=0.001, FH_DATE_PAST_20XX=3.188, UNPARSEABLE_RELAY=0.001 X-Spam-Level: X-Archive-Number: 201001/64 X-Sequence-Number: 36944 Jesper Krogh wrote: > I have a table that consists of somewhere in the magnitude of 100.000.000 > rows and all rows are of this tuples > > (id1,id2,evalue); > > Then I'd like to speed up a query like this: > > explain analyze select id from table where id1 = 2067 or id2 = 2067 order > by evalue asc limit 100; > > ...The inner sets are on average 3.000 for > both id1 and id2 and a typical limit would be 100, so if I could convince > postgresql to not fetch all of them then I would reduce the set retrieved > by around 60. The dataset is quite large so the random query is not very > likely to be hitting the same part of the dataset again, so there is going > to be a fair amount of going to disk., If disk seeks are killing you a kinda crazy idea would be to duplicate the table - clustering one by (id1) and the other one by an index on (id2) and unioning the results of each. Since each of these duplicates of the table will be clustered by the column you're querying it on, it should just take one seek in each table. Then your query could be something like select * from ( select * from t1 where id1=2067 order by evalue limit 100 union select * from t2 where id2=2067 order by evalue limit 100 ) as foo order by evalue limit 100; Hmm.. and I wonder if putting evalue into the criteria to cluster the tables too (i.e. cluster on id1,evalue) if you could make it so the limit finds the right 100 evalues first for each table....