用户信息表水平拆表方案
下面方法就是利用用户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.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

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.

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.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.
