Table of Contents
回复内容:
Home Backend Development PHP Tutorial 如何解决MySQL分表与新数据的插入

如何解决MySQL分表与新数据的插入

Jun 06, 2016 pm 08:48 PM
mysql nginx php sub-table

最近在网上看到了一个关于“mysql分表后如何插入新数据”的问题,对一个用户表进行分表,他的分表规则是按用户ID来分的,但是当有一条新数据要插入时,还没有用户ID,因此不知道插入哪一张分表中(因为只有当数据插入后才会有用户ID)。

我的想法是这样的:专门建一张表,用来存放所有用户ID,也就是说当有新数据插入时,先在这张表中产生一条记录,这条记录的主键就作为新用户ID,然后再根据这个用户ID来决定这个用户的注册数据插入到哪一张分表中。

这类问题,不知道大家是怎么处理的,想听听在家的做法……

回复内容:

最近在网上看到了一个关于“mysql分表后如何插入新数据”的问题,对一个用户表进行分表,他的分表规则是按用户ID来分的,但是当有一条新数据要插入时,还没有用户ID,因此不知道插入哪一张分表中(因为只有当数据插入后才会有用户ID)。

我的想法是这样的:专门建一张表,用来存放所有用户ID,也就是说当有新数据插入时,先在这张表中产生一条记录,这条记录的主键就作为新用户ID,然后再根据这个用户ID来决定这个用户的注册数据插入到哪一张分表中。

这类问题,不知道大家是怎么处理的,想听听在家的做法……

抱歉我忽视了用户名如今已经不是单一的登陆查询和用户识别的依据了。

现在这个时代,登录的依据如此庞杂(手机、邮箱、用户名、各种第三方认证),并且一个人往往有多个登录入口,总不可能把每个人的数据重复存储多次……

我们首先认可这个原则没有改变:查询什么,就必须根据什么来分表。那么在这个原则之下,就只好采取二级分表的方法了。

先建立一个前级表格,记录所有的登录依据,所对应的用户ID。例如这样:

<code> Criteria           UserID 
---------------------------
 testuser1          1
 testuser2          2
 1@example.com      1
 15200000001        1
 **weibo_auth_data  2
</code>
Copy after login

对这个表进行分表的方法,仍然使用对关键词做hash的方式就不错。归根到底,无论用户名、邮箱、手机等各种登陆方法,本质无非是凭一个字符串查询用户ID,体现多对一的关系。

而真正存储用户信息的地方,就按用户的唯一标识分表就行了。

注意我没有用“用户ID”这个说法。因为在分表的情况下,还使用合表时适用的单一数字递增ID,恐怕就不是什么好主意了。有一个简单的结论:分表,自然是性能实在合不上了才要分。所以分布式的结构中,无法再包含集中式的结构。提问中所说的“单独组织一个用户ID总表”的办法,显然是行不通的。

我倾向于把用户的这个唯一标识做成一个二元组
大的方面,用户注册时用Hash等任何算法,对用户的注册数据做个简单平摊,决定用户所在的桶编号iBucket
小的方面,在每个单独的分表中按单一递增ID,决定用户在表内的局部编号iID

查询用户的时候,一律用两个编号(iBucket, iID)决定一个用户。这样就既遵守了增加数据后才得到用户ID,又达到了分表的目的。

注意两点:

  1. 使用了Hash,别忘了一定要约束用户输入的最大长度,严防Hash冲撞攻击。
  2. 如果用户表真的大到了非分表不可的程度,实践中可能需要考虑采用NoSQL的数据库引擎,例如Redis。

哥们你真不嫌费劲吗?

分表的最大目的是为了高效查询,所以分表的依据,就必然和查询的关键字有直接的联系。从这个意义上看,除非你的用户用ID而不是用户名来登录,否则“按用户ID分表”本身就是一个馊的要死的主意。

我推荐的分表依据是对用户名做HASH,取HASH结果16进制串的头1位(或几位)。这样可以非常简单的把集合无限大的用户名,简单的分割成几个桶,同时各个桶之间无需维护就能达到非常好的均衡。

楼上的写的太复杂了吧。。。其实简单直白有效的做法就跟楼主写的差不多,单独用一张步长表,如果你分两个表的话,步长表的步长为2,表1从0开始每次加步长2,表2从1开始每次加步长2,这样就会取到表1和2各自的id又不会冲突,然后把这个生成的id记录到步长表中,然后每次算id的时候取出步长表的id加上步长,然后再把新的id写入到步长表中,拿到这个id后你就可以插入数据分表中了。

Statement of this Website
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

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles