Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 3D8B1634E7C for ; Mon, 23 Feb 2009 09:27:52 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 28227-05 for ; Mon, 23 Feb 2009 09:27:48 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay.pcl-ipout01.plus.net (relay.pcl-ipout01.plus.net [212.159.7.99]) by mail.postgresql.org (Postfix) with ESMTP id 06C5D634C83 for ; Mon, 23 Feb 2009 09:27:48 -0400 (AST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAPMyoknUnw4S/2dsb2JhbADIOgeMaYQPBg Received: from pih-relay05.plus.net ([212.159.14.18]) by relay.pcl-ipout01.plus.net with ESMTP; 23 Feb 2009 13:27:46 +0000 Received: from [84.51.143.99] (helo=server3.office.archonet.com) by pih-relay05.plus.net with esmtp (Exim) id 1LbaqX-0005gX-8w; Mon, 23 Feb 2009 13:27:45 +0000 Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id ED49A27405F; Mon, 23 Feb 2009 13:27:44 +0000 (GMT) Message-ID: <49A2A450.20002@archonet.com> Date: Mon, 23 Feb 2009 13:27:44 +0000 From: Richard Huxton User-Agent: Thunderbird 2.0.0.19 (X11/20090105) MIME-Version: 1.0 To: kouber@saparev.com CC: pgsql-performance@postgresql.org Subject: Re: LIMIT confuses the planner References: <49A295F0.5010405@saparev.com> In-Reply-To: <49A295F0.5010405@saparev.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Plusnet-Relay: 64f7d17395b9e137f1ebcab6274e0a9b X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200902/263 X-Sequence-Number: 32844 Kouber Saparev wrote: > db=# EXPLAIN ANALYZE > SELECT > * > FROM > login_attempt > WHERE > username='kouber' > ORDER BY > login_attempt_sid DESC; > > QUERY PLAN > ------------------------------------------------------------------------------------------------------------------------------------------------------ > > Sort (cost=1415.15..1434.93 rows=7914 width=38) (actual > time=0.103..0.104 rows=2 loops=1) > Sort Key: login_attempt_sid > Sort Method: quicksort Memory: 25kB > -> Index Scan using login_attempt_username_idx on login_attempt > (cost=0.00..902.71 rows=7914 width=38) (actual time=0.090..0.091 rows=2 > loops=1) > Index Cond: ((username)::text = 'kouber'::text) > Total runtime: 0.140 ms It's expecting 7914 rows returned and is getting only 2. That is probably the root of the problem. > However when I add a LIMIT clause to the same query the planner no > longer uses the right index, hence the query becomes very slow: > > > db=# EXPLAIN ANALYZE > SELECT > * > FROM > login_attempt > WHERE > username='kouber' > ORDER BY > login_attempt_sid DESC LIMIT 20; Since it's expecting 7914 rows for "kouber" it thinks it will find the 20 rows you want fairly quickly by just looking backward through the login_attempt_pkey index. Try increasing the stats on the username column. ALTER TABLE login_attempt ALTER COLUMN username SET STATISTICS 100; ANALYZE login_attempt; You can try different values of statistics up to 1000, but there's no point in setting it too high. -- Richard Huxton Archonet Ltd