PostgreSQL numeric类型上的算术运算比整数类型或者浮点数类型要慢很多

准备两个function,一个使用integer计算,一个使用numeric计算

使用Integer计算:

create function compute_integer()returns integer as $$
declare
   num integer:=0;
begin
   for i in 1..10000 loop
      num=num+i;
   end loop;
   return num;
end;
$$language plpgsql;

使用nemeric计算:

create function compute_numeric)returns numeric as $$
declare
   num numeric:=0.0;
begin
   for i in 1..10000 loop
      num=num+i;
   end loop;
   return num;
end;
$$language plpgsql;

观察运算结果:

1. integer

postgres=# explain analyze select compute_integer();
                                     QUERY PLAN                                     
------------------------------------------------------------------------------------
 Result  (cost=0.00..0.26 rows=1 width=0) (actual time=6.816..6.817 rows=1 loops=1)
 Total runtime: 6.829 ms
(2 rows)


2. numeric

postgres=# explain analyze select compute_numeric();
                                      QUERY PLAN                                      
--------------------------------------------------------------------------------------
 Result  (cost=0.00..0.26 rows=1 width=0) (actual time=14.011..14.012 rows=1 loops=1)
 Total runtime: 14.029 ms
(2 rows)

后者的计算速度还是慢一些的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值