X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5FC6B52806 for ; Wed, 10 Aug 2005 14:52:32 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 23587-10 for ; Wed, 10 Aug 2005 17:52:22 +0000 (GMT) Received: from mail.nordicbet.com (mail.nordicbet.com [193.69.167.130]) by svr1.postgresql.org (Postfix) with ESMTP id BD32252C3C for ; Wed, 10 Aug 2005 14:52:17 -0300 (ADT) Received: from [84.48.51.94] (helo=tobias.nordicbet.invalid) (Authenticated Sender=tobias@nordicbet.com) by mail.nordicbet.com with esmtpa (Exim 4.50 #1 (Debian)) id 1E2uiM-0004XD-9f for ; Wed, 10 Aug 2005 19:50:13 +0200 Received: by tobias.nordicbet.invalid (Postfix, from userid 500) id 7F3ABE14C2; Wed, 10 Aug 2005 19:52:08 +0200 (CEST) Date: Wed, 10 Aug 2005 19:52:08 +0200 From: Tobias Brox To: pgsql-performance@postgresql.org Subject: partial index regarded more expensive Message-ID: <20050810175208.GD6141@tobias.lan> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Organization: Group Nordicbet User-Agent: Mutt/1.5.8i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200508/96 X-Sequence-Number: 13843 So, I have a table game with a timestamp attribute 'game_end', ranging from jan-2005 to present. The game table also have an attribute state, with live games beeing in state 2, and ended games beeing in state 4 (so, game_end+delta>now() usually means state=4). There are also an insignificant number of games in states 1,3. This query puzzles me: select * from game where game_end>'2005-07-30' and state in (3,4); Now, one (at least me) should believe that the best index would be a partial index, "resolved_game_by_date" btree (game_end) WHERE ((state = 3) OR (state = 4)) NBET=> explain analyze select * from game where game_end>'2005-07-30' and state in (3,4); QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- Index Scan using resolved_game_by_date on game (cost=0.00..7002.87 rows=7147 width=555) (actual time=0.220..86.234 rows=3852 loops=1) Index Cond: (game_end > '2005-07-30 00:00:00'::timestamp without time zone) Filter: ((state = 3) OR (state = 4)) Total runtime: 90.568 ms (4 rows) Since state has only two significant states, I wouldn't believe this index to be any good: "game_by_state" btree (state) ...and it seems like I'm right: NBET=> explain analyze select * from game where game_end>'2005-07-30' and state in (3,4); QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------ Index Scan using game_by_state, game_by_state on game (cost=0.00..4413.78 rows=7147 width=555) (actual time=0.074..451.771 rows=3851 loops=1) Index Cond: ((state = 3) OR (state = 4)) Filter: (game_end > '2005-07-30 00:00:00'::timestamp without time zone) Total runtime: 457.132 ms (4 rows) Now, how can the planner believe the game_by_state-index to be better? ('vacuum analyze game' did not significantly impact the numbers, and I've tried running the queries some times with and without the game_by_state-index to rule out cacheing effects) -- Tobias Brox This signature has been virus scanned, and is probably safe to read. This mail may contain confidential information, please keep your eyes closed.