当前位置:网站首页>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~
边栏推荐
- Linux系统卸载,安装,升级,迁移clickHouse数据库
- 牛客网项目17节生成验证码 刷新验证码一直没反应
- 日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
- Use compilation to realize special effects of love
- 【云原生】快出数量级的性能是怎样炼成的?就提升了亿点点
- node封装一个图片拼接插件
- 中国发布丨滴滴因违反网络安全法等被罚80.26亿元!调查细节公布
- 用了TCP协议,就一定不会丢包嘛?
- State Management in Jetpack Compose
- 周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...
猜你喜欢
随机推荐
瑞吉外卖项目剩余功能补充
让电商运营10倍提效的自动化工具,你get了吗?
Mistakes in Brushing the Questions 1-Implicit Conversion and Loss of Precision
2022牛客暑期多校训练营4(ADHKLMN)
HikariCP数据库连接池,太快了!
QT专题:组合会话框和文本编辑器
【Redis】通用命令
转转反爬攻防战
leetcode 62. Unique Paths(独特的路径)
裁员趋势下的大厂面试:“字节跳动”
DVWA 通关记录 2 - 命令注入 Command Injection
带你认识40G单纤双向光模块-QSFP+ BiDi光模块
图形化矩阵,矩阵到底长什么样?
李航《统计学习方法》笔记之监督学习Supervised learning
8月份的.NET Conf 活动 专注于 .NET MAUI
mysql进阶(二十一)删除表数据与数据库四大特性
二维数组零碎知识梳理
用汇编实现爱心特效【七夕来袭】
一文带你了解推荐系统常用模型及框架
腾讯T8架构师,教你学中小研发团队架构实践PDF,高级架构师捷径