用户信息表水平拆表方案
下面方法就是利用用户ID进行取模,MOD(new.id,3),3就代表拆分到3张表里。以下是步骤:第一步、在主库上(M)建立拆分的三张表,t0,t1,t2createtablet0liket;creat
下面
方法就是利用用户ID进行取模,MOD(new.id,3),3就代表拆分到3张表里。
以下是步骤:
第一步、在主库上(M)建立拆分的三张表,t0,t1,t2
第二步、在主库上(M)建立三个触发器(插入、更新、删除)
这样t表数据变更时,就会按照取模的结果更新到t0,t1,t2这三张表里
第三步、确保同步复制延时为0时,在从库上(S)stop slave关闭同步复制,并show slave status\G记录到文本里,以便后面时用到。
第四步、在从库上新建一个实例(S2),并导出t,t0,t1,t2四张表(导出时只要数据,不要表结构)
--dump-slave是MySQL5.5的一个特性,香港空间,具体你可参考我这一篇文章
如果你是5.1的,就要用到第三步我们记录的show slave status\G到文本,找到change master to的那个点,这样就保证了拆分后的增量数据。
第五步,在从库上S2实例上,创建表t,t0,t1,t2,也同样建立那三个触发器,这样做的目的是,当我们导入的时候,就会通过触发器取模,把数据分散到t0,t1,t2这三张表里,然后开启同步复制主库M。
第六步,上面五步做完以后,香港服务器,请检查下数据的增长情况,并观察同步是否正常,都没问题的话,就可以把数据分散到新的3台服务器里。
在S2实例上,把t0表dump出来并导入到M_new1机器上,然后在M_new1机器上,在my.cnf里增加
忽略t1,t2表的同步复制。
把t1表dump出来并导入到M_new2机器上,然后在M_new2机器上,在my.cnf里增加
忽略t0,t2表的同步复制。
把t2表dump出来并导入到M_new3机器上,然后在M_new3机器上,在my.cnf里增加
忽略t0,t1表的同步复制。
第七步,美国服务器,到目前为止,基本上我们已经做完了,剩下的就要开发改他们的代码,告诉他们拆分的规则,按用户ID取模,拆分几个表,拆分到哪台机器上,IP是什么。
等凌晨0:00点,开发让运维重启前端应用,这样就顺利实现了用户信息表的拆分。
过程可能有些复杂,有兴趣的朋友,可以按照我上面的操作,做下测试。
本文出自 “贺春旸的技术专栏” 博客,请务必保留此出处

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

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.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.
