专栏名称: Java知音
专注于Java,推送技术文章,热门开源项目等。致力打造一个有实用,有情怀的Java技术公众号!
目录
相关文章推荐
庞门正道  ·  看似很软,其实很硬! ·  昨天  
庞门正道  ·  机械昆虫~变身! ·  2 天前  
字体设计  ·  2025年Logo十大设计趋势预测及解析 ·  4 天前  
优秀网页设计  ·  AI工具丨设计师刚需,可商用的AI生成背景图片库! ·  6 天前  
甘肃省文化和旅游厅  ·  雪后丹霞:半山素裹半山霓 ·  3 天前  
甘肃省文化和旅游厅  ·  雪后丹霞:半山素裹半山霓 ·  3 天前  
51好读  ›  专栏  ›  Java知音

谁说MySQL单表行数不要超过2000W?

Java知音  · 公众号  ·  · 2024-03-15 10:05

正文

背景

网上看了一篇文章《为什么说MySQL单表行数不要超过2000w》,亲自实践了一下,跟原作者有不同的结论。原文的结论是2000W左右性能会成指数级的下降,而我的结论是:随着数据量成倍地增加,查询的时间也刚好是成倍增加,是成正比的。

我并不会直接搬运网上的文章和结论,下边的实践过程是参考文章的实践方式进行优化的。原文的理论感觉是正确的,但为啥我实践的结果不支持他的理论?动手能力强的小伙伴,可以照的我的实践过程试试。

前置条件

查看sql语句执行时间和效率

show profiles; # 是mysql提供可以用来分析当前会话中语句执行的资源消耗情 况。可以用来SQL的调优测量。
select @@have_profiling; # 查看是否支持profiling

set profiling = 1; # 设置MySQL支持profile
select count(*) from tmp.person; #执行自己的sql语句;
show profiles; 就可以查到sql语句的执行时间;

效果如下

mysql> set profiling = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select count(*) from tmp.person;
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

mysql> show profiles;
+----------+------------+---------------------------------+
| Query_ID | Duration   | Query                           |
+----------+------------+---------------------------------+
|        1 | 0.00017775 | select count(*) from tmp.person |
+----------+------------+---------------------------------+
1 row in set, 1 warning (0.00 sec)

实验

建一张表

drop database if exists tmp;
create database tmp;
use tmp;
CREATE TABLE person(
    id int NOT NULL AUTO_INCREMENT PRIMARY KEY comment '主键',
    person_id tinyint not null comment '用户id',
    person_name VARCHAR(200) comment '用户名称',
    gmt_create datetime comment '创建时间',
    gmt_modified datetime comment '修改时间'
) comment '人员信息表';

插入一条数据

insert into person values(1,1,'user_1', NOW(), now());

利用 mysql 伪列 rownum 设置伪列起始点为 1

select (@i:=@i+1) as rownum, person_name from person, (select @i:=100) as init;
set @i=1;

运行下面的 sql,连续执行 20 次,就是 2 的 20 次方约等于 100w 的数据;执行 23 次就是 2 的 23 次方约等于 800w , 如此下去即可实现千万测试数据的插入,如果不想翻倍翻倍的增加数据,而是想少量,少量的增加,有个技巧,就是在 SQL 的后面增加 limit 条件,如 limit 100 控制将要新增的数据量。

insert into person(id, person_id, person_name, gmt_create, gmt_modified)
select @i:=@i+1,
left(rand()*10,10) as person_id,
concat('user_',@i%2048),
date_add(gmt_create,interval + @i*cast(rand()*100 as signed) SECOND),
date_add(date_add(gmt_modified,interval +@i*cast(rand()*100 as signed) SECOND), interval + cast(rand()*1000000 as signed) SECOND)
from person;

此处需要注意的是,也许你在执行到近 800w 或者 1000w 数据的时候,会报错:The total number of locks exceeds the lock table size,这是由于你的临时表内存设置的不够大,只需要扩大一下设置参数即可。

SET GLOBAL tmp_table_size =512*1024*1024; #(512M)
SET global innodb_buffer_pool_size= 1*1024*1024*1024; # (1G);

验证

select count(1) from person;
select count(1) from person where person_id =6;
show profiles;

优化测试

MySQL函数

ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)

这是因为mysql 默认不允许创建自定义函数(安全性的考虑),此时我们需要将参数 log_bin_trust_function_creators 设置为开启状态

show variables like 'log_bin_trust_function_creators';
set global log_bin_trust_function_creators = 1;

但这样只是临时设置,重启终端后该设置即会失效。如果要配置永久的,需要在配置文件的 [mysqld] 上配置以下属性: log_bin_trust_function_creators=1

-- 随机产生字符串
drop function if exists rand_string;  -- 先判断是否已存在同名函数,如果已存在则先删除
DELIMITER $$ -- 两个 $$ 表示结束
create function rand_string(n int) returns varchar(255)
begin
    declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    declare return_str varchar(255) default '';
    declare i int default 0;
    while i         set return_str = concat(return_str, substring(chars_str, floor(1+rand()*52), 1));
        set i=i+1;
    end while;
    return return_str;
end $$
DELIMITER ;
-- 随机生成编号
drop function if exists rand_num;
DELIMITER $$
create function rand_num() 
returns int(5)
begin
    declare i int default 0;
    set i=floor(100+rand()*10);
    return i;
end $$
DELIMITER ;

自定义函数的调用和其他普通函数的调用一样,示例如下:

select rand_string(5);
select rand_num();

一键测试

drop database if exists tmp;
create database tmp;
use tmp;
CREATE TABLE person(
    id int NOT NULL AUTO_INCREMENT PRIMARY KEY comment '主键',
    person_id tinyint not null comment '用户id',
    person_name VARCHAR(200) comment '用户名称',
    gmt_create datetime comment '创建时间',
    gmt_modified datetime comment '修改时间'
) comment 'user info';


SET @@profiling = 0;
SET @@profiling_history_size = 0;
SET @@profiling_history_size = 100; 
SET @@profiling = 1;
insert into person values(1,1,'user_1', NOW(), now());
show profiles;

set @i=1;
drop function if exists test_performance;
DELIMITER $$ #设置结束符
create function test_performance(num int) returns varchar(255)
    begin
        declare return_str varchar(255) default '';
        if(num > 0) then
                insert into person(id, person_id, person_name, gmt_create, gmt_modified)
                    select @i:=@i+1,
                    left(rand()*10,10) as person_id,
                    concat('user_',@i%2048),
                    date_add(gmt_create,interval + @i*cast(rand()*100 as signed) SECOND),
                    date_add(date_add(gmt_modified,interval +@i*cast(rand()*100 as signed) SECOND), interval + cast(rand()*1000000 as signed) SECOND)
                    from person limit num;
            else
                insert into person(id, person_id, person_name, gmt_create, gmt_modified)
                    select @i:=@i+1,
                    left(rand()*10,10) as person_id,
                    concat('user_',@i%2048),
                    date_add(gmt_create,interval + @i*cast(rand()*100 as signed) SECOND),
                    date_add(date_add(gmt_modified,interval +@i*cast(rand()*100 as signed) SECOND), interval + cast(rand()*1000000 as signed) SECOND)
                    from person;
        end if;
            
    select count(1) into return_str from person where person_id = "9";
    select count(1) into return_str from person;
    
        return return_str;
    end $$

DELIMITER ;

select test_performance(0); #2^1
select test_performance(0); #2^2
select test_performance(0); #2^3
select test_performance(0); #2^4
select test_performance(0); #2^5
select test_performance(0); #2^6
select test_performance(0); #2^7
select test_performance(0); #2^8
select test_performance(0); #2^9
select test_performance(0); #2^10
select test_performance(0); #2^11
select test_performance(0); #2^12
select test_performance(0); #2^13
select test_performance(0); #2^14
select test_performance(0); #2^15
select test_performance(0); #2^16
select test_performance(0); #2^17
select test_performance(0); #2^18
select test_performance(0); #2^19次方=524288
select test_performance(475712); #补上475712凑够100w
select test_performance(250000); #125w
select test_performance(0); #250w
select test_performance(0); #500w
select test_performance(0); #1kw
select test_performance(0); #2kw
select test_performance(0); #4kw
select test_performance(0); #8kw
select test_performance(0); #16kw
select test_performance(0); #32kw

实验结果

数据量 有查询条件 无查询条件
125w 0.1309075 0.08538975
250w 0.25213025 0.18290725
500w 0.4816255 0.35839375
1kw 0.94493875 0.6809015
2kw 1.878788 1.44631675
4kw 5.40815725 3.05356825
8kw 11.074242 6.6517985
16kw 22.753852 17.94861325
2kw 46.36041225 36.5971315

Figure_1

理论

单表数量限制

首先我们先想想数据库单表行数最大多大?

CREATE TABLE person(
id int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY comment '主键',
person_id tinyint not null comment '用户id',
person_name VARCHAR(200) comment '用户名称',
gmt_create datetime comment '创建时间',
gmt_modified datetime comment '修改时间'
) comment '人员信息表';

看看上面的建表 sql,id 是主键,本身就是唯一的,也就是说主键的大小可以限制表的上限,如果主键声明 int 大小,也就是 32 位,那么支持 2^32-1 ~~21 亿;如果是 bigint,那就是 2^62-1 ?(36893488147419103232),难以想象这个的多大了,一般还没有到这个限制之前,可能数据库已经爆满了!!

有人统计过,如果建表的时候,自增字段选择无符号的 bigint , 那么自增长最大值是 18446744073709551615,按照一秒新增一条记录的速度,大约什么时候能用完?

图片

表空间

下面我们再来看看索引的结构,对了,我们下面讲内容都是基于 Innodb 引擎的,大家都知道 Innodb 的索引内部用的是 B+ 树

图片

这张表数据,在硬盘上存储也是类似如此的,它实际是放在一个叫 person.ibd (innodb data)的文件中,也叫做表空间;虽然数据表中,他们看起来是一条连着一条,但是实际上在文件中它被分成很多小份的数据页,而且每一份都是 16K。

大概就像下面这样,当然这只是我们抽象出来的,在表空间中还有段、区、组等很多概念,但是我们需要跳出来看。

图片

页的数据结构

因为每个页只有 16K 的大小,但是如果数据很多,那一页肯定就放不下这些数据,那数据肯定就会被分到其他的页中,所以为了把这些页关联起来,肯定就会有记录前后页地址,方便找到对应页;同时每页都是唯一的,那就会需要有一个唯一标志来标记页,就是页号;

页中会记录数据所以会存在读写操作,读写操作会存在中断或者其他异常导致数据不全等,那就会需要有校验机制,所以里面还有会校验码,而读操作最重要的就是效率问题,如果按照记录一个个进行遍历,那肯定是很费劲的,所以这里面还会为数据生成对应的页目录(Page Directory); 所以实际页的内部结构像是下面这样的。

图片

从图中可以看出,一个 InnoDB 数据页的存储空间大致被划分成了 7 个部分,有的部分占用的字节数是确定的,有的部分占用的字节数是不确定的。

在页的 7 个组成部分中,我们自己存储的记录会按照我们指定的行格式存储到 User Records 部分。

但是在一开始生成页的时候,其实并没有 User Records 这个部分,每当我们插入一条记录,都会从 Free Space 部分,也就是尚未使用的存储空间中申请一个记录大小的空间划分到 User Records 部分,当 Free Space 部分的空间全部被 User Records 部分替代掉之后,也就意味着这个页使用完了,如果还有新的记录插入的话,就需要去申请新的页了。这个过程的图示如下。

图片

刚刚上面说到了数据的新增的过程。

那下面就来说说,数据的查找过程,假如我们需要查找一条记录,我们可以把表空间中的每一页都加载到内存中,然后对记录挨个判断是不是我们想要的,在数据量小的时候,没啥问题,内存也可以撑;但是现实就是这么残酷,不会给你这个局面;为了解决这问题,mysql 中就有了索引的概念;大家都知道索引能够加快数据的查询,那到底是怎么个回事呢?下面我就来看看。

索引的数据结构

在 mysql 中索引的数据结构和刚刚描述的页几乎是一模一样的,而且大小也是 16K, 但是在索引页中记录的是页 (数据页,索引页) 的最小主键 id 和页号,以及在索引页中增加了层级的信息,从 0 开始往上算,所以页与页之间就有了上下层级的概念。

图片

看到这个图之后,是不是有点似曾相似的感觉,是不是像一棵二叉树啊,对,没错!它就是一棵树,只不过我们在这里只是简单画了三个节点,2 层结构的而已,如果数据多了,可能就会扩展到 3 层的树,这个就是我们常说的 B+ 树,最下面那一层的 page level =0, 也就是叶子节点,其余都是非叶子节点。

图片

看上图中,我们是单拿一个节点来看,首先它是一个非叶子节点(索引页),在它的内容区中有 id 和 页号地址两部分,这个 id 是对应页中记录的最小记录 id 值,页号地址是指向对应页的指针;而数据页与此几乎大同小异,区别在于数据页记录的是真实的行数据而不是页地址,而且 id 的也是顺序的。

单表建议值

下面我们就以 3 层,2 分叉(实际中是 M 分叉)的图例来说明一下查找一个行数据的过程。







请到「今天看啥」查看全文


推荐文章
庞门正道  ·  看似很软,其实很硬!
昨天
庞门正道  ·  机械昆虫~变身!
2 天前
字体设计  ·  2025年Logo十大设计趋势预测及解析
4 天前
甘肃省文化和旅游厅  ·  雪后丹霞:半山素裹半山霓
3 天前
甘肃省文化和旅游厅  ·  雪后丹霞:半山素裹半山霓
3 天前
行业研究报告  ·  跨境电商研究报告
7 年前