一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法。首先,我们需要想好到底分多少个 表,前提当然是满足应用。这里我使用了一个比较简单的分表方法,就是根据自增id的尾数来分,也就是说分0-9一共10个表,其取值也很好做,就是对10 进行取模。另外,还可以根据某一字段的md5值取其中几位进行分表,这样的话,可以分的表就很多了。
好了,先来创建表吧,代码如下:
CREATE TABLE `ttlsa_com`.`article_0` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_1` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_2` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_3` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_4` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_5` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_6` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_7` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_8` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `ttlsa_com`.`article_9` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
好了10个表创建完毕了,需要注意的是,这里的id不能设为自增,而且所有的表结构必须一致,包括结构,类型,长度,字段的顺序都必须一致那么对于这个id如何取得呢?后面我会详细说明。现在,我们需要一个合并表,用于查询,创建合并表的代码如下:
CREATE TABLE `ttlsa_com`.`article` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE=MRG_MyISAM DEFAULT CHARSET=utf8 INSERT_METHOD=0 union =(`article_0`,`article_1`,`article_2`,`article_3`,`article_4`,`article_5`,`article_6`,`article_7`,`article_8`,`article_9`);
注意,合并表也必须和前面的表有相同的结构,类型,长度,包括字段的顺序都必须一致这里的INSERT_METHOD=0表示不允许对本表进行 insert操作。好了,当需要查询的时候,我们可以只对article这个表进行操作就可以了,也就是说这个表仅仅只能进行select操作,那么对于 插入也就是insert操作应该如何来搞呢,首先就是获取唯一的id了,这里就还需要一个表来专门创建id,代码如下:
CREATE TABLE `ttlsa_com`.`create_id` ( `id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE = MYISAM
也就是说,当我们需要插入数据的时候,必须由这个表来产生id值,我的php代码的方法如下:
<?php function get_AI_ID() { $sql = "insert into create_id (id) values('')"; $this->db->query($sql); return $this->db->insertID(); } ?>
好了,现在假设我们要插入一条数据了,应该怎么操作呢?还是继续看代码吧
<?php function new_Article() { $id = $this->get_AI_ID(); $table_name = $this->get_Table_Name($id); $sql = "insert into {$table_name} (id,subject,content) values('{$id}','测试标题','测试内容')"; $this->db->query($sql); } /** * 用于根据id获取表名 */ function get_Table_Name($id) { return 'article_'.intval($id)%10; } ?>
其实很简单的,对吧,就是先获取id,然后根据id获取应该插入到哪个表,然后就很简单了。
对于update的操作我想应该不需要再说了吧,无非是有了id,然后获取表名,然后进行update操作就好了。
对于用户表,建个最少列最基本信息的用户名,比如用户ID,用户名,密码。用户的其他信息分布到以用户ID分表的表上。
怎么分表如何分表以业务需求而定。
你可以根据id分,也可以根据年,月,地区来分。要按照业务需求。
以上所述是小编给大家介绍的PHP操作mysql数据库分表的方法,希望对大家有所帮助,如果大家有不同的见解欢迎提出,共同学习进步!

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
