当前位置:网站首页>MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
2022-08-02 09:56:00 【night rain】
In order to facilitate testing, create a new table and insert 20 million pieces of test data
Mysql version: 5.7.34
1. Quickly insert millions of data
--Create a MyISAM schema table to facilitate batch running of data (simulate the actual table with multiple fields)CREATE TABLE `my_tables` (`id` bigint(32) NOT NULL AUTO_INCREMENT,`name` varchar(32) DEFAULT NULL,`age` int(32) DEFAULT NULL,`time` varchar(32) DEFAULT NULL,`pwd` varchar(32) DEFAULT NULL,`test` varchar(255) DEFAULT NULL,`test2` varchar(255) DEFAULT NULL,`test3` varchar(255) DEFAULT NULL,`test4` varchar(255) DEFAULT NULL,`test5` varchar(255) DEFAULT NULL,`test6` varchar(255) DEFAULT NULL,`test7` varchar(255) DEFAULT NULL,`test8` varchar(255) DEFAULT NULL,`test9` varchar(255) DEFAULT NULL,`test10` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;--Create a stored procedure (insert two million pieces of data, about 45 seconds, the more data is executed a few times)DROP PROCEDURE IF EXISTS my_insert;CREATE PROCEDURE my_insert()BEGINDECLARE n int DEFAULT 1;loopname:LOOPINSERT INTO `my_tables`(`name`,`age`,`time`,`pwd`)VALUES ('Test Name', 18, '2022-07-27', '369');SET n=n+1;IF n=2000000 THENLEAVE loopname;END IF;END LOOP loopname;END;--Execute the stored procedureCALL my_insert();2. Paging statement
The writing method of select * in the following sql is just for testing and display Do not actually use it like this (write which fields should be checked)!
//Number of queriesSELECT count(1) FROM my_tables;25000021 --- 0.03 seconds (count query is really fast)// only query the indexSELECT id FROM my_tables limit 20000000,20;2.965 seconds//Ordinary paging querySELECT * FROM my_tables limit 20000000,20;4.441 seconds//Optimization 1: Subquery methodSELECT * FROM my_tables where id>=(select id from my_tables limit 20000000,1)limit 20;2.981 seconds//Optimization 2: join query method (compared to sub-query query speed is about the same, the speed is increased by about 30%)SELECT * FROM my_tables a JOIN(select id from my_tables limit 20000000,20)b on a.id=b.id;2.953 seconds//Optimization 3: Sort method according to primary key or unique index (use index to quickly locate part of data, avoid full table scan, and increase the speed by 99%)SELECT * FROM my_tables where id>=20000000 ORDER BY id ASC LIMIT 0,20;0.038 seconds
3. Actual usage:

pageNum: page numberpageSize: How many items are displayed on one pageSELECT * FROM table name WHERE id>=(pageNum*pageSize) ORDER BY id ASC LIMIT pageNum,pageSize;4. Error example:
//When connecting table query or sub-table query, the limit should be placed in it, otherwise the query time and data volume will multiply the number of data in the two tables.mistake:SELECT * FROM a table a JOIN(select id from a table) b on a.id=b.id limit 0,20;correct:SELECT * FROM a table a JOIN(select id from a table limit 0,20) b on a.id=b.id;mistake:select * from (select * from a table a left join b table b on a.id = b.id ) tt where ... limit 0,20correct:select * from (select * from a table a left join b table b on a.id = b.id limit 0,13) tt where ...The optimization ends here. The above sql is written for testing without adding where conditions, etc. You need to rewrite it according to your actual needs
If the article is wrong or in doubt, please leave a message to discuss~
边栏推荐
- 未知内容监控
- Daily practice of dynamic programming (3)
- 向量点积(Dot Product),向量叉积(Cross Product)
- 腾讯T8架构师,教你学中小研发团队架构实践PDF,高级架构师捷径
- Jetpack Compose 中的状态管理
- 软件测试X模型
- 第15章 泛型
- Worship, Alibaba distributed system development and core principle analysis manual
- 【云原生】快出数量级的性能是怎样炼成的?就提升了亿点点
- This article takes you to understand the commonly used models and frameworks of recommender systems
猜你喜欢

Have you ever learned about these architecture designs and architecture knowledge systems?(Architecture book recommendation)

Rust from entry to master 03-helloworld

李航《统计学习方法》笔记之朴素贝叶斯法

Facebook's automated data analysis solution saves worry and effort in advertising

The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"

Getting Started with SCM from Scratch (1): Summary of Background Knowledge

从零开始入门单片机(一):必会背景知识总结

李航《统计学习方法》笔记之感知机perceptron

DVWA 通关记录 2 - 命令注入 Command Injection

leetcode:639. 解码方法 II
随机推荐
周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...
2022.7.25-7.31 AI行业周刊(第108期):值钱比赚钱更重要
HCIA动态主机配置协议实验(dhcp)
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
AutoJs学习-存款计算器
软件测试之发现和解决bug
C语言volatile关键字、内嵌汇编volatile与编译器的爱恨情仇
刷题错题录1-隐式转换与精度丢失
Pytorch的LSTM参数解释
你有了解过这些架构设计,架构知识体系吗?(架构书籍推荐)
8月份的.NET Conf 活动 专注于 .NET MAUI
剑指offer专项突击版第17天
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
AutoJs学习-AES加解密
YugaByte adds Voyager migration service in its 2.15 database update
使用scrapy 把爬到的数据保存到mysql 防止重复
单机部署flink,创建oracle19c rac的连接表时报错 ORA-12505 ,怎么回事?
【新版干货书】深度伪造 (DeepFakes):创造,检测和影响
裁员趋势下的大厂面试:“字节跳动”
This article takes you to understand the commonly used models and frameworks of recommender systems