postgres的自定义函数没有上下文切换开销?

[复制链接]
查看11 | 回复1 | 2012-5-21 10:19:41 | 显示全部楼层 |阅读模式
test=# \timing
Timing is on.
--用内置round函数
test=# select avg(round(a)) from generate_series(1,1000000) a;
avg
----------
500000.5
(1 row)
Time: 208.583 ms
test=# select avg(round(a)) from generate_series(1,10000000) a;
avg
-----------
5000000.5
(1 row)
Time: 1971.878 ms (00:01.972)
--用自定义函数,实际再调用round函数
test=# create or replace function myround(a int) returns double precision
test-# as $$
test$# select round(a);
test$# $$
test-# language sql;
CREATE FUNCTION
Time: 2.227 ms
test=# select avg(myround(a)) from generate_series(1,1000000) a;
avg
----------
500000.5
(1 row)
Time: 200.740 ms
test=# select avg(myround(a)) from generate_series(1,10000000) a;
avg
-----------
5000000.5
(1 row)
Time: 2048.413 ms (00:02.048)


回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
系统内置的是3个重载函数
test=# \df round

List of functions
Schema | Name| Result data type | Argument data types | Type
------------+-------+------------------+---------------------+------
pg_catalog | round | double precision | double precision| func
pg_catalog | round | numeric
| numeric
| func
pg_catalog | round | numeric
| numeric, integer| func
(3 rows)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行