mysql 大sql文件使用mysqldump備份提速方案
mysqldump備份
備份時添加 --extended-insert 將多個insert into 語句合並成一行,能提高效率3-4倍
使用--extended-insert=false導出的sql文件數據是這樣的,每行一條insert語句,執行效率非常低下
使用–-extended-insert=true導出的表,是一個很長的insert語句,會進行批量插入。
使用msyqldump -uroot -pPWD –-extended-insert database >database.sql
調整MYSQL快速插入參數
如果你的數據庫儲存引擎是MYISAM參數的話,可以將此參數設置到512M或256M,MyISAM會使用一種特殊的樹狀緩存來做出更快的批量插入。
相關文檔https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_bulk_insert_buffer_size
該值默認是8M = 8388608byte
查看插入緩沖區大小
`SHOW VARIABLES LIKE '%bulk%'
設置插入緩沖區大小(全局)
`SET GLOBAL bulk_insert_buffer_size =1024*1024*512;`
設置插入緩沖區大小(session)
`SET bulk_insert_buffer_size =1024*1024*256;`
如果需要設置Mysql重新啟動時,依然保留該值,需要將這段配置添加了my.cnf
`[mysqld]``bulk_insert_buffer_size = 256M`
對於Innodb引擎中,我們可以關閉一些系統檢查項來實現更快的插入的方案.
//關閉自動提交
SET autocommit=0;
//關閉唯一檢查
set unique_checks = 0;
//關閉外鍵檢查
SET foreign_key_checks=0;
//備份的時候開啟
--extended-insert
參數
關於Innodb批量數據加載相關文檔:https://dev.mysql.com/doc/refman/5.7/en/optimizing-innodb-bulk-data-loading.html
注意事項:對於大數據量恢復備份存在日志緩存問題,推測可能與關閉自動提交和一次插入大批量數據,沒有分條插入有關
數據恢復
登錄mysql 數據庫
msyql -uroot -pPWD
> source /path/bak.sql
恢復數據完成后,獲取所有表行數,和原來數據庫對比數據差異
生成查詢sql
select concat(
'select "',
TABLE_name,
'", count(*) from ',
TABLE_SCHEMA,
'.',
TABLE_name,
' union all'
) from information_schema.tables
where TABLE_SCHEMA='database';
復制所有生成的sql ,去掉最后一行結尾處的 union all ,執行查詢,等待查詢結果
select "call_me_cash_coupon", count(*) from xxx.call_me_cash_coupon union all
select "call_me_com_spe", count(*) from xxx.call_me_com_spe union all
select "call_me_delivery_clerk", count(*) from xxx.call_me_delivery_clerk union all
select "call_me_goods_spec_relation", count(*) from xxx.call_me_goods_spec_relation
執行效果:
| -call_me_cash_coupon | count(*) |
|---|---|
| call_me_cash_coupon | 633 |
| call_me_com_spe | 64 |
| call_me_delivery_clerk | 4510 |
| call_me_goods_spec_relation | 3067 |
