Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 3CD05D84656 for ; Fri, 8 Jun 2012 13:23:25 -0300 (ADT) Received: from mail-pb0-f46.google.com ([209.85.160.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Sd1y8-0003lG-0Z for pgsql-performance@postgresql.org; Fri, 08 Jun 2012 16:23:24 +0000 Received: by pbbrp8 with SMTP id rp8so2743179pbb.19 for ; Fri, 08 Jun 2012 09:23:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=ppso+oaylXnlHdhZgSjCsS9J2xzBuBlf3ajgqXN2/uM=; b=juEVuGWqg9bTCl7jHPh/b84b3MLfQP4ihJtD8O+mI8TqlmNyjJWy6uOIvokpdFgpps 27a9mTm0e7QAJUNxivP/gHIN61e08OAR0Ac8kZzL6+gvt2FMkU+3oGq++zxY0QRDf4SJ vJFtIqsTcN+ao3q8F4P0n8dzRZSkshqRldpezD6pQD9vzHwD5MMX7H+Ln+YtQaqlCAQX RPUme1SwD+G+mY5V5bWvy9P1vd0PDzXNfPWlQhvHOpVpl7iWeS+i3tyiUGDlefOeB4LI DRfgL2QWIKjtGYHNbZ1n13xUrnRxekjayq83Rb3HlZXjTWZps/SzpI8fHVvEeuMj0dqn fkxQ== Received: by 10.68.201.2 with SMTP id jw2mr3680210pbc.106.1339172591058; Fri, 08 Jun 2012 09:23:11 -0700 (PDT) Received: from [192.168.11.3] ([114.95.165.117]) by mx.google.com with ESMTPS id pd9sm8306622pbc.26.2012.06.08.09.23.07 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 08 Jun 2012 09:23:09 -0700 (PDT) Message-ID: <4FD226E6.2060705@gmail.com> Date: Sat, 09 Jun 2012 00:23:02 +0800 From: Rural Hunter User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20120604 Thunderbird/13.0 MIME-Version: 1.0 To: Kevin Grittner CC: Tom Lane , pgsql-performance@postgresql.org Subject: Re: how to change the index chosen in plan? References: <4FD1FF14.9060303@gmail.com> <11881.1339164658@sss.pgh.pa.us> <4FD20AD0.3040902@gmail.com> <4FD1C7C702000025000481E7@gw.wicourts.gov> In-Reply-To: <4FD1C7C702000025000481E7@gw.wicourts.gov> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201206/46 X-Sequence-Number: 47072 Hi Kevin, Thanks for your detailed explanation. 于 2012/6/8 22:37, Kevin Grittner 写道: > Rural Hunter wrote: >> 于2012年6月8日 22:10:58,Tom Lane写到: >>> Rural Hunter writes: >>>> I have a query like this: >>>> select a.* from a inner join b on a.aid=b.aid where a.col1=33 >>>> a.col2=44 and b.bid=8 >>>> postgresql selected the index on a.col1 then selected the index >>>> on b.bid. But in my situation, I know that the query will be >>>> faster if it chose the index on b.bid first since there are only >>>> a few rows with value 8. >>> If you know that and the planner doesn't, maybe ANALYZE is called >>> for. >>> >> No, it's not the analyze problem. > > So you ran ANALYZE and retried? If not, please do. Yes, I did. > >> For some other values on b.bid such as 9, 10, the plan is fine >> since there a a lot of rows in table b for them. > > So it uses the same plan regardless of the number of rows in table b > for the value? yes. > That sure *sounds* like you need to run ANALYZE, > possibly after adjusting the statistics target for a column or two. How can adjust the statistics target? > >> But for some specific values such as 8 I want the plan changed. > > If you approach it from that line of thought, you will be unlikely > to reach a good long-term solution. PostgreSQL has a costing model > to determine which plan is expected to be cheapest (fastest). This > is based on statistics gathered during ANALYZE and on costing > factors. Generally, if it's not choosing the fastest plan, you > aren't running ANALYZE frequently enough or with a fine-grained > enough statistics target _or_ you need to adjust your costing > factors to better model your actual costs. > > You haven't given us a lot of clues about which it is that you need > to do, but there is *some* suggestion that you need to ANALYZE. If > you *try* that and it doesn't solve your problem, please read this > page and provide more information: > > http://wiki.postgresql.org/wiki/SlowQueryQuestions Sorry the actual tables and query are very complicated so I just simplified the problem with my understanding. I rechecked the query and found it should be simplified like this: select a.* from a inner join b on a.aid=b.aid where a.col1=33 and a.col2=44 and a.time > -Kevin >