Specific implementation methods of MySQL table sharding and partitioning

Specific implementation methods of MySQL table sharding and partitioning

Vertical table

Vertical table splitting means splitting a table with many columns into multiple tables. For example, table A contains 20 fields, and now it is split into tables A1 and A2, with ten fields in each table (the specific splitting method depends on the business).

Advantage: In high-concurrency scenarios, the number of table locks and row locks can be reduced.

Disadvantages: When the data records are very large, the reading and writing speeds will still encounter bottlenecks.

Horizontal table

If a certain website has a table in its database with hundreds of millions of records, then if you query it through select, the query will be very slow without an index. In this case, you can use the hash algorithm to divide the table into 10 sub-tables (at this time, the amount of data in each table is only 10 million records).

At the same time, a general table is generated to record the information of each sub-table. If you query a record with id=100, it no longer needs to scan the entire table. Instead, it uses the general table to find the corresponding sub-table where the record is located, and then searches the corresponding table, which reduces IO pressure.

Disadvantages: It will cause great trouble to the maintenance of SQL code of front-end program application. At this time, you can use MySQL Merge storage engine to implement table partitioning.

---------------------------------------I am the dividing line that makes me feel pain----------------------------------------------------

Using the Merge storage engine to partition tables is transparent to the application's SQL statements and does not require any code changes.

CREATE TABLE t1 ( a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20));
CREATE TABLE t2 ( a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20));
INSERT INTO t1 (message) VALUES ('Testing'),('table'),('t1');
INSERT INTO t2 (message) VALUES ('Testing'),('table'),('t2');
CREATE TABLE total (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20)) ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;

When creating the total table, an error may occur:

Unable to open underlying table which is differently defined or of non-MyISAM type or doesn't exist

In fact, the merge storage engine is a virtual table, and the corresponding actual table must be a MyISAM type table. If your MySQL version is 5.1 or above, the default database uses the InnoDB storage engine, so when creating total, the t1 and t2 tables must be MyISAM storage engines.

If you need to add sub-tables regularly, you only need to modify the union of the merge table.

CREATE TABLE t3( a INT NOT NULL AUTO_INCREMENT PRIMARY KEY, message CHAR(20));
ALTER TABLE total UNION=(t1,t2,t3)

Horizontal partition

For example, if there are 1 million pieces of data, they are divided into ten parts. The first 100,000 pieces of data are placed in the first partition, the second 100,000 pieces of data are placed in the second partition, and so on. When a piece of data is retrieved, it contains all the fields in the table structure, and horizontal partitioning does not change the table structure.

Vertical partition

For example, when designing a user table, you may not think carefully at the beginning and put all personal information into one table. This table will have relatively large fields, such as personal profiles. However, these profiles may not be viewed by many people, so when someone wants to view them, they will search for them. When splitting the table, you can separate such large fields.

A complete table corresponds to three files: a .MYD data file, a .MYI index file, and a .frm table structure file.

You may also be interested in:
  • MySQL advanced features - detailed explanation of the concept and mechanism of data table partitioning
  • In-depth explanation of MySql table, database, sharding and partitioning knowledge
  • Introduction to MySql table, database, sharding and partitioning knowledge points
  • MySQL partitioning practice through Navicat
  • Correct use of MySQL partition tables
  • Mysql optimization Zabbix partition optimization
  • Is it necessary to create a separate index for the MySQL partition field column?
  • MySQL database table partitioning considerations [recommended]
  • A Brief Analysis of MySQL Data Table Partition Technology
  • MySQL data table partitioning strategy and advantages and disadvantages analysis

<<:  Use native js to simulate the scrolling effect of live bullet screen

>>:  Teach you how to enable the Linux subsystem of Win10 (with detailed pictures and text)

Recommend

MySQL 5.7.17 winx64 installation and configuration tutorial

Today I installed the MySQL database on my comput...

onfocus="this.blur()" is hated by blind webmasters

When talking about the screen reading software op...

Add ico mirror code to html (favicon.ico is placed in the root directory)

Code: Copy code The code is as follows: <!DOCTY...

Detailed explanation of how MySQL solves phantom reads

1. What is phantom reading? In a transaction, aft...

Detailed explanation of the process of zabbix monitoring sqlserver

Let's take a look at zabbix monitoring sqlser...

Detailed explanation of TIMESTAMPDIFF case in MySQL

1. Syntax TIMESTAMPDIFF(unit,begin,end); Returns ...

MySQL string splitting example (string extraction without separator)

String extraction without delimiters Question Req...

Three ways to configure JNDI data source in Tomcat

In my past work, the development server was gener...

How to install Jenkins on CentOS 8

To install Jenkins on CentOS 8, you need to use t...