博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL查询表中是否存在值的优化
阅读量:6860 次
发布时间:2019-06-26

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

  hot3.png

      经常会碰到这么一个应用:往表里更新数据前先查询一遍被更新的数据存不存在。通常我们的做法是使用select 查询过滤一遍,然后再决定是否更新,怎么更新。在PG库里,除了以上方法外,还有一种更能提升性能的办法,使用perform来代替select。

Example:

Create or replace function test.insert_exist_test(i_id int,i_info text) returns void as$BODY$

--author: kenyon  --created:2012_03_05 --purpose:test insert into a table if exists declare begin perform 1  from test.exists_test where id = i_id; if not found then     insert into test.exists_test(id,info) values (i_id,i_info); return; else   update test.exists_test set info=i_info where id=i_id; return; end if; exception when others then raise exception 'Insert exists_test(id,info) values(%,%) error.',i_id,i_info; return; end; $BODY$ language plpgsql;

使用:

select test.insert_exist_test(1,'kevin');

select test.insert_exist_test(2,'BruceLee');
select test.insert_exist_test(3,'Jacky');
select test.insert_exist_test(1,'kenyon');

转载于:https://my.oschina.net/Kenyon/blog/54376

你可能感兴趣的文章
Ubuntu 削减非 LTS 支持周期
查看>>
_实用的cms企业后台管理模板
查看>>
菜鸟看Redis(一)
查看>>
matplotlib.pyplot.plot()参数详解
查看>>
||PHP||关于=>和->以及::的用法
查看>>
最短路径问题
查看>>
Yii2中定义自己的Widget
查看>>
Aforge.net识别简易数字验证码问题
查看>>
JVM系列二:GC策略&内存申请、对象衰老
查看>>
MySQL 数据库备份策略:全备与增量备份
查看>>
Springboot的热部署
查看>>
Thinking in UML-1-为什么需要UML
查看>>
vs编译obj给delphi用
查看>>
过游戏保护NP或TP的几种方法和思路
查看>>
equals和hashcode为什么要一起重写
查看>>
模态与非模态对话框的问题
查看>>
地对地导弹地对地导弹地对地导弹
查看>>
让div 充满整个body
查看>>
程序员保持快乐活跃的6个好习惯(转)
查看>>
找工作的一些感悟——前端小菜的成长
查看>>