Home Database Mysql Tutorial Detailed explanation of MySql database partitioning and table partitioning methods and introduction to partitioning and table partitioning

Detailed explanation of MySql database partitioning and table partitioning methods and introduction to partitioning and table partitioning

Mar 29, 2017 pm 01:49 PM

This article mainly introduces MySql database partitioning and table partitioning methods in detail, telling you what table partitioning and partitioning are, and what is the connection between mysql table partitioning and partitioning. It has a certain reference value. Interested friends can refer to it

1. Why should we divide tables and partitions

We often encounter big problems in daily development In the case of tables, the so-called large tables refer to tables that store millions or even tens of millions of records. Such a table is too large, causing the database to take too long to query and insert, resulting in low performance. If joint query is involved, the performance will be even worse. The purpose of table partitioning and table partitioning is to reduce the burden on the database and improve the efficiency of the database. Generally speaking, it is to improve the efficiency of adding, deleting, modifying, and querying tables.

2. What are sub-tables and partitions

2.1 Sub-tables

Sub-tables are Decompose a large table into multiple entity tables with independent storage space according to certain rules. We can call them sub-tables. Each table corresponds to three files, MYD data file, .MYI index file, and .frm table structure file. . These subtables can be distributed on the same disk or on different machines. When the app reads and writes, it gets the corresponding subtable name according to the predefined rules, and then operates it.

2.2 Partition

Partitioning is similar to table partitioning, both of which decompose tables according to rules. The difference is that table splitting decomposes a large table into several independent entity tables, while partitioning divides data into segments and stores them in multiple locations, which can be on the same disk or on different machines. After partitioning, there is still one table on the surface, but the data is hashed to multiple locations. When the app reads and writes, it still operates on the big table name, and the db automatically organizes the partitioned data.

The main purpose of partitioning is to reduce the total amount of data read and write in a specific SQL operation to reduce response time.

2.3 What is the connection between mysql tables and partitions?

1), can improve the performance of mysql, and have a good performance under high concurrency

state. 2) Table subdivision and partitioning are not contradictory and can cooperate with each other. For those tables with large access volume and relatively large table data, we can combine table subdivision and partitioning. The access volume is not large, but the table For tables with a lot of data, we can partition them.
3) The sub-table technology is more troublesome. You need to manually create sub-tables, and the app server needs to calculate the sub-table names when reading and writing. It is better to use merge, but you must also create the
union relationship between the subtables and configure the subtables. 4) Compared with sub-tables, table partitioning is easy to operate and does not require the creation of sub-tables.

3. Several ways to divide tables

3.1 mysql cluster

It is not a sub-table, but it plays the same role as a sub-table. The cluster can share the number of database operations and distribute the tasks to multiple databases. The cluster can separate reading and writing to reduce reading and writing pressure. Thereby improving database performance.

3.2 Customized rule table splitting

A large table can be decomposed into multiple sub-tables according to business rules. Usually there are the following types, you can also define your own rules.

Range (Range) – This mode allows data to be divided into different ranges. For example, a table can be divided into several partitions by year.
Hash (Hash) –This mode allows calculation of the Hash Key of one or more columns of the table, and finally uses this Hash Partition the data areas corresponding to different code values. For example, you can create a table that partitions the table's primary key.
Key (key value) –An extension of the above Hash mode, the Hash Key here is generated by the MySQL system.
List (Predefined List) – This mode allows the system to split data by the value of a predefined list.
Composite (composite mode) The combination of the above modes uses the

table splitting rules As with partitioning rules, they are described in detail in the partitioning module.


The following uses Range to briefly introduce how to divide the table (according to the year table).

Assume that the table structure has 4 fields: auto-increment id, name, deposit amount, deposit date
Use the deposit date as a rule to divide the tables and create several tables respectively
2011: account_2011
2012 :account_2012
......
2015: account_2015
The app searches for the corresponding table name based on the date when reading and writing, and needs to be determined manually.

var getTableName = function() {
  var data = {
    name: 'tom',
    money: 2800.00,
    date: '201410013059'
  };
  var tablename = 'account_';
  var year = parseInt(data.date.substring(0, 4));
  if (year < 2012) {
    tablename += 2011; // account_2011
  } else if (year < 2013) {
    tablename += 2012; // account_2012
  } else if (year < 2014) {
    tablename += 2013; // account_2013
  } else if (year < 2015) {
    tablename += 2014; // account_2014
  } else {
    tablename += 2015; // account_2015
  }
  return tablename;
}
Copy after login

3.3 Use the merge storage engine to implement table splitting

merge split table is divided into main table and sub-table. The main table is similar to a The shell logically encapsulates the sub-table. In fact, the data is stored in the sub-table.

We can insert and query data through the main table. If we know the rules of sub-tables, we can also directly operate the sub-tables.

Sub table 2011

CREATE TABLE `account_2011` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`money` float NOT NULL ,
`tradeDate` datetime NOT NULL
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=2
CHECKSUM=0
ROW_FORMAT=DYNAMIC
DELAY_KEY_WRITE=0
;
Copy after login

Sub table 2012

CREATE TABLE `account_2012` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`money` float NOT NULL ,
`tradeDate` datetime NOT NULL
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=2
CHECKSUM=0
ROW_FORMAT=DYNAMIC
DELAY_KEY_WRITE=0
;
Copy after login

Main table, all years

CREATE TABLE `account_all` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`money` float NOT NULL ,
`tradeDate` datetime NOT NULL
PRIMARY KEY (`id`)
)
ENGINE=MRG_MYISAM
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
UNION=(`account_2011`,`account_2012`)
INSERT_METHOD=LAST
ROW_FORMAT=DYNAMIC
;
Copy after login

When creating the main table, there is an INSERT_METHOD, indicating Insertion mode, the value can be: 0 does not allow insertion; FIRST inserts into the first table in UNION; LAST inserts into the last table in UNION.

When querying through the main table, it is equivalent to querying all sub-tables together. This does not reflect the advantages of sub-tables. It is recommended to query sub-tables.

4. Several ways of partitioning

4.1 Range

create table range( 
  id int(11), 
  money int(11) unsigned not null, 
  date datetime 
  )partition by range(year(date))( 
  partition p2007 values less than (2008), 
  partition p2008 values less than (2009), 
  partition p2009 values less than (2010) 
  partition p2010 values less than maxvalue 
);
Copy after login

4.2 List

create table list( 
  a int(11), 
  b int(11) 
  )(partition by list (b) 
  partition p0 values in (1,3,5,7,9), 
  partition p1 values in (2,4,6,8,0) 
 );
Copy after login

4.3 Hash

create table hash( 
  a int(11), 
  b datetime 
  )partition by hash (YEAR(b) 
  partitions 4;
Copy after login

4.4 key

create table t_key( 
  a int(11), 
  b datetime) 
  partition by key (b) 
  partitions 4;
Copy after login

4.5 Partition Management

4.5.1 Add new partition

ALTER TABLE sale_data
ADD PARTITION (PARTITION p201010 VALUES LESS THAN (201011));
Copy after login

4.5.2 Delete partition

When a partition is deleted, all data in the partition is also deleted.

ALTER TABLE sale_data DROP PARTITION p201010;
Copy after login

4.5.3 Merge partitions

The following SQL merges p201001 - p201009 into 3 partitions p2010Q1 - p2010Q3

ALTER TABLE sale_data
REORGANIZE PARTITION p201001,p201002,p201003,
p201004,p201005,p201006,
p201007,p201008,p201009 INTO
(
PARTITION p2010Q1 VALUES LESS THAN (201004),
PARTITION p2010Q2 VALUES LESS THAN (201007),
PARTITION p2010Q3 VALUES LESS THAN (201010)
);
Copy after login

The above is the detailed content of Detailed explanation of MySql database partitioning and table partitioning methods and introduction to partitioning and table partitioning. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

How to fill in mysql username and password How to fill in mysql username and password Apr 08, 2025 pm 07:09 PM

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Apr 08, 2025 pm 07:12 PM

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

How to view mysql How to view mysql Apr 08, 2025 pm 07:21 PM

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

See all articles