设置mysql数据包大小_MYSQL 大量插入数据设置数据包max_allowed_packet大小

查询目前大小

show VARIABLES like '%max_allowed_packet%';

修改.my.cnf

[mysqld]

max_allowed_packet=200M

命令行

set global max_allowed_packet = 20*1024*1024*10

/**

* 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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值