当前位置:网站首页>提高INSERT速度
提高INSERT速度
2022-06-24 07:03:00 【一个不靠谱的程序员】
当我采集的数据量达到8000万时,往一张具有唯一索引的表里批量插入数据时,需要检查重复,因此插入速度越来越慢。今天就来聊下如何提高INSERT的执行速度。
批量插入比单条插入速度快
INSERT INTO table (field1,field2,field3) VALUES (‘a’,“b”,“c”), (‘a’,“b”,“c”),(‘a’,“b”,“c”);
INSERT INTO table (field1,field2,field3) VALUES (‘a’,“b”,“c”);INSERT INTO table (field1,field2,field3) VALUES (‘a’,“b”,“c”);INSERT INTO table (field1,field2,field3) VALUES (‘a’,“b”,“c”);
使用1的批量插入写法,能大大提高插入速度。
合理利用数据库buffer
可以调大bulk_insert_buffer_size这个参数。默认为8M。
这个参数仅作用于使用 MyISAM存储引擎,用来缓存批量插入数据的时候临时缓存写入数据。mysql会使用这个内存区域来缓存批量结构的数据以帮助批量写入数据文件。
例如将该buffer大小设置为100M。
set global bulk_insert_buffer_size = 1024*1024*100;
用INSERT DELAYED延迟插入
当一个客户端使用INSERT DELAYED时,会立刻从服务器处得到一个确定。并且行被排入队列,当表没有被其它线程使用时,此行被插入。
使用INSERT DELAYED的另一个重要的好处是,来自许多客户端的插入被集中在一起,并被编写入一个块。这比执行许多独立的插入要快很多。
需要注意:
- INSERT DELAYED应该仅用于指定值清单的INSERT语句。服务器忽略用于INSERT DELAYED…SELECT语句的DELAYED。
- 服务器忽略用于INSERT DELAYED…ON DUPLICATE UPDATE语句的DELAYED。
- 因为在行被插入前,语句立刻返回,所以您不能使用LAST_INSERT_ID()来获取AUTO_INCREMENT值。AUTO_INCREMENT值可能由语句生成。
- 对于SELECT语句,DELAYED行不可见,直到这些行确实被插入了为止。
- DELAYED在从属复制服务器中被忽略了,因为DELAYED不会在从属服务器中产生与主服务器不一样的数据。
- 目前在队列中的各行只保存在存储器中,直到它们被插入到表中为止。这意味着,如果您强行中止了mysqld(例如,使用kill -9) 或者如果mysqld意外停止,则所有没有被写入磁盘的行都会丢失。
边栏推荐
- jwt(json web token)
- Opencv实现图像的基本变换
- ZUCC_编译语言原理与编译_实验06 07 语法分析 LL 分析
- The article takes you to understand the security of Windows operating system and protect your computer from infringement
- Qt源码分析--QObject(2)
- Final review and key points of software process and project management
- 权限模型 DAC ACL RBAC ABAC
- Synthesize video through ffmpeg according to m3u8 file of video on the network
- Maya re deployment
- 2021-06-25: a batch of strings consisting only of lowercase letters (a~z) are put
猜你喜欢

ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language

uniapp 热更新后台管理

io模型初探

【关于运维和网工的差别,一文说透】

jwt(json web token)

Vscode install the remote -wsl plug-in to connect to the local WSL

成为IEEE学生会员

ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析

Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022

Qt导出PDF文件的两种方法
随机推荐
QT writing security video monitoring system 36 onvif continuous movement
Fund raising, trading and registration
(PKCS1) RSA 公私钥 pem 文件解析
Shell basic operators -- relational operators
Base64编码详解及其变种(解决加号在URL变空格问题)
ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
JUC personal simple notes
Variable declaration and some special variables in shell
dataX使用指南
常用日期格式符与Qt获取当前时间的办法
PHP代码加密+扩展解密实战
Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022
Two methods of QT exporting PDF files
Shell basic operator -- arithmetic operator
相机投影矩阵计算
String转Base64
How to handle the problem that calling easycvr address integration cannot be played through easyplayer player?
新技术实战,一步步用Activity Results API封装权限申请库
ZUCC_ Principles of compiling language and compilation_ Big job