search
HomeDatabaseMysql Tutorial[原创]数据表的水平拆分(1) 初探分库分表

[原创]数据表的水平拆分(1) 初探分库分表

Jun 07, 2016 pm 04:32 PM
Branch librarysub-tablePreliminary explorationOriginalSplitnumberdata sheetlevel

当一个数据表数据量非常大的时候,查询会变得非常慢。 一般来说MySQL达到千万条以后(视情况而定,查询较少的表可能会稍好一点), 所以就要尝试分库分表, 就是所谓的数据库水平拆分。 水平拆分最重要的一点是按照什么 分表 .先不说理论,先看下边实例 用户

          当一个数据表数据量非常大的时候,查询会变得非常慢。 一般来说MySQL达到千万条以后(视情况而定,查询较少的表可能会稍好一点), 所以就要尝试分库分表, 就是所谓的数据库水平拆分。
水平拆分最重要的一点是按照什么分表. 先不说理论,先看下边实例

用户表 user( uname-用户名,唯一 upwd- 密码 unickname-昵称)
用户信息表 user_info (uname-用户名,外键 tel-电话 email-邮箱 ......)

          假设我们有好多好多数据, 有可能上亿条,甚至十亿 百亿(绝对是史上最大的用户表了),我们怎么分这个数据表?
          按照什么分表? 当然 uname 是最佳的选择。 不为什么,就因为我们查询的时候最有可能用到这个字段做为唯一查询条件: select * from user where uname='XXX';
          方案1:
                按照用户名的最后两位来分表。用户名 hello 的用户就存在 user_lo 里边 , 用户名是world 的就存在 user_ld 里, 这样做的好处是:
                       1. 按照用户名有这样一个散列规则,如果知道用户名的话, 经过我们的散列规则算法一下就确定了数据保存在那张数据表中, 不需要再搜索
                       2. 数据表中的数据也基本的平均, 理论上边每个组合的用户名后两位是平均分布的(事实上是有差别的, 如果是自增的数字ID的话是基本上平均的),达到了我们分库分表的初衷
                但是这种算法也是有缺点的:
                      1. 数据表的组合从user_aa 到 user_zz (假如全由字母组成, 不区分大小写), 有 26*26张数据表, 数据表个数是固定的, 如果后期发现有些数据表的数据又比较多了, 要扩展的话就比较难了。
                      2. 统计起来比较困难。 要查询某一个时间段注册的用户, 这样的就比较困难了,这样的话就必须查询每一张数据表然后把结果合并起来。

             再来看另外一个例子: 订单。 大家可以发现京东上边是这样做的, 京东上边默认查询的是当月的订单, 然后所有的订单是另外查询的(卓越亚马逊等其他网站也有好多这么做的)。他们的分表应该是这样的, 数据肯定是要按月分的,你所有的订单他就让你再点一下才可以查到。我们模拟一下:
             因为用户只能查到自己的订单, 所以按照用户分表还是必要的, 对于京东来说,按照用户来分还不足以应付这么多的数据量, 所以我们就用一个更复杂一点的散列规则, 按照用户名和日期组合分表:
             order_date_name date 表示日期, name 表示用户名的后两位, 比如我这个月订单的数据表就存在 order_201103_ng, 根据我的名字 查询我当月的订单就去这帐数据表中查询, 查询我的所有订单就去 show tables like 'order_%_ng'; 我把的名字后缀的表先取出来, 然后再去 查询这些数据表, 最后合并结果。
             当然你可能会问, 后台统计的时候是不是太麻烦了, 我可以很负责地告诉你: 是! 这个没办法, 后台取统计数据的话可以延迟个几分钟, 老板不会说你, 但是如果让前台用户等级分钟老板绝对饶不了你!
             总之数据拆分的精髓就在于,根据实际情况将数据按照最合适的规则存储在不同的数据表中,尽量避免多个数据表的数据合并
             更多数据拆分的东西我们以后再讨论, 敬请继续关注我的博客,谢谢!

声明: 本文采用 CC BY-NC-SA 3.0 协议进行授权

转载请注明来源:小景的博客

本文链接地址:http://www.phpv5.com/blog/archives/109/

Statement
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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 Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.