一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法。目前我所知道的方法都是MYISAM的,INNODB如何做分表并且保留事务和外键,我还不是很了解。
首先,我们需要想好到底分多少个表,前提当然是满足应用。这里我使用了一个比较简单的分表方法,就是根据自增id的尾数来分,也就是说分0-9一共10个表,其取值也很好做,就是对10进行取模。另外,还可以根据某一字段的md5值取其中几位进行分表,这样的话,可以分的表就很多了。
好了,先来创建表吧,代码如下

CREATE TABLE `test`.`article_0` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_1` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_2` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_3` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_4` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_5` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_6` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_7` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_8` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `test`.`article_9` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci

好了10个表创建完毕了,需要注意的是,这里的id不能设为自增,而且所有的表结构必须一致,包括结构,类型,长度,字段的顺序都必须一致那么对于这个id如何取得呢?后面我会详细说明。现在,我们需要一个合并表,用于查询,创建合并表的代码如下

CREATE TABLE `test`.`article` ( `id` BIGINT( <strong>20</strong> ) NOT NULL , `subject` VARCHAR( <strong>200</strong> ) NOT NULL , `content` TEXT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE=CREATE TABLE user ( `id` int(11) NOT NULL PRIMARY key, `username` varchar(20) NOT NULL, `passwd` char(32) NOT NULL) ENGINE=MERGE DEFAULT CHARSET=utf8 INSERT_METHOD=no 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_METHOD=NO
注意,合并表也必须和前面的表有相同的结构,类型,长度,包括字段的顺序都必须一致这里的INSERT_METHOD=0表示不允许对本表进行insert操作。好了,当需要查询的时候,我们可以只对article这个表进行操作就可以了,也就是说这个表仅仅只能进行select操作
那么对于插入也就是insert操作应该如何来搞呢,首先就是获取唯一的id了,这里就还需要一个表来专门创建id,代码如下
CREATE TABLE `test`.`create_id` ( `id` BIGINT( <strong>20</strong> ) NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE = MYISAM
也 就是说,当我们需要插入数据的时候,必须由这个表来产生id值,我的php代码的方法如下
function get_AI_ID() { $sql = "insert into create_id (id) values('')"; $this->db->query($sql); return $this->db->insertID(); }
好了,现在假设我们要插入一条数据了,应该怎么操作呢?还是继续看代码吧

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操作就好了。

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.

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.

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 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 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 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.

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.

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


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

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

WebStorm Mac version
Useful JavaScript development tools