Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 540076336CE for ; Thu, 27 May 2010 11:09:38 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 27504-08 for ; Thu, 27 May 2010 14:09:30 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.187]) by mail.postgresql.org (Postfix) with ESMTP id 23614633558 for ; Thu, 27 May 2010 11:09:30 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0LzHnN-1NDw8b25OT-014oLA; Thu, 27 May 2010 16:09:25 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1OHdlz-0003WC-00 for pgsql-sql@postgresql.org; Thu, 27 May 2010 16:09:23 +0200 Date: Thu, 27 May 2010 16:09:23 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Question about slow queries... Message-ID: <20100527140923.GF6907@a-kretschmer.de> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX1/lsE7wDp7vmLAheLBYjTloo/V6IYlNR+yiY7I aGzlLo+9IsOA3KVOV6QFy2WkNyKkEt6YRgH4InGht+zCRJkbdN +AifbvHG4R/OECjcFX+wA== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.81 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_NONE=-0.0001, T_FRT_CONTACT=0.01 X-Spam-Level: X-Archive-Number: 201005/131 X-Sequence-Number: 34179 In response to Good, Thomas : > > Hi, > > I have a question about a query that starts out fine and over time > slows to a halt - but only on a webhosted site. Locally it does fine. > > The query is a singleton select (no joins), hitting a table with about > 5,000 records in it. Over time the query slows to a crawl and I have Okay, about 5000 records. > to dump and rebuild/repopulate to restore efficiency. Vacuum does > nothing but a dump and reload fixes the problem and the query runs > lightning fast again -- for a period of a week or so. As stated this okay > > With everything working properly (table rebuilt just yesterday) explain analyze produces this on the webhost: > > Sort (cost=121547.89..121558.43 rows=4214 width=4620) (actual time=386.172..386.224 rows=89 loops=1) > Sort Key: visit_date > -> Seq Scan on client_service_note (cost=0.00..100334.19 rows=4214 width=4620) (actual time=0.019..385.917 rows=89 loops=1) > Filter: (client_id = 385) > Total runtime: 386.335 ms Well, estimated rows= 4214, nealy the whole table.Because of this the planner choose a seq-scan. But - real only 89 rows with this condition. Again: estimated rows=4214, real rows=89. This is a big difference! I think, you have a lot of dead tuples in this table - many updates and/or deletes & inserts. You should check your vacuum-strategy. Other reason, maybe, wrong statistics. You can try to increase statistics target. (alter table ... ALTER [ COLUMN ] column SET STATISTICS 100 or more) > > And locally: > > Sort (cost=535.82..536.04 rows=88 width=696) (actual time=2.140..2.156 rows=88 loops=1) > Sort Key: visit_date > Sort Method: quicksort Memory: 65kB > -> Seq Scan on client_service_note (cost=0.00..532.98 rows=88 width=696) (actual time=0.014..1.988 rows=88 loops=1) > Filter: (client_id = 385) > Total runtime: 2.295 ms Do you have an INDEX on client_id? > > My query is terse: > SELECT * FROM client_service_note WHERE client_id = 385 ORDER BY visit_date DESC; > > Locally I'm running 8.4.2, the webhost is 8.1.18 There are many improvements between 8.1 and 8.4 ... Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99