当前位置:网站首页>Detailed explanation of how to smoothly go online after MySQL table splitting
Detailed explanation of how to smoothly go online after MySQL table splitting
2022-04-23 11:06:00 【liming89】
Source of the article : Learn through http://www.bdgxy.com/
The purpose of the sub table
Project under development , Our database data is getting bigger and bigger , What follows is that there is too much data in a single table . So that the query data becomes slower , Moreover, the application operation is also seriously affected due to the table locking mechanism , There is a database performance bottleneck .
When this happens , We can consider sub tables , About to split a single database table , Split into multiple data tables , Then when the user accesses , According to a certain algorithm , Let users access different tables , In this way, the data is scattered into multiple data tables , Reduce the access pressure of a single data table . Improved database access performance .
Take a chestnut
For example, our most common user table (user surface )
id | user_id | Other fields |
---|---|---|
Primary key id | user id | Other fields |
We usually use user_id To query the corresponding user information , But as the business grows , This watch will get bigger and bigger , Even hundreds of millions , It seriously affects the query performance . So we'll split this table , Divide into multiple tables to reduce query pressure
Tabulation strategy
By points 10 Take Zhang Biao as an example ( How many tables are divided Estimate according to the actual situation ) First, let's build 10 A watch user1、user2、user3.....user10
In general , We all use fields as indexes (user_id) Take the mold . How many watches do you want to divide , Just take the mold according to how much , Like this case Namely 10
$table_name = $user_id % 10;
According to the above mold formula
- user_id by 1295 Will fall on user5 Inside
- user_id by 8634 Will fall on user4 Inside
- .......
「 Every time CURD Just follow the strategy of looking up the table above 」, It's not a big problem , Let's not say more .
What about running tables that are already online ?
In fact, everyone should know how to use the above methods , But there's a problem , What about the online watch ? The data of that table has been searched or changed online . How to smooth sub table , And make users imperceptible ?
Method 1
Go online directly , Write a script in advance , The script is to put the old table (user) Data synchronization to user1 Table to user10 surface , As soon as it goes online, execute it quickly
This method obviously won't work , There are mainly the following problems
- What if there is a problem with the script during execution ? All code is rolled back ?
- The script takes the old table (user) Data synchronization to user1 Table to user10 surface , How long does this script have to run ? If it is 1 Hours , Then the business related to this table on this timeline is abnormal
This is obviously not going to work , It has a great impact on the online .
Method 2
First write a script to synchronize data , The script is to put the old table (user) Data synchronization to user1 Table to user10 surface , When the script is synchronized, go online again .
This method looks a little friendly , But there are also some problems .
- Script synchronization finished , Go online now , There is some time difference between the two things , There may be some changes in the online table in this time difference , What about these changes ?
「 Neither of these methods seems to work , So it looks a little different . Let's look directly at the conclusion .」
step 1 Online double writing
First, let's write the double line , What does that mean ? such as user_id=123, For the increase , Delete , For modification operations , We both operate user surface , Also operate user_id=123 Corresponding user3 surface .
function modify($user_id){ // Including the addition of , Delete , Modify the operating
modify_user(); //modify user surface
$table_name = $user_id % 10;
modify_user($table_name) //modify Corresponding sub table
}
Because the part of the query is still user The , Therefore, the above operations have no impact on online users .
step 2 Full amount of synchronization
Write a full synchronization user Table to user1-user10 Table of , It's best to find a low peak execution script , Just in case it affects user Table in the query
After this step , Because we went online before ( See step 1), therefore user Table and user1-user10 The data between tables are completely consistent .
step 3 Query new table data
Change the part of the query to user1-user10
Because the first two steps have guaranteed user The data between tables and sub tables are completely consistent , So there is no problem changing the query directly
If you follow the above steps , Then there is no impact on the online data , And that's how we operate online , After many times of practice, it is ensured that there will be no problems , You can use it at ease .
summary
This is about mysql This is the article on how to smooth the online after the table is divided , More about mysql Split table smooth online content, please search rookie tutorial www.piaodoo.com Previous articles or continue to browse the relevant articles below. I hope you can support rookie tutorials in the future www.piaodoo.com!
版权声明
本文为[liming89]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102124270.html
边栏推荐
- Latex usage
- GO接口使用
- Solution architect's small bag - 5 types of architecture diagrams
- Common parameters of ffmpeg command line
- Visual common drawing (I) stacking diagram
- Learning website materials
- Mba-day5 Mathematics - application problems - engineering problems
- mysql分表之后如何平滑上线详解
- Intuitive understanding entropy
- The songbird document editor will be open source: starting with but not limited to markdown
猜你喜欢
Intuitive understanding entropy
Cygwin 中的 rename 用法
Learning Notes 6 - Summary of several deep learning convolutional neural networks
Jupyter lab top ten high productivity plug-ins
升级cpolar内网穿透能获得的功能
《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
ID number verification system based on visual structure - Raspberry implementation
Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
Go interface usage
The songbird document editor will be open source: starting with but not limited to markdown
随机推荐
Jupyter Lab 十大高生产力插件
Alarm scene recognition
Microsoft Access database using PHP PDO ODBC sample
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
Typora operation skill description (I)
Detailed explanation of MySQL creation stored procedure and function
remote: Support for password authentication was removed on August 13, 2021.
Cygwin 中的 rename 用法
Which company is good for opening futures accounts? Who can recommend several safe and reliable futures companies?
防止web项目中的SQL注入
Visualization Road (11) detailed explanation of Matplotlib color
How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
Detailed explanation of typora Grammar (I)
MySQL对数据表已有表进行分区表的实现
学习 Go 语言 0x04:《Go 语言之旅》中切片的练习题代码
MySQL sorting feature details
Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
闹钟场景识别
Source insight 4.0 FAQs
学习 Go 语言 0x01:从官网开始