加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 17:22:57 所属栏目:百科 来源:网络整理
导读:准备两个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;

准备两个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)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读