joinupdatevssub-selectsupdate
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了joinupdatevssub-selectsupdate,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3757字,纯文字阅读大概需要6分钟。
内容图文
开发说使用多表关联进行更新的时候发现没有正确的更新记录(事后发现是条件问题),之前一直没怎么使用关联更新,这次看了下,发现了个问题,当返回多行记录的时候并不会像传统的子查询更新那样报错,而是随机选择一个记录进行更新( 貌似最后一个? ) 因此
开发说使用多表关联进行更新的时候发现没有正确的更新记录(事后发现是条件问题),之前一直没怎么使用关联更新,这次看了下,发现了个问题,当返回多行记录的时候并不会像传统的子查询更新那样报错,而是随机选择一个记录进行更新(貌似最后一个?)
因此,虽然关联更新会快很多,但是要考虑的这个不确定带来的隐患。
gtlions=# create table joinupdate1(id int,name varchar(20)); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table. HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. CREATE TABLE gtlions=# create table joinupdate2(id int,name varchar(20)); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table. HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. CREATE TABLE gtlions=# insert into joinupdate1 values(1,'a'); INSERT 0 1 gtlions=# insert into joinupdate1 values(2,'b'); INSERT 0 1 gtlions=# insert into joinupdate2 values(1,'b'); INSERT 0 1 gtlions=# insert into joinupdate2 values(2,'b'); INSERT 0 1 gtlions=# truncate table joinupdate2; TRUNCATE TABLE gtlions=# insert into joinupdate2 values(1,'c'); INSERT 0 1 gtlions=# insert into joinupdate2 values(2,'d'); INSERT 0 1 gtlions=# select * from joinupdate1; id | name ----+------ 1 | a 2 | b (2 rows) gtlions=# select * from joinupdate2; id | name ----+------ 1 | c 2 | d (2 rows) gtlions=# begin; BEGIN gtlions=# update joinupdate1 set name=(select name from joinupdate2 where joinupdate1.id=joinupdate2.id); UPDATE 2 gtlions=# select * from joinupdate1; id | name ----+------ 1 | c 2 | d (2 rows) gtlions=# rollback; ROLLBACK gtlions=# end; WARNING: there is no transaction in progress COMMIT gtlions=# select * from joinupdate1; id | name ----+------ 1 | a 2 | b (2 rows) gtlions=# begin; BEGIN gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id; UPDATE 2 gtlions=# select * from joinupdate1; id | name ----+------ 1 | c 2 | d (2 rows) gtlions=# rollback; ROLLBACK gtlions=# insert into joinupdate2 values(1,'e'); INSERT 0 1 gtlions=# begin; BEGIN gtlions=# update joinupdate1 set name=(select name from joinupdate2 where joinupdate1.id=joinupdate2.id); ERROR: more than one row returned by a subquery used as an expression (seg0 slice2 h1:40000 pid=14123) gtlions=# rollback; ROLLBACK gtlions=# begin; BEGIN gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id; UPDATE 2 gtlions=# select * from joinupdate1; id | name ----+------ 1 | e 2 | d (2 rows) gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id; UPDATE 2 gtlions=# select * from joinupdate1; id | name ----+------ 1 | e 2 | d (2 rows) gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id; UPDATE 2 gtlions=# select * from joinupdate1; id | name ----+------ 1 | e 2 | d (2 rows) gtlions=# insert into joinupdate2 values(1,'f'); INSERT 0 1 gtlions=# update joinupdate1 set name=joinupdate2.name from joinupdate2 where joinupdate1.id=joinupdate2.id; UPDATE 2 gtlions=# select * from joinupdate1; id | name ----+------ 1 | f 2 | d (2 rows) gtlions=# rollback; ROLLBACK
-EOF-
内容总结
以上是互联网集市为您收集整理的joinupdatevssub-selectsupdate全部内容,希望文章能够帮你解决joinupdatevssub-selectsupdate所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。