Re: forced sequential scan when condition has current_user

Поиск
Список
Период
Сортировка
От Robert Haas
Тема Re: forced sequential scan when condition has current_user
Дата
Msg-id 603c8f071001041359u58159c49k10587c7c46424672@mail.gmail.com
обсуждение исходный текст
Ответ на forced sequential scan when condition has current_user  (Keresztury Balázs <balazs@gaslightmusic.hu>)
Ответы Re: forced sequential scan when condition has current_user
Re: forced sequential scan when condition has current_user
Список pgsql-performance
2010/1/4 Keresztury Balázs <balazs@gaslightmusic.hu>:
> just a small question: is it normal that PostgreSQL 8.4.1 always uses
> sequential scanning on any table when there is a condition having the
> constant "current_user"? Of course there is a btree index set on that table,
> but the DBMS just doesn't want to utilize it. When I replace current_user to
> any string, the planner uses the index normally.
>
> I can demonstrate it with the following simple query:
>
> SELECT psz.kotesszam FROM projekt.projektszervezet psz WHERE
> psz.felhasznalo_id = current_user;
>
> Explain analyze:
>
> "Seq Scan on projektszervezet psz  (cost=0.00..255.07 rows=42 width=9)"
> "  Filter: ((felhasznalo_id)::name = "current_user"())"

You've only got 42 rows in that table - PostgreSQL probably thinks a
sequential scan will be faster.  It might even be right.  The thing
is, PostgreSQL doesn't know at planning time what the value of
current_user() will be, so the plan can't depend on that; the planner
just takes its best shot.  But if you provide a particular value in
the query then it will look at the stats and see what seems to make
the most sense for that particular value.  So using one of the more
commonly-occuring value in the table might produce a sequential scan,
while a less common value might lead to an index scan.

...Robert

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

Предыдущее
От: Madison Kelly
Дата:
Сообщение: Re: DB is slow until DB is reloaded
Следующее
От: Rosser Schwarz
Дата:
Сообщение: Re: DB is slow until DB is reloaded