在PostgreSQL里,所有的存储函数需求都可以用PLPGSQL来实现。同时也支持用第三方语言来编写,这个就得看自己哪个方面熟练了。不过要注意的一点是 PLPGSQL的效率怎么着都比其他第三方语言来的高效。------------------------------------华丽丽的分割线------------------------------------CentOS 6.3环境下yum安装PostgreSQL 9.3 http://www.linuxidc.com/Linux/2014-05/101787.htmPostgreSQL缓存详述 http://www.linuxidc.com/Linux/2013-07/87778.htmWindows平台编译 PostgreSQL http://www.linuxidc.com/Linux/2013-05/85114.htmUbuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htmUbuntu上的phppgAdmin安装及配置 http://www.linuxidc.com/Linux/2011-08/40520.htmCentOS平台下安装PostgreSQL9.3 http://www.linuxidc.com/Linux/2014-05/101723.htmPostgreSQL配置Streaming Replication集群 http://www.linuxidc.com/Linux/2014-05/101724.htm------------------------------------华丽丽的分割线------------------------------------比如,简单的插入表的存储函数:CREATE OR REPLACE FUNCTION ytt.insert_plpgsql(f_num integer)
RETURNS void
LANGUAGE plpgsql
AS $ytt$
declare i int := 0;
v_rank int := 0;
v_log_time timestamp;
begin
while i < f_num
loop
v_rank = ceil(random()*100);
v_log_time = now() - "1 day"::interval*ceil(random()*50);
insert into t1 (rank,log_time) values (v_rank,v_log_time);
i = i + 1;
end loop;
end;
$ytt$;现在来插入100W条记录,花费时间大概为27秒。t_girl=# select insert_plpgsql(1000000);
insert_plpgsql
----------------
(1 row)
Time: 27286.668 ms我们改用python 来实现
在编写python 脚本前,确保系统已经载入了plpythonu扩展。t_girl=# dx plpythonu
List of installed extensions
Name | Version | Schema | Description
-----------+---------+------------+------------------------------------------
plpythonu | 1.0 | pg_catalog | PL/PythonU untrusted procedural language
(1 row)以下是函数体:CREATE OR REPLACE FUNCTION ytt.insert_py(f_num integer)
RETURNS void
LANGUAGE plpythonu
AS $ytt$
import datetime
import random
i = 0
while i < f_num:
v_rank = random.randrange(0,100)
v_log_time = datetime.datetime.now() - datetime.timedelta(days=random.randrange(0,50))
query0 = "insert into ytt.t1 (rank,log_time) values (" + str(v_rank) + ","" + str(v_log_time)+ "")"
plpy.execute(query0)
i += 1
$ytt$;
更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-06/102568p2.htm
MySQL和PostgreSQL 导入数据对比Oracle 11g 单实例打11.2.0.3.4 P14275605 PSU补丁相关资讯 PostgreSQL函数 PostgreSQL存储函数
- PostgreSQL avg()函数 (02月26日)
- 使用C编写的动态链接库为 (11/24/2012 11:08:46)
| - PostgreSQL的行转列函数使用一例 (10/15/2013 14:09:56)
|
本文评论 查看全部评论 (0)