博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL 如何实现批量更新、删除、插入
阅读量:7174 次
发布时间:2019-06-29

本文共 2679 字,大约阅读时间需要 8 分钟。

标签

PostgreSQL , 批量 , batch , insert , update , delete , copy


背景

如何一次插入多条记录?

如何一次更新多条记录?

如何一次批量删除多条记录?

批量操作可以减少数据库与应用程序的交互次数,提高数据处理的吞吐量。

批量插入

批量插入1

使用insert into ... select的方法

postgres=# insert into tbl1 (id, info ,crt_time) select generate_series(1,10000),'test',now();    INSERT 0 10000    postgres=# select count(*) from tbl1;     count     -------     10001    (1 row)

批量插入2

使用values(),(),...();的方法

postgres=# insert into tbl1 (id,info,crt_time) values (1,'test',now()), (2,'test2',now()), (3,'test3',now());    INSERT 0 3

批量插入3

BEGIN; ...多条insert...; END;

严格来说,这应该不属于批量,但是可以减少事务提交时的同步等待。同样有性能提升的效果。

postgres=# begin;    BEGIN    postgres=# insert into tbl1 (id,info,crt_time) values (1,'test',now());    INSERT 0 1    postgres=# insert into tbl1 (id,info,crt_time) values (2,'test2',now());    INSERT 0 1    postgres=# insert into tbl1 (id,info,crt_time) values (3,'test3',now());    INSERT 0 1    postgres=# end;    COMMIT

批量插入4

copy

copy协议与insert协议不一样,更加精简,插入效率高。

test03=# \d test                  Table "public.test"    Column  |            Type             | Modifiers   ----------+-----------------------------+-----------   id       | integer                     | not null   info     | text                        |    crt_time | timestamp without time zone |   Indexes:      "test_pkey" PRIMARY KEY, btree (id)    test03=# copy test from stdin;  Enter data to be copied followed by a newline.  End with a backslash and a period on a line by itself.  >> 8    'test'  '2017-01-01'  >> 9    'test9' '2017-02-02'  >> \.  COPY 2

不同的语言驱动,对应的COPY接口不一样。

参考

批量更新

批量更新

test03=# update test set info=tmp.info from (values (1,'new1'),(2,'new2'),(6,'new6')) as tmp (id,info) where test.id=tmp.id;  UPDATE 3  test03=# select * from test;   id |     info     |          crt_time            ----+--------------+----------------------------    3 | hello        | 2017-04-24 15:31:49.14291    4 | digoal0123   | 2017-04-24 15:42:50.912887    5 | hello digoal | 2017-04-24 15:57:29.622045    1 | new1         | 2017-04-24 15:58:55.610072    2 | new2         | 2017-04-24 15:28:20.37392    6 | new6         | 2017-04-24 15:59:12.265915  (6 rows)

批量删除

批量删除

test03=# delete from test using (values (3),(4),(5)) as tmp(id) where test.id=tmp.id;  DELETE 3  test03=# select * from test;   id |  info   |          crt_time            ----+---------+----------------------------    1 | new1    | 2017-04-24 15:58:55.610072    2 | new2    | 2017-04-24 15:28:20.37392    6 | new6    | 2017-04-24 15:59:12.265915

如果要清除全表,建议使用truncate

test03=# set lock_timeout = '1s';SETtest03=# truncate test;  TRUNCATE TABLE  test03=# select * from test;   id | info | crt_time   ----+------+----------  (0 rows)

转载地址:http://phdzm.baihongyu.com/

你可能感兴趣的文章
去除数组中除第一个负数的所有负数
查看>>
哪些因素导致Python运行效率低?python入门编程
查看>>
[Python]第一个爬虫练习
查看>>
提高Python代码效率的方法
查看>>
zabbix使用msmtp&&mutt搭建邮件告警服务
查看>>
USB抓包工具--Bus Hound的使用方法详解
查看>>
location of android sdk has not been setup in the preference
查看>>
Centos7 二进制安装mysql5.7
查看>>
Centos7之Nginx的两种工作模式
查看>>
Java之品优购课程讲义_day18(3)
查看>>
rpm,yum,权限
查看>>
更新yum到 163
查看>>
Office 2019 & Office 2016 下载地址
查看>>
tomcat应用转到weblogic上时的问题
查看>>
国外程序员是如何准备面试的
查看>>
Zookeeper监控之——node-zk-browser
查看>>
我的友情链接
查看>>
10个最酷的linux单行命令
查看>>
myeclipse 10 在mac retina 屏幕下显示字体模糊解决方法
查看>>
创建自定义的指令
查看>>