Vertical and horizontal splitting of MySQL tables

Vertical and horizontal splitting of MySQL tables

Vertical Split

Vertical splitting refers to the splitting of data table columns, splitting a table with many columns into multiple tables

  1. Usually we perform vertical splitting according to the following principles:
  2. Put infrequently used fields in a separate table;
  3. Split out large fields such as text and blob and put them in the appendix;

Columns that are frequently queried in combination are placed in one table;

Vertical splitting should be performed at the beginning of data table design, and then the join key can be used when querying;

Horizontal Split

Horizontal splitting refers to the splitting of data table rows. When the number of rows in a table exceeds 2 million, the processing will become slow. At this time, the data of one table can be split into multiple tables for storage.

Some tips for horizontal splitting

1. Split principle

Usually, we use the modulus method to split the table; for example, a user table with 4 million users is divided into 4 tables to improve its query efficiency.

users1, users2, users3, users4

By using the ID modulus method, the data is distributed into four tables. Id%4+1 = [1,2,3,4]

Then query, update, and delete are also queried by taking the modulus method

$_GET['id'] = 17,
17%4 + 1 = 2, 
$tableName = 'users'.'2'
Select * from users2 where id = 17;

When inserting, a temporary table uid_temp is also needed to provide an auto-incrementing ID. The only use of this table is to provide an auto-incrementing ID.

insert into uid_temp values(null);

After getting the self-incrementing ID, the sub-table is inserted through the modulo method;

Note that the columns and types of the fields in the table after horizontal split should be the same as those in the original table, but remember to remove the auto_increment self-increment

in addition

  • Some business logic can also be split by fields such as region and year;
  • The split table can only meet the efficient query requirements of some queries. At this time, we need to restrict user query behavior from the interface in product planning. For example, if we want to split archives by year, the page design will constrain users to select a year before they can search;
  • When doing analysis or statistics, it doesn't matter if you wait a little longer because it's your own needs, and the concurrency is very low. At this time, you can use union to combine all tables into a view for query, and then query again;

Create view users as select from users1 union select from users2 union.........

The above are the details of vertical splitting and horizontal splitting of MySQL tables. For more information about MySQL table splitting, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MySQL string splitting example (string extraction without separator)
  • MySQL string splitting operation (string interception containing separators)
  • MySQL merge and split by specified characters example tutorial
  • MySQL interception and split string function usage examples
  • Summary of MYSQL database data splitting: sub-library and sub-table
  • MySQL common data splitting methods
  • Use Perl to split the data table (MySQL) and migrate the data instance
  • mysql splits a row of data into multiple rows based on commas

<<:  Native JS object-oriented typing game

>>:  How to configure port forwarding for docker on CentOS 7 to be compatible with firewall

Recommend

Installation process of zabbix-agent on Kylin V10

1. Download the installation package Download add...

Border-radius IE8 compatible processing method

According to canisue (http://caniuse.com/#search=...

HTML unordered list bullet points using images CSS writing

Create an HTML page with an unordered list of at l...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...

Vue implements button switching picture

This article example shares the specific code of ...

HTML+CSS implementation code for rounded rectangle

I was bored and suddenly thought of the implementa...

Detailed installation and use of SSH in Ubuntu environment

SSH stands for Secure Shell, which is a secure tr...

Solution for Baidu site search not supporting https (tested)

Recently, https has been enabled on the mobile ph...

Solution to the problem of eight hours difference in MySQL insertion time

Solve the problem of eight hours time difference ...

Detailed explanation of nodejs built-in modules

Table of contents Overview 1. Path module 2. Unti...

Implementation code of Nginx anti-hotlink and optimization in Linux

Hide version number The version number is not hid...

Complete steps to implement location punch-in using MySQL spatial functions

Preface The project requirement is to determine w...

MySQL Basics Quick Start Knowledge Summary (with Mind Map)

Table of contents Preface 1. Basic knowledge of d...

Search engine free collection of website entrances

1: Baidu website login entrance Website: http://ww...