mysql begin end 批量更新_mysql 批量更新

mysql 批量更新共有以下四种办法

1、 replace into 批量更新

replace into 表名l (id,字段1) values (1,'2'),(2,'3'),...(x,'y');

2、insert into ...on duplicate key update批量更新

insert into 表名l (id,字段1) values (1,'2'),(2,'3'),...(x,'y') on duplicate key update 字段1=values(字段1);

3.创建临时表,先更新临时表,然后从临时表中update

create temporary table tmp(id int(4) primary key,dr varchar(50));

insert into tmp values (0,'gone'), (1,'xx'),...(m,'yy');

update test_tbl, tmp set test_tbl.dr=tmp.dr where test_tbl.id=tmp.id;

注意:这种方法需要用户有temporary 表的create 权限。

4、使用mysql 自带的语句构建批量更新

mysql 实现批量 可以用点小技巧来实现:

48304ba5e6f9fe08f3fa1abda7d326ab.png

UPDATE yoiurtable

SET dingdan = CASE id

WHEN 1 THEN 3

WHEN 2 THEN 4

WHEN 3 THEN 5

END

WHERE id IN (1,2,3)

48304ba5e6f9fe08f3fa1abda7d326ab.png

这句sql 的意思是,更新dingdan 字段,如果id=1 则dingdan 的值为3,如果id=2 则dingdan 的值为4……

where部分不影响代码的执行,但是会提高sql执行的效率。确保sql语句仅执行需要修改的行数,这里只有3条数据进行更新,而where子句确保只有3行数据执行。

例子:

UPDATE book

SET Author= CASEid

WHEN1 THEN '黄飞鸿'WHEN2 THEN '方世玉'WHEN3 THEN '洪熙官'

ENDWHERE id IN (1,2,3)

如果更新多个值的话,只需要稍加修改:

UPDATE categories

SET dingdan= CASEid

WHEN1 THEN 3WHEN2 THEN 4WHEN3 THEN 5

END,title= CASEid

WHEN1 THEN 'New Title 1'WHEN2 THEN 'New Title 2'WHEN3 THEN 'New Title 3'

ENDWHERE id IN (1,2,3)

到这里,已经完成一条mysql语句更新多条记录了。

php中用数组形式赋值批量更新的代码:

$display_order = array(1 => 4,

2 => 1,

3 => 2,

4 => 3,

5 => 9,

6 => 5,

7 => 8,

8 => 9);$ids = implode(',', array_keys($display_order));$sql = "UPDATE categories SET display_order = CASE id ";foreach ($display_order as $id => $ordinal) {$sql .= sprintf("WHEN %d THEN %d ", $id, $ordinal);

}$sql .= "END WHERE id IN ($ids)";echo $sql;

这个例子,有8条记录进行更新。代码也很容易理解,你学会了吗

更新 100000条数据的性能就测试结果来看,测试当时使用replace into性能较好。

replace into  和 insert into on duplicate key update的不同在于:

replace into 操作本质是对重复的记录先delete 后insert,如果更新的字段不全会将缺失的字段置为缺省值,用这个要悠着点!否则不小心清空大量数据可不是闹着玩的!!!

insert into 则是只update重复记录,不会改变其它字段。

相同点:

(1)没有key的时候,replace与insert .. on deplicate udpate相同。

(2)有key的时候,都保留主键值,并且auto_increment自动+1。

不同点

有key的时候,replace是delete老记录,而录入新的记录,所以原有的所有记录会被清除,这个时候,如果replace语句的字段不全的话,有些原有的比如例子中c字段的值会被自动填充为默认值。

而insert .. deplicate update则只执行update标记之后的sql,从表象上来看相当于一个简单的update语句。

但是实际上,根据我推测,如果是简单的update语句,auto_increment不会+1,应该也是先delete,再insert的操作,只是在insert的过程中保留除update后面字段以外的所有字段的值。

所以两者的区别只有一个,insert .. on deplicate udpate保留了所有字段的旧值,再覆盖然后一起insert进去,而replace没有保留旧值,直接删除再insert新值。

从底层执行效率上来讲,replace要比insert .. on deplicate update效率要高,但是在写replace的时候,字段要写全,防止老的字段数据被删除。

以下代码作为简单测试

* CREATE TABLE `demo` (

* `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

* `data` varchar(255) NOT NULL,

* PRIMARY KEY (`id`)

* ) ENGINE=InnoDB DEFAULT CHARSET=utf8;*/

/** 连接数据库*/

$dsn = 'mysql:host=127.0.0.1;dbname=testdb;';$user = 'root';$password = '123456';try{$dbh = new PDO( $dsn , $user , $password);

}catch ( \Exception $e) {throw new \Exception( $e->getMessage () );

}/** 调整 Mysql Server接受的数据包*/

$dbh->exec ( "set global max_allowed_packet = 2*1024*1024*1024");/** 测试记录总数*/

$rowsCount = 10000;/** 1 普通方式,逐行写入测试数据*/

$time_start = microtime ( true);try{for ( $i = 1 ; $i <= $rowsCount ; $i ++) {$sql = "insert into demo( data ) value ('" . mt_rand ( 10000000 , 99999999 ) . "')";$dbh->exec ( $sql);

}

}catch ( \Exception $e) {throw new \Exception( $e->getMessage () );

}$time_end = microtime ( true);$time = $time_end - $time_start;echo "1 Execution time: {$time} s" . PHP_EOL;/** 2 事务*/

$time_start = microtime ( true);$dbh->beginTransaction ();try{for ( $i = 1 ; $i <= $rowsCount ; $i ++) {$sql = "insert into demo(data) value ('" . mt_rand ( 10000000 , 99999999 ) . "')";$dbh->exec ( $sql);

}$dbh->commit ();

}catch ( \Exception $e) {$dbh->rollBack ();throw new \Exception( $e->getMessage () );

}$time_end = microtime ( true);$time = $time_end - $time_start;echo "2 Execution time: {$time} s" . PHP_EOL;/** 3 值合并方式,values (...),(...)*/

$time_start = microtime ( true);try{$sql = "insert into demo( data ) values ";for ( $i = 1 ; $i <= $rowsCount ; $i ++) {$sql .= "('" . mt_rand ( 10000000 , 99999999 ) . "'),";

}$dbh->exec ( rtrim ( $sql , ',') );

}catch ( \Exception $e) {throw new \Exception( $e->getMessage () );

}$time_end = microtime ( true);$time = $time_end - $time_start;echo "3 Execution time: {$time} s " . PHP_EOL;/** 4 合并加事务*/

$time_start = microtime ( true);$dbh->beginTransaction ();try{$sql = "insert into demo( data ) values ";for ( $i = 1 ; $i <= $rowsCount ; $i ++) {$sql .= "('" . mt_rand ( 10000000 , 99999999 ) . "'),";

}$dbh->exec ( rtrim ( $sql , ',') );$dbh->commit ();

}catch ( \Exception $e) {$dbh->rollBack ();throw new \Exception( $e->getMessage () );

}$time_end = microtime ( true);$time = $time_end - $time_start;echo "4 Execution time : {$time} s " . PHP_EOL;

输出结果:10w数据:

1 Execution time: 269.58895611763s2 Execution time: 25.353534936905s3 Execution time: 1.2171220779419s4 Execution time : 1.1611158847809s

50w数据:

1 Execution time: 1358.3988881111s2 Execution time: 119.97599983215s3 Execution time: 6.7320001125336s4 Execution time : 6.4200000762939 s

总结:

在数据量大的时候,进行数据合并形式"values(...),(...),...",如果有数据完整性的需求,采用事务,相对来说能好些.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值