
6

在一个事务内插入大量数据会怎么样
source link: https://www.v2ex.com/t/800340
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

mysql,讨论下面两种场景
开启事务
插入 1000 万条数据
提交事务
开启事务
插入 1000 万条数据
回滚事务
会不会导致数据库挂掉。
如何做到安全插入大批量数据进数据库
17 条回复 • 2021-09-08 00:16:43 +08:00
haoliang 12 小时 59 分钟前
同好奇,搜索了下。
参考这里: https://mariadb.com/kb/en/how-to-quickly-insert-data-into-mariadb/#using-big-transactions
> When doing many inserts in a row, you should wrap them with BEGIN / END to avoid doing a full transaction
> (which includes a disk sync) for every row. For example, doing a begin/end every 1000 inserts will speed up your > inserts by almost 1000 times.
> ...
> The reason why you may want to have many BEGIN/END statements instead of just one is that the former will use up less transaction log space.
然后发现了一个 [transaction log]( https://mariadb.com/kb/en/innodb-redo-log/ ),估计事务的一部分代价吧
参考这里: https://mariadb.com/kb/en/how-to-quickly-insert-data-into-mariadb/#using-big-transactions
> When doing many inserts in a row, you should wrap them with BEGIN / END to avoid doing a full transaction
> (which includes a disk sync) for every row. For example, doing a begin/end every 1000 inserts will speed up your > inserts by almost 1000 times.
> ...
> The reason why you may want to have many BEGIN/END statements instead of just one is that the former will use up less transaction log space.
然后发现了一个 [transaction log]( https://mariadb.com/kb/en/innodb-redo-log/ ),估计事务的一部分代价吧
haoliang 12 小时 51 分钟前
幸好我看到了 @Sasasu 的留言,这个 transaction log 应该对应的是 [innodb undo log]( https://mariadb.com/kb/en/innodb-undo-log/ )
gBurnX 11 小时 55 分钟前
@HamQ
强制断电过分了啊。
只看软件部分,在严格按照鲁棒性要求,做好各种情况的失败处理,并且测试到位,那么用户在生产系统里,强行杀死软件进程,或强制关机,是不会影响到数据安全的。
但强制断电,万一内存条、磁盘甚至 CPU 等硬件炸了,软件再强也没办法了。
强制断电过分了啊。
只看软件部分,在严格按照鲁棒性要求,做好各种情况的失败处理,并且测试到位,那么用户在生产系统里,强行杀死软件进程,或强制关机,是不会影响到数据安全的。
但强制断电,万一内存条、磁盘甚至 CPU 等硬件炸了,软件再强也没办法了。
lyjr 10 小时 52 分钟前
假如 mysql 的 redo 日志配置如下:
innodb_log_files_in_group=4
innodb_log_file_size=4G
则 redo 日志文件总大小就是 16G,写入数据超过了就必然报错了。但其实 mysql 会作 redo 容量预测,有些 mysql 版本远远没到总大小(只到十分之一)就报错了。
redo 日志也比原始写入数据要大,redo 日志构造加入很多元数据,而且 undo 空间也要受 redo 保护,同样耗费 redo 资源。
innodb_log_files_in_group=4
innodb_log_file_size=4G
则 redo 日志文件总大小就是 16G,写入数据超过了就必然报错了。但其实 mysql 会作 redo 容量预测,有些 mysql 版本远远没到总大小(只到十分之一)就报错了。
redo 日志也比原始写入数据要大,redo 日志构造加入很多元数据,而且 undo 空间也要受 redo 保护,同样耗费 redo 资源。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK