pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: Rural Hunter <ruralhunter@gmail.com>
Cc: pgsql-performance@postgresql.org
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Subject: Re: how to change the index chosen in plan?
Date: Fri, 08 Jun 2012 11:39:38 -0500
Message-ID: <4FD1E47A02000025000481FC@gw.wicourts.gov> (raw)
In-Reply-To: <4FD226E6.2060705@gmail.com>
References: <4FD1FF14.9060303@gmail.com>
	<11881.1339164658@sss.pgh.pa.us>
	<4FD20AD0.3040902@gmail.com>
	<4FD1C7C702000025000481E7@gw.wicourts.gov>
	<4FD226E6.2060705@gmail.com>

Rural Hunter <ruralhunter@gmail.com> wrote:
 
>   How can adjust the statistics target?
 
default_statistics_target
 
http://www.postgresql.org/docs/current/interactive/runtime-config-query.html#RUNTIME-CONFIG-QUERY-OT...
 
or ALTER TABLE x ALTER COLUMN y SET STATISTICS n
 
http://www.postgresql.org/docs/current/interactive/sql-altertable.html
 
> 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<now() and b.bid=8 order by a.time limit 10
> There is an index on (a.col1,a.col2,a.time). If I remove the
> order-by clause, I can get the plan as I expected. I think that's
> why postgresql selected that index.
 
Sounds like it expects the sort to be expensive, which means it
probably expects a large number of rows.  An EXPLAIN ANALYZE of the
query with and without the ORDER BY might be instructive.  It would
also help to know what version of PostgreSQL you have and how it is
configured, all of which shows up in the results of the query on
this page:
 
http://wiki.postgresql.org/wiki/Server_Configuration
 
> But still I want the index on b.bid selected first 
> for value 8 since there are only several rows with bid 8. though
> for other normal values there might be several kilo to million
> rows.
 
An EXPLAIN ANALYZE of one where you think the plan is a good choice
might also help.
 
Oh, and just to be sure -- are you actually running queries with the
literals like you show, or are you using prepared statements with
placeholders and plugging the values in after the statement is
prepared?  Sample code, if possible, might help point to or
eliminate issues with a cached plan.  If you're running through a
cached plan, there is no way for it to behave differently based on
the value plugged into the query -- the plan has already been set
before you get to that point.
 
-Kevin



view thread (12+ messages)  latest in thread

Message-ID: <4FD1E47A02000025000481FC@gw.wicourts.gov>
Permalink:  ../4FD1E47A02000025000481FC@gw.wicourts.gov/
Also on:    postgresql.org/message-id/4FD1E47A02000025000481FC@gw.wicourts.gov

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: Kevin.Grittner@wicourts.gov, ruralhunter@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: how to change the index chosen in plan?
  In-Reply-To: <4FD1E47A02000025000481FC@gw.wicourts.gov>
  References: <4FD1FF14.9060303@gmail.com> <11881.1339164658@sss.pgh.pa.us> <4FD20AD0.3040902@gmail.com> <4FD1C7C702000025000481E7@gw.wicourts.gov> <4FD226E6.2060705@gmail.com> <4FD1E47A02000025000481FC@gw.wicourts.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox