---
title: "update多列更新"
author: "bestxiaok"
date: "2012-04-05T16:06:33Z"
tags: ["Update"]
categories: ["数据库"]
slug: "update"
draft: true
lastmod: "2012-04-05T16:06:33Z"
id: 1474592
url: "https://bestxiaok.iteye.com/blog/1474592"
---

再ETL的时候，经常会遇到列转换的问题，再对照转换中出现多列更新的时候，一下是我总结的一些：
当update多列时有如下几种选择
1.)教科书式写法

update t_table a

    set f1=(select f1 from testz b where a.id=b.id),

           f2=(select f2 from testz b where a.id=b.id),

           f3=(select f3 from testz b where a.id=b.id)

    where id=2;
2.)教科书变种

update t_table a

    set (f1,f2,f3)=(select f1,f2,f3 from testz b where a.id=b.id)

    where id=2;

虽然道理和方法1一样,却省了不少事

3.)另类nest table写法

update (select f1,f2,f3 from t_table where id=2)

    set (f1,f2,f3)=(select f1,f2,f3 from testz b where id=2);

此方法虽然比2的代码量要多一些,但是没有表连接,如果两个表都在id列有主键,速度应该较方法1和方法2快一些.

说明:update是一个耗回滚段,耗重做日志,耗时间,耗成本的操作,尤其是大表的多列update,如有必要应比较其与重新建表的效率.
