From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andriy Tkachuk <ant@imt.com.ua>
Cc: pgsql-performance@postgresql.org
Subject: Re: runtime of the same query in function differs on 2 degree!
Date: Thu, 02 Oct 2003 11:30:31 -0400
Message-ID: <27542.1065108631@sss.pgh.pa.us> (raw)
In-Reply-To: <20031002163005.N53420-100000@pool.imt.com.ua>
References: <20031002163005.N53420-100000@pool.imt.com.ua>
Andriy Tkachuk <ant@imt.com.ua> writes:
> What's wrong with planner that executes my query in function?:
> tele=# EXPLAIN analyze select sum(cost) from bills where (parent(user_id) = 6916799 or user_id = 6916799) and dat >= 1062363600 and dat < 10649555
> 99;
In the function case, the planner will not have access to the specific
values that "dat" is being compared to --- it'll see something like
... and dat >= $1 and dat < $2
In this case it has to fall back on a default estimate of how many rows
will be selected, and I suspect it's guessing that a seqscan will be
faster. The trouble is that for a sufficiently large range of d1/d2,
a seqscan *will* be faster.
You might find that the best solution is to use FOR ... EXECUTE and plug
the parameters into the query string so that the planner can see their
values. This will mean re-planning on every function call, but the
advantage is the plan will adapt to the actual range of d1/d2.
regards, tom lane
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: tgl@sss.pgh.pa.us, ant@imt.com.ua
Subject: Re: runtime of the same query in function differs on 2 degree!
In-Reply-To: <27542.1065108631@sss.pgh.pa.us>
References: <20031002163005.N53420-100000@pool.imt.com.ua> <27542.1065108631@sss.pgh.pa.us>
* 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