1. Horizontal segmentation
Horizontal sharding, also known as the Sharding strategy , splits records in the same table into multiple tables with the same structure.
When the data of a table continues to increase, Sharding is an inevitable choice. It can distribute the data to different nodes of the cluster, thereby caching the pressure of a single database.
- Hash modulo: hash(key) % NUM_DB
- Range: Can be an ID range or a time range
- Mapping table: use a separate database to store the mapping relationship
1. Business issues
Use distributed transactions to solve, such as XA interface.
2. Links
The original JOIN can be decomposed into multiple single-table queries, and then JOIN can be performed in the user program.
3. ID uniqueness
- Use globally unique ID: GUID
- Specify an ID range for each shard
- Distributed ID generators (like Twitter's Snowflake algorithm)
2. Vertical Slicing
Vertical splitting is to split a table into multiple tables by column, usually according to the degree of relationship density of the columns. Vertical splitting can also be used to split frequently used columns and infrequently used columns into in a different table.
Using vertical segmentation at the database level will deploy to different libraries according to the density of tables in the database, for example, vertically segmenting the original e-commerce database into commodity databases, user databases, etc.
For more information, please refer to:
- How Sharding WorksOpens in a new window
- The practice of sub-database and sub-table of Dianping order system
Copyright belongs to @pdai Original link: https://pdai.tech/md/db/sql-mysql/sql-mysql-devide.html