Re: Optimizer Parameters

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Optimizer Parameters
Дата
Msg-id 18388.1058042770@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Optimizer Parameters  (Martin Foster <martin@ethereal-realms.org>)
Ответы Re: Optimizer Parameters  (Martin Foster <martin@ethereal-realms.org>)
Список pgsql-novice
Martin Foster <martin@ethereal-realms.org> writes:
> force Postgres into using Indexes when available.    So I changed the
> following two lines in the .conf file:

>     enable_seqscan = false
>     enable_nestloop = false

> This was recommended in the documentation,

Where would you say that setting those off in the config file is
"recommended"?

> Now how sane is it to keep those options turned off?

It isn't.  If you have to force them off for a particular query, do
so right before you issue that query, and turn them on again after.
Turning them off globally is sure to cause you pain later.

> And any
> way to have the planner quiet guessing tens of thousands of rows will be
> return when there are at most hundred?

>     AND Po.PostTimestamp > (LOCALTIMESTAMP - INTERVAL '10 minutes')
>     AND Po.PuppetName IS NOT NULL

>                            ->  Seq Scan on post po  (cost=0.00..14369.84 rows=40513 width=41) (actual
time=2820.88..2826.30rows=392 loops=1) 
>                                  Filter: ((posttimestamp > (('now'::text)::timestamp(6) without time zone -
'00:10'::interval))AND (puppetname IS NOT NULL)) 

Not with that coding technique; "LOCALTIMESTAMP - INTERVAL '10 minutes'"
isn't a constant and so the planner can't look at its statistics to
see that only a small part of the table will be selected.

There are two standard workarounds for this:

1. Do the timestamp arithmetic on the client side, so that the query
you send the backend has a simple constant:

    ... AND Po.PostTimestamp > '2003-07-12 16:27'

2. Create a function that is falsely marked immutable, viz:

create function ago(interval) returns timestamp without time zone as
'select localtimestamp - $1' language sql immutable strict;

    ... AND Po.PostTimestamp > ago('10 minutes')

Because the function is marked immutable, the planner will reduce
"ago('10 minutes')" to a constant on sight, and then use that value
for planning purposes.  This technique can cause problems, since
in some contexts the reduction will occur prematurely, but as long
as you only use ago() in interactively-issued queries it works okay.

            regards, tom lane

В списке pgsql-novice по дате отправления:

Предыдущее
От: "Grant Rhodes"
Дата:
Сообщение: Problem with psql interface
Следующее
От: Ron Johnson
Дата:
Сообщение: Re: Problem with psql interface