X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 5A37C9DCADB for ; Tue, 6 Dec 2005 09:41:15 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 12150-03 for ; Tue, 6 Dec 2005 09:41:18 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtpauth09.mail.atl.earthlink.net (smtpauth09.mail.atl.earthlink.net [209.86.89.69]) by postgresql.org (Postfix) with ESMTP id 04B589DCAC4 for ; Tue, 6 Dec 2005 09:41:12 -0400 (AST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=dk20050327; d=earthlink.net; b=eQqmr2XQKlIPKIAPkrx/MtFcHaOp5EitOUFM1Jw5afmi9jQ5cHuxCAMnB+WmQWAJ; h=Received:Message-Id:X-Mailer:Date:To:From:Subject:In-Reply-To:References:Mime-Version:Content-Type:X-ELNK-Trace:X-Originating-IP; Received: from [70.22.193.119] (helo=ron-6d52adff2a6.earthlink.net) by smtpauth09.mail.atl.earthlink.net with asmtp (Exim 4.34) id 1Ejd4F-0002II-OO; Tue, 06 Dec 2005 08:41:15 -0500 Message-Id: <6.2.5.6.0.20051206080854.01de6ba0@earthlink.net> X-Mailer: QUALCOMM Windows Eudora Version 6.2.5.6 Date: Tue, 06 Dec 2005 08:41:11 -0500 To: Joost Kraaijeveld , Pgsql-Performance From: Ron Subject: Re: Can this query go faster??? In-Reply-To: <1133862229.8837.61.camel@Panoramix> References: <1133862229.8837.61.camel@Panoramix> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-ELNK-Trace: acd68a6551193be5d780f4a490ca69563f9fea00a6dd62bc343ae2dd30243cf28756bb53ef9a2829350badd9bab72f9c350badd9bab72f9c350badd9bab72f9c X-Originating-IP: 70.22.193.119 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200512/165 X-Sequence-Number: 15986 At 04:43 AM 12/6/2005, Joost Kraaijeveld wrote: >Hi, > >Is it possible to get this query run faster than it does now, by adding >indexes, changing the query? > >SELECT customers.objectid FROM prototype.customers, prototype.addresses >WHERE >customers.contactaddress = addresses.objectid >ORDER BY zipCode asc, housenumber asc >LIMIT 1 OFFSET 283745 > >Explain: > >Limit (cost=90956.71..90956.71 rows=1 width=55) > -> Sort (cost=90247.34..91169.63 rows=368915 width=55) > Sort Key: addresses.zipcode, addresses.housenumber > -> Hash Join (cost=14598.44..56135.75 rows=368915 width=55) > Hash Cond: ("outer".contactaddress = "inner".objectid) > -> Seq Scan on customers (cost=0.00..31392.15 >rows=368915 width=80) > -> Hash (cost=13675.15..13675.15 rows=369315 width=55) > -> Seq Scan on addresses (cost=0.00..13675.15 >rows=369315 width=55) > >The customers table has an index on contactaddress and objectid. >The addresses table has an index on zipcode+housenumber and objectid. > >TIA customer names, customers.objectid, addresses, and addresses.objectid should all be static (addresses do not change, just the customers associated with them; and once a customer has been assigned an id that better never change...). To me, this sounds like the addresses and customers tables should be duplicated and then physically laid out in sorted order by .objectid in one set and by the "human friendly" associated string in the other set. Then a finding a specific .objectid or it's associated string can be done in at worse O(lgn) time assuming binary search instead of O(n) time for a sequential scan. If pg is clever enough, it might be able to do better than that. IOW, I'd try duplicating the addresses and customers tables and using the appropriate CLUSTERed Index on each. I know this breaks Normal Form. OTOH, this kind of thing is common practice for data mining problems on static or almost static data. Hope this is helpful, Ron