Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 9C5006335FB for ; Wed, 28 Jul 2010 21:51:25 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 02216-07 for ; Thu, 29 Jul 2010 00:51:16 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-px0-f174.google.com (mail-px0-f174.google.com [209.85.212.174]) by mail.postgresql.org (Postfix) with ESMTP id 4A392632DD1 for ; Wed, 28 Jul 2010 21:51:17 -0300 (ADT) Received: by pxi14 with SMTP id 14so1137636pxi.19 for ; Wed, 28 Jul 2010 17:51:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:content-type :content-transfer-encoding; bh=vw6lq2RoLX5SuF7mvn4627u2H60yjpFTnuSidmJiLoM=; b=ei1zS0aIZw7pRmORkXZMSxo4oCmZxBQWAY12yDDo8xmlyLn8M13CwHFQKcGH21H3a1 9M6NosCuWuWCX4q3EUqPvb19cngDpc+awCqcqzGCjPznT4GXLf7s1lmFIfO9nVCb2g3c PC4KDRENLakRLcvAUg/ApCalDSuZed7aQzJM0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; b=vpU9ZqiP6y3IIgPmreqbJh+U2qGHQaFrTHI+TU6zGFukXzXZIKlOjS9OCwhd4tqCqm tTnfYn0LJCGooKEbmhWVm2zKXLsP+yubhAiuPjQaDBE06tmOHOC9WpMDzLAvHRswzX7n fiJoW30Fow2U8pXiHwIyACz8wJ9ixXNOAOoEk= Received: by 10.114.59.10 with SMTP id h10mr16126162waa.194.1280364674654; Wed, 28 Jul 2010 17:51:14 -0700 (PDT) Received: from [192.168.2.65] (ppp234-39.static.internode.on.net [59.167.234.39]) by mx.google.com with ESMTPS id 33sm356311wad.18.2010.07.28.17.51.12 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 28 Jul 2010 17:51:13 -0700 (PDT) Message-ID: <4C50D07D.4040004@gmail.com> Date: Thu, 29 Jul 2010 10:51:09 +1000 From: Chris User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.11) Gecko/20100713 Thunderbird/3.0.6 MIME-Version: 1.0 To: PostgreSQL Performance Subject: planner index choice Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201007/440 X-Sequence-Number: 39829 Hi there, I have a simple query where I don't understand the planner's choice to use a particular index. The main table looks like this: # \d sq_ast_attr_val Table "public.sq_ast_attr_val" Column | Type | Modifiers -------------+-----------------------+------------------------------ assetid | character varying(15) | not null attrid | integer | not null contextid | integer | not null default 0 custom_val | text | use_default | character(1) | not null default '1'::bpchar Indexes: "ast_attr_val_pk" PRIMARY KEY, btree (assetid, attrid, contextid) "sq_ast_attr_val_assetid" btree (assetid) "sq_ast_attr_val_attrid" btree (attrid) "sq_ast_attr_val_concat" btree (((assetid::text || '~'::text) || attrid)) "sq_ast_attr_val_contextid" btree (contextid) The query: SELECT assetid, custom_val FROM sq_ast_attr_val WHERE attrid IN (SELECT attrid FROM sq_ast_attr WHERE name = 'is_contextable' AND (type_code = 'metadata_field_select' OR owning_type_code = 'metadata_field')) AND contextid = 0 INTERSECT SELECT assetid, custom_val FROM sq_ast_attr_val WHERE assetid = '62321' AND contextid = 0; The explain analyze plan: http://explain.depesz.com/s/nWs I'm not sure why it's picking the sq_ast_attr_val_contextid index to do the contextid = 0 check, the other parts (attrid/assetid) are much more selective. If I drop that particular index: http://explain.depesz.com/s/zp All (I hope) relevant postgres info: Centos 5.5 x86_64 running pg8.4.4. Server has 8gig memory. # select name, setting, source from pg_settings where name in ('shared_buffers', 'effective_cache_size', 'work_mem'); name | setting ----------------------+-------- shared_buffers | 262144 effective_cache_size | 655360 work_mem | 32768 All planner options are enabled: # select name, setting, source from pg_settings where name like 'enable_%'; name | setting | source -------------------+---------+--------- enable_bitmapscan | on | default enable_hashagg | on | default enable_hashjoin | on | default enable_indexscan | on | default enable_mergejoin | on | default enable_nestloop | on | default enable_seqscan | on | default enable_sort | on | default enable_tidscan | on | default Any insights welcome - thanks! -- Postgresql & php tutorials http://www.designmagick.com/