CREATE table if not exists Dy_User(
  Dy_No int not null auto_increment primary key comment '记录编号',
  Dy_Id varchar(18) not null comment '抖音号',
  Dy_Nm varchar(20) not null comment '抖音名称',
  Phone varchar(11) default null comment '手机号',
  Email varchar(20) default null comment '邮箱',
  Sex char(1) default 1 comment '性别',
  address varchar(100) default null comment '地址',
  Crt_tm TIMESTAMP comment '创建时间',
  Up_Tm TIMESTAMP default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '修改时间',
  unique index index_Dy_id (Dy_Id)
) engine=innodb,charset=utf8,comment '抖音用户表';

批量插入100万测试数据:
-- DELIMITER 测试了下多个存储过程,只需要在头部配置一个既可。 但是参考大家的结果是定义分隔符,缺少则创建失败
DELIMITER @
-- 创建存储过程  
create function fuc_batchInserData()
returns int
DETERMINISTIC -- MySQL8.0  需要加这个
begin
  declare num int default 1000000;
  declare i int default 1;
  while i <= num DO
    insert into Dy_User (Dy_Id,Dy_Nm,Phone,Email,Sex,address,Crt_tm)
    VALUES
    (UUID_SHORT(),CONCAT('用户',i),concat('183',floor((rand()*99999999))),'123456778@qq.com',floor(rand()*2),concat('黄河路',floor(rand()*999)),now());
    set i=i+1;
  end while;
  return i;
end;
-- 执行存储过程
select fuc_batchInserData();

explain SQL分析

explain
select * from Dy_User d where d.Dy_Id = '101528667519189664';

explain
select * from Dy_User d where d.Dy_Nm = '用户666'; 

查询索引:

show index from xxx

创建索引:

alter table dy_user
         add index index_dy_nm(dy_nm);

create index index_phone on dy_user(Phone);

删除索引:

drop index index_phone on dy_user;

alter table dy_user
    drop index index_dy_id;

常用的索引类型:

  1. 主键索引 primary key
  2. 唯一索引 unique index
  3. 普通索引 index

索引优点:

  1. 提高了查询、修改、删除的速度
  2. 优化分组和排序性能
  3. 唯一索引可以确定表中的每一行数据唯一性
  4. 全文索引可以显著提升大文本中全文搜索的效率

索引缺点:

  1. 每个索引都会占用额外的磁盘空间,存储负担增加
  2. 在执行插入、修改、删除操作时需要更新索引,维护成本增加
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐