当前位置:网站首页>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~
边栏推荐
- State Management in Jetpack Compose
- This article takes you to understand the commonly used models and frameworks of recommender systems
- AutoJs学习-AES加解密
- In the whole development of chi V853 board tried to compile QT test
- 中国发布丨滴滴因违反网络安全法等被罚80.26亿元!调查细节公布
- js防抖函数和函数节流的应用场景
- QT专题:组合会话框和文本编辑器
- Implementation of mysql connection pool
- [Must read] Mylander valuation analysis, electrical stimulation products for pelvic and postpartum rehabilitation
- 1对1视频源码——快速实现短视频功能提升竞争力
猜你喜欢
system_error错误处理库学习
一文带你了解推荐系统常用模型及框架
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
mysql连接池的实现
Have you ever learned about these architecture designs and architecture knowledge systems?(Architecture book recommendation)
【技术分享】OSPFv3基本原理
腾讯T8架构师,教你学中小研发团队架构实践PDF,高级架构师捷径
matlab-day02
leetcode 62. Unique Paths(独特的路径)
随机推荐
HikariCP database connection pool, too fast!
EdrawMax Crack,多合一的图表应用程序
php组件漏洞
Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning
百战RHCE(第四十七战:运维工程师必会技-Ansible学习2-Ansible安装配置练习环境)
软件测试X模型
牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
Pytorch的LSTM参数解释
软件测试与质量 之白盒测试
Facebook's automated data analysis solution saves worry and effort in advertising
Naive Bayesian Method of Li Hang's "Statistical Learning Methods" Notes
Worship, Alibaba distributed system development and core principle analysis manual
【微信小程序】本地服务页面案例实现
每天花2小时恶补腾讯T8纯手打688页SSM框架和Redis,成功上岸美团
The perceptron perceptron of Li Hang's "Statistical Learning Methods" notes
nacos项目搭建
MySql千万级分页优化,快速插入千万数据方法
重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透
leetcode:81. 搜索旋转排序数组 II
从零开始入门单片机(一):必会背景知识总结