Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id E12769FB478 for ; Thu, 4 Oct 2007 17:23:18 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 85647-07 for ; Thu, 4 Oct 2007 17:23:05 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from outmail136047.authsmtp.com (outmail136047.authsmtp.com [62.13.136.47]) by postgresql.org (Postfix) with ESMTP id 3EFF39F9B11 for ; Thu, 4 Oct 2007 17:23:07 -0300 (ADT) Received: from outmail128183.authsmtp.com (outmail128183.authsmtp.com [62.13.128.183]) by punt3.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l94KN1HP009355; Thu, 4 Oct 2007 21:23:04 +0100 (BST) Received: from [192.168.0.3] (85-211-149-22.dyn.gotadsl.co.uk [85.211.149.22]) (authenticated bits=0) by mail.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l94KN0Qu010982; Thu, 4 Oct 2007 21:23:00 +0100 (BST) Subject: Re: quickly getting the top N rows From: Simon Riggs To: Ben Cc: Tom Lane , pgsql-performance@postgresql.org In-Reply-To: References: <20071004141411.9509fe66.wmoran@collaborativefusion.com> <3785.1191523930@sss.pgh.pa.us> Content-Type: text/plain Date: Thu, 04 Oct 2007 21:23:05 +0100 Message-Id: <1191529385.4223.231.camel@ebony.site> Mime-Version: 1.0 X-Mailer: Evolution 2.8.2 Content-Transfer-Encoding: 7bit X-Server-Quench: 9a4a1cf7-72b7-11dc-b86a-001185d377ca X-AuthRoute: OCdxZQATClZOTQEd DAteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksadRtFaAFbYlpF HGQLW1NEUlx7W2B/ awofZQBDYEtPQQRu WVZLQE1QERpoT0AI AmVgUng3dwRGcXZy K0VlW3UVVRV+JEcu FElJEm9UYnphaTRM TRJQcVJJcANLfxdA PlN4UyBcLwdSbGoL JyIJERcLGAAXMTlU SQIJPBodTFoXHzgz DwwCEH0zGlVNYW0p KAM9Qidh X-Authentic-SMTP: 61633235383639.squirrel.dmpriest.net.uk:752/Kp X-Report-SPAM: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-Virus-Status: No virus detected - but ensure you scan with your own anti-virus system! X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/44 X-Sequence-Number: 27066 On Thu, 2007-10-04 at 12:52 -0700, Ben wrote: > The original, slow query: > > explain analyze SELECT * FROM log WHERE clientkey in > ('000000004500000000010000000001') AND premiseskey in > ('000000004500000000010000000001') and logicaldel = 'N' > ORDER BY logtime desc, logkey desc, clientkey desc, premiseskey desc LIMIT 20 offset 0; > > QUERY PLAN > ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > Limit (cost=356402.58..356402.63 rows=20 width=563) (actual time=215858.481..215858.527 rows=20 loops=1) > -> Sort (cost=356402.58..357598.25 rows=478267 width=563) (actual time=215858.478..215858.498 rows=20 loops=1) > Sort Key: logtime, logkey, clientkey, premiseskey > -> Seq Scan on log (cost=0.00..52061.67 rows=478267 width=563) (actual time=29.340..100043.313 rows=475669 loops=1) > Filter: ((clientkey = '000000004500000000010000000001'::bpchar) AND (premiseskey = '000000004500000000010000000001'::bpchar) AND (logicaldel = 'N'::bpchar)) > Total runtime: 262462.582 ms > (6 rows) > > > Every row in log has identical clientkey and premiseskey values, so if I > just remove those columns from the order by clause, I get this far > superior plan: > > explain analyze SELECT * FROM log WHERE clientkey in > ('000000004500000000010000000001') AND premiseskey in > ('000000004500000000010000000001') and logicaldel = 'N' > ORDER BY logtime desc, logkey desc LIMIT 20 offset 0; > QUERY PLAN > ------------------------------------------------------------------------------------------------------------------------------------------- > Limit (cost=0.00..12.33 rows=20 width=563) (actual time=0.047..0.105 rows=20 loops=1) > -> Index Scan Backward using log_ak1 on log (cost=0.00..294735.70 rows=478267 width=563) (actual time=0.044..0.076 rows=20 loops=1) > Index Cond: ((clientkey = '000000004500000000010000000001'::bpchar) AND (premiseskey = '000000004500000000010000000001'::bpchar)) > Filter: (logicaldel = 'N'::bpchar) > Total runtime: 0.165 ms > (5 rows) > > > ...which made me to think that maybe postgres is not using log_ak1 in the > former case because two of the columns in the order by match every row. > > Unfortunately, in this case it's not an option to alter the query. I'm > just trying to figure out an explaination. In the first query, Postgres cannot use the index because the sort order of the index does not match the sort order of the query. When you change the sort order of the query so that it matches that of the index, then the index is used. If you define your index on (logtime, logkey, clientkey, premiseskey) rather than on (clientkey, premiseskey, logtime, logkey) you will have a fast query. Yes, the column order matters. -- Simon Riggs 2ndQuadrant http://www.2ndQuadrant.com