Re: Query is fast and function is slow

Поиск
Список
Период
Сортировка
От Richard Ray
Тема Re: Query is fast and function is slow
Дата
Msg-id Pine.LNX.4.64.0612070826040.16909@rray.drdc.mstc.ms.gov
обсуждение исходный текст
Ответ на Re: Query is fast and function is slow  (Thomas Pundt <mlists@rp-online.de>)
Ответы Re: Query is fast and function is slow  (Thomas Pundt <mlists@rp-online.de>)
Re: Query is fast and function is slow  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-sql
On Thu, 7 Dec 2006, Thomas Pundt wrote:

> Hi,
>
> On Wednesday 06 December 2006 16:44, Richard Ray wrote:
> | select count(*) from documents where doc_num = '106973821'  and (select
> | bit_or(group_access) from mda_groups where group_name in (select groname
> | from pg_user,pg_group where usename =  'bbob'  and usesysid = any(grolist)
> | and (groname ~ '.*owner$' or groname = 'admin'))) & access >
> | '0'::bit(100);
> |
> | returns very fast
> |
> | If I create function
> |
> | create or replace function check_for_update_permission(text,text) returns
> | boolean as '
> | declare
> |    doc_number alias for $1;
> |    user alias for $2;
> |    doc_count integer;
> | begin
> ...
> | end;
> | ' language 'plpgsql';
> |
> |
> | and run "select check_for_update_permission('106973821','bbob');"
> | it returns the correct info but takes several minutes
> | Would someone please enlighten me.
> | Can you do something like explain analyze on a function
>
> Just a guess: is the column "doc_num" really of type text? Maybe using "text"
> in the function lets the planner choose a sequential scan?

Actually "doc_num" is char(9)
I changed text to char(9) and got same slow results

>
> I'd try putting a "raise notice '%', explain analyze ..." statement into the
> function and check the log file.

It appears that the function is not using the index
The table documents has a index on doc_num and doc_num is a unique value
dcc=# explain analyze select doc_num from documents where doc_num = 
'106973821';                                                         QUERY PLAN

-----------------------------------------------------------------------------------------------------------------------------
IndexScan using documents_pkey on documents  (cost=0.00..5.48 rows=1 
 
width=13) (actual time=37.475..37.481 rows=1 loops=1)   Index Cond: (doc_num = '106973821'::bpchar) Total runtime:
37.535ms
 
(3 rows)

dcc=#

But this same statement in a function takes several minutes;

My SQL knowledge is pitiful so would you explain how to use
"explain analyze" in the function

I get errors when I try to load the file with
raise notice  ''%'',explain analyze select doc_num from documents where 
doc_num = doc_number;

dcc=# \i 
/src/check_for_update_permission
psql:/src/check_for_update_permission:52: 
ERROR:  syntax error at or near "analyze" at character 16
QUERY:  SELECT explain analyze select doc_num from documents where doc_num 
=  $1
CONTEXT:  SQL statement in PL/PgSQL function "check_for_update_permission" 
near line 18
psql:/src/check_for_update_permission:52: 
LINE 1: SELECT explain analyze select doc_num from documents where d...
psql:/src/check_for_update_permission:52: 
^
dcc=#

>
> Ciao,
> Thomas
>
>


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

Предыдущее
От: Thomas Pundt
Дата:
Сообщение: Re: Query is fast and function is slow
Следующее
От: Thomas Pundt
Дата:
Сообщение: Re: Query is fast and function is slow