甲骨文和PostgreSQL发展(19)-管行

  

甲骨文的PL/SQL提供了管线式表函数特性用于把多行数据返回到调用者,可以有效的提升性能。   
在PostgreSQL中,可以通过在函数中利用套或者返回下一个来实现。   

  <强>甲骨文   
创建数据表,插入数据   <前>   <代码> TEST-orcl@DESKTOP-V430TU3> drop table t_piperow;   删除表t_piperow   *   误差在1号线:   ora - 00942:表或视图不存在   TEST-orcl@DESKTOP-V430TU3>创建表t_piperow (id int, c1时间戳,c2 varchar2 (20), c3号码);   创建表。   TEST-orcl@DESKTOP-V430TU3>   TEST-orcl@DESKTOP-V430TU3>插入t_piperow (id, c1, c2, c3)   2选择rownum sysdate '测试' | | rownum,从123455.55 dba_objects rownum & lt;=500;   500行。   TEST-orcl@DESKTOP-V430TU3>提交;   提交完成。   TEST-orcl@DESKTOP-V430TU3>      

创建类型   <前>   <代码> TEST-orcl@DESKTOP-V430TU3>创建或替换类型rec_t_piperow对象(id int, c1时间戳,c2 varchar2 (20), c3号码);   2/类型创建的。   TEST-orcl@DESKTOP-V430TU3>创建或替换类型type_t_piperow rec_t_piperow表;   2/类型创建的。      

函数实现

  <前>   <代码> TEST-orcl@DESKTOP-V430TU3>创建或替换函数func_piperow_demo1返回type_t_piperow管线式   2开始   3为矩形(select * from t_piperow)循环   4管行(rec_t_piperow (rec.id、rec.c1 rec.c2, rec.c3));   5结束循环;   6返回;   7结束;   8/函数创建的。      

查询数据

  <前>   <代码> TEST-orcl@DESKTOP-V430TU3> 9999999999999.9999 c3列格式   从表TEST-orcl@DESKTOP-V430TU3> select * (func_piperow_demo1 ()) rownum & lt;5;   ID C1 C2 C3   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   1 31-OCT-19 10.50.38.0 test1 123455.5500   00000年我   2 31-OCT-19 10.50.38.0 test2 123455.5500   00000年我   3 31-OCT-19 10.50.38.0 test3 123455.5500   00000年我   4 31-OCT-19 10.50.38.0 test4 123455.5500   00000年我      

  <强> PostgreSQL   
下面来看看PG的实现,创建表,插入数据   <前>   <代码>(本地):5432 pg12@testdb=#删除表如果存在t_piperow;   删除表   时间:5.255毫秒   (本地):5432 pg12@testdb=#创建表t_piperow (id int, c1时间戳,c2 varchar (20), c3浮动);   创建表   时间:4.711毫秒   (本地):5432 pg12@testdb=#   (本地):5432 pg12@testdb=#插入t_piperow (id, c1, c2, c3)   pg12@testdb - #选择x,现在(),“测试”| | x 123455.55 generate_series x (1500);   插入0 500   时间:10.183毫秒   (本地):5432 pg12@testdb=#   (本地):5432 pg12@testdb=#      

函数实现   
第一种方式,使用SQL:   <前>   <代码>(本地):5432 pg12@testdb=#创建或替换函数func_piperow_demo1()返回套PUBLIC.t_piperow   pg12@testdb——#   pg12@testdb - # $ $   从t_piperow pg12@testdb $ # SELECT *;   pg12@testdb $ # $ $   pg12@testdb - #语言SQL;   创建函数   时间:1.341毫秒   (本地):5432 pg12@testdb=#   (本地):5432 pg12@testdb=#选择func_piperow_demo1()限制5;   func_piperow_demo1   --------------------------------------------------   (1、“2019-10-31”11:09:27.222996 test1, 123455.55)   (2“2019-10-31”11:09:27.222996 test2, 123455.55)   (3“2019-10-31”11:09:27.222996 test3, 123455.55)   (4“2019-10-31”11:09:27.222996 test4, 123455.55)   (5“2019-10-31”11:09:27.222996 test5, 123455.55)   (5行)   时间:1.038毫秒   (本地):5432 pg12@testdb=#      

第二种方式,使用PL/pgSQL,返回查询

  <前>   <代码>(本地):5432 pg12@testdb=#创建或替换函数func_piperow_demo2()返回套PUBLIC.t_piperow   pg12@testdb——#   pg12@testdb - # $ $   pg12@testdb $ #开始   从t_piperow pg12@testdb $ #返回查询SELECT *;   pg12@testdb $ #结束;   pg12@testdb $ # $ $   pg12@testdb - #语言PLPGSQL;   创建函数   时间:3.850毫秒   (本地):5432 pg12@testdb=#选择func_piperow_demo2()限制5;   func_piperow_demo2   --------------------------------------------------   (1、“2019-10-31”11:09:27.222996 test1, 123455.55)   (2“2019-10-31”11:09:27.222996 test2, 123455.55)   (3“2019-10-31”11:09:27.222996 test3, 123455.55)   (4“2019-10-31”11:09:27.222996 test4, 123455.55)   (5“2019-10-31”11:09:27.222996 test5, 123455.55)   (5行)   时间:5.645毫秒   (本地):5432 pg12@testdb=#   

甲骨文和PostgreSQL发展(19)-管行