测试记录-MySQL复合索引与单索引的比较

测试1

信息

MySQL版本:8.0.24
时间:20230310
腾讯视频,《星汉灿烂》的弹幕数据共291万条。

建表

表1 复合主键

create table qq_danmu_test1(
vid varchar(60) not null comment '视频id',
danmu_id bigint not null comment '弹幕id',
pub_time int null comment '发弹幕的现实时间, 10位时间戳',
time_offset int null comment '弹幕在视频中出现的时间, 单位为秒',
content varchar(100) null comment '弹幕内容',
up_count int null comment '弹幕的赞数',
create_time timestamp default CURRENT_TIMESTAMP null comment '数据入库时间',
primary key (vid, danmu_id)
)comment '复合索引';

表2 主键+索引

create table qq_danmu_test2(
vid varchar(60) not null comment '视频id',
danmu_id bigint not null comment '弹幕id' primary key,
pub_time int null comment '发弹幕的现实时间, 10位时间戳',
time_offset int null comment '弹幕在视频中出现的时间, 单位为秒',
content varchar(100) null comment '弹幕内容',
up_count int null comment '弹幕的赞数',
create_time timestamp default CURRENT_TIMESTAMP null comment '数据入库时间',
index (vid)
)comment '主键+索引';
测试
# 插入测试数据
insert into qq_danmu_test1 select * from qq_danmu;
# 全量计数
select count(1) from qq_danmu_test1;
# 分组计数
select vid, count(1) from qq_danmu_test1 group by vid;
# 非索引字段查询
select content from qq_danmu_test1 where vid="取某个vid";
统计
复合主键 qq_danmu_test1主键+索引 qq_danmu_test2
插入测试数据37s1min56s
全量计数184ms122ms
分组计数800ms756ms
非索引字段查询76ms73ms
表空间占用277M601M

记录的时间均为初次运行时间。再次运行因为缓存的缘故会更短。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值