


Detailed example of how MyBatis implements Mysql database sub-database and table sub-explanation
This article mainly introduces the operation and summary of MyBatis's implementation of Mysql database sub-database and table sub-tables. Friends in need can refer to the following
Foreword
As a database, as a table in the database, as the number of users increases and time goes by, one day, the amount of data will be so large that it is difficult to handle. At this time, the data in just one table exceeds tens of millions. Whether it is querying or modifying, its operation will be very time-consuming. At this time, database segmentation operation is required.
The simplest steps for MyBatis to implement sub-tables
Since the title of the article is written like this, it is more practical to go directly to the dry information. Let’s Let’s first take a look at how to implement the simplest sub-table.
1. We simulated the data volume of the user table to exceed tens of millions (although it is actually unlikely)
2. The original name of the user table was user_tab
, and we divided it into user_tab_0
and user_tab_1
(actually it may not be such a random name), so that the original tens of millions of data can be separated into two tables with an amount of data of two million.
3. How to operate these two tables? We use userId, which is the user's unique identifier, to distinguish.
4, userId%2 == 0
user operation table user_tab_0
, similarly userId%2 == 1
user operation table user_tab_1
5. So how to implement the sql statement in MyBatis? The following is an example of a SQL statement to query a user
<select id="getUser" parameterType="java.util.Map" resultType="UserDO"> SELECT userId, name FROM user_tab_#{tabIndex} WHERE userId = #{userId} </select>
We passed in two parameters tabIndex and userId, tabIndex is the indicator value of the table to be operated on (0 or 1) , so if you need to query the user whose userId is 5, the final sql statement will be:
SELECT userId, name FROM user_tab_1 WHERE userId = 5
I will not show more of the other redundant DAO services and implementations here. Yes, I believe you will do it if you are smart.
The above is the simplest implementation. It does not require extra frameworks or any plug-ins to meet the requirements of sub-tables.
The above is basically all the implementation content. Now we will start to talk about the details of separation in detail. Those who are watching the excitement can basically leave.
I will talk about it from the following perspectives. I put it in the simplest vernacular possible.
Separation methods
There are two main ways of segmentation, horizontal segmentation and vertical segmentation.
1. Horizontal segmentation
To put it simply, split a table into several identical tables, and then the table names are different. Just like the simplest example above.
This kind of segmentation is suitable for situations where the amount of data in a table is too large and the operation time is slowed down, such as some saved record tables.
2. Vertical Segmentation
Divide different business modules into different databases. These business modules are directly preferably 0-coupled (simply put, they have no relationship).
This is mainly suitable for situations where the amount of data is generally large, and the business scenarios are scattered and there is no logical relationship between them.
Separation strategy
There are many specific strategies. You can also design your own. The common strategies are as follows: It’s just a list without going into detail.
1. "%" modulo is implemented in the above example and is also the simplest one.
2, MD5 hash
3, shift
4, date and time (divided into tables according to different dates, such as one table per month, the operation will be performed this month This table will be replaced next month)
5. Enumeration range (users 1-10000 operate the first table, users 10001-20000 operate the second table)
The problem of separation
Let’s talk about the final point and the problems it causes.
The database is definitely not divided as you say. (People are more emotional, how can they just break up?)
Seriously, I have listed the following problems that separation will only cause.
1. The problem of uniqueness of the primary key when adding; after separating multiple tables, the original self-increasing primary key will not be unique, so there is no way to self-increase, causing problems, and there are solutions. , such as maintaining a separate primary key table specifically to store the current primary key, or using other middleware, etc.
2. Although the efficiency issue when adding new data is not a big problem, adding new data will definitely increase the amount of calculation. This problem can be ignored.
3. The paging problem caused by the query will be very difficult after it is separated into multiple tables. This also takes into account that different separations require different solutions. In short, problems will arise.
4. In the same way, related queries, originally it was very simple to associate one table with another table or another table with one table, but now it is difficult after separation.
5. Transaction issues. Multiple tables need to use distributed transactions to complete the original operations with transactions. Because the original transaction only locked one table, now it may have to lock multiple tables.
6. Scalability issues. Some sharding strategies do not have good data scalability. If more data comes later, does it mean that you can create new tables to expand?
Principles of separation
The following summarizes several principles of separation, which are mainly based on references on the Internet without any actual basis (I am not A DBA with an annual salary of one million cannot come across such large data to actually test it), so if you have any questions, please point them out.
1. If you can’t separate, don’t separate.
2. If you can separate less, don’t separate too much.
3. Too redundant and irrelevant
4 , Avoid using distributed transactions, mainly because it is too difficult and I don’t know how to do it
5. If there are less than 10 million records in a single table, it will not be divided
6. If you don’t divide it now, it will be too late
7. Expand, couple, and carefully consider
Ways to achieve separation
Finally, let’s talk about the way of separation, which is now popular The best DAO framework is MyBatis, but there are many other frameworks. The separation is mainly implemented in the following ways.
1. Native implementation, just like the above example, does not require anything else. Use the native framework to control the implementation yourself.
The advantages are: easy to control and take the initiative.
The disadvantages are: there is a lot of code, you need to know it clearly, it is inconvenient to modify, and it does not support complex segmentation. For example, you need to do some paging queries after segmentation, as well as the primary key issues mentioned above.
2. Plug-in implementation, use some plug-ins developed by the framework itself to implement these plug-ins, and then use the plug-ins to access the database to directly achieve separation.
The advantages are: small amount of code, simple implementation, and good scalability.
Disadvantages are: difficult to control, limited separation methods, and difficult to solve problems. No particularly mature plug-ins found.
3. Middleware implementation. Use some database access middleware to perform some operations before accessing the database to make corresponding changes in SQL to achieve separation.
The advantages are: small coupling, good scalability, and can solve the problem of distributed transactions.
Definitely: the implementation is more complicated, requires learning the middleware, and the cost is high. Maintenance is also a big issue, in case it fails. .
In short, each method has its own merits, but considering the cost, the first method is almost 0 cost, you can get started, and it is easier to control, just like the example given above, and the data I am currently processing is still We haven’t reached the point where we have to separate everywhere, so I choose the first option. Also recommended. If you find a plug-in or middleware that is easier to use, you can recommend it in the comments.
Summary
In the actual project, I had to separate the user’s account records because there were too many, and because the account records were more Most of them are just new additions without modification or deletion, and there are only a few queries, so we used the simplest way to separate them and chose the simplest strategy. I hope the above summary of principles, strategies, methods and issues can be helpful and reference for you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for your support of the Script House website!
The above is the detailed content of Detailed example of how MyBatis implements Mysql database sub-database and table sub-explanation. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.
