From c18d31a4377e8d9f3a03446688f6ed301db2e716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=99=BA=E7=BF=94?= <2045602860@qq.com> Date: Thu, 19 Oct 2023 03:42:11 +0000 Subject: [PATCH] =?UTF-8?q?34=20=E5=88=98=E6=99=BA=E7=BF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘智翔 <2045602860@qq.com> --- .../\347\264\242\345\274\225.md" | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 "34 \345\210\230\346\231\272\347\277\224/\347\264\242\345\274\225.md" diff --git "a/34 \345\210\230\346\231\272\347\277\224/\347\264\242\345\274\225.md" "b/34 \345\210\230\346\231\272\347\277\224/\347\264\242\345\274\225.md" new file mode 100644 index 0000000..0d0e2fe --- /dev/null +++ "b/34 \345\210\230\346\231\272\347\277\224/\347\264\242\345\274\225.md" @@ -0,0 +1,166 @@ +# 笔记 + +### 索引 + + 帮助mysql高效获取数据的数据结构 + + 建立索引要花费对应的时间和硬盘空间 + +#### 单列索引 + + 普通索引语法 + +``` mysql +1.直接在已有表中创建索引 + create index 索引名 on 表名(列名); +2.修改表结构追加普通索引 + alter table 表名 add index 索引名(列名); +3.创建表时直接指定 + create table 表名( + index 索引名(列名) + ); +``` + +``` mysql +查看表的索引 + show index from 表名; +删除索引 + drop index 索引名 on 表名; + alter table 表名 drop index 索引名; + -- 并且删除表时,会一并删除表上的全部索引 +``` + + 唯一索引 + +``` mysql +1 create unique index 索引名 on 表名(列名); +2 alter table 表名 add unique 索引名(列名); +3 create table 表名( + unique [index] 索引名(列名) -- 唯一索引,一定有索引名,index可以省略 +); + -- 删除唯一索引与删除普通索引相同 +``` + + 主键索引 + + 主键索引与index关键字没有关系,创建主键索引就是创建主键约束。 + +删除主键:alter table 表名 drop primary key; + + + +#### 联合索引 + +``` mysql +create index 索引名 on 表名(列名1,列名2......) -- 普通联合索引 +create unique index 索引名 on 表名(列名1,列名2.....) -- 联合唯一索引 +``` + + + +### explain 执行计划 + + 可以用于查看某个语句的执行详细情况 + + explain select..... from...... + + + +#### 最左前缀法则(联合索引) + + 如果使用了联合索引,要遵守最左前缀法则。 + + 就是指查询从索引的最左列开始,不跳过索引中的列,如果跳过,后面的字段索引失效 + + + +#### sql提示 + +``` mysql +select * from 表名 use index(索引名) where.... -- 建议 +select * from 表名 ignore index(索引名) where.... -- 忽略 +select * from 表名 force index(索引名) where.... -- 强制 +``` + +# 作业 + +``` mysql +create database tt charset utf8; +use tt; +create table tb_user( + id int primary key auto_increment comment '主键', + name varchar(50) not null comment '用户名', + phone varchar(11) not null comment '手机号', + email varchar(100) comment '邮箱', + profession varchar(11) comment '专业', + age tinyint unsigned comment '年龄', + gender char(1) comment '性别 , 1: 男, 2: 女', + status char(1) comment '状态', + createtime datetime comment '创建时间' +) comment '系统用户表'; + + +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('吕布', '17799990000', 'lvbu666@163.com', '软件工程', 23, '1', '6', '2001-02-02 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('曹操', '17799990001', 'caocao666@qq.com', '通讯工程', 33, '1', '0', '2001-03-05 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('赵云', '17799990002', '17799990@139.com', '英语', 34, '1', '2', '2002-03-02 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('孙悟空', '17799990003', '17799990@sina.com', '工程造价', 54, '1', '0', '2001-07-02 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('花木兰', '17799990004', '19980729@sina.com', '软件工程', 23, '2', '1', '2001-04-22 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('大乔', '17799990005', 'daqiao666@sina.com', '舞蹈', 22, '2', '0', '2001-02-07 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('露娜', '17799990006', 'luna_love@sina.com', '应用数学', 24, '2', '0', '2001-02-08 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('程咬金', '17799990007', 'chengyaojin@163.com', '化工', 38, '1', '5', '2001-05-23 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('项羽', '17799990008', 'xiaoyu666@qq.com', '金属材料', 43, '1', '0', '2001-09-18 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('白起', '17799990009', 'baiqi666@sina.com', '机械工程及其自动化', 27, '1', '2', '2001-08-16 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('韩信', '17799990010', 'hanxin520@163.com', '无机非金属材料工程', 27, '1', '0', '2001-06-12 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('荆轲', '17799990011', 'jingke123@163.com', '会计', 29, '1', '0', '2001-05-11 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('兰陵王', '17799990012', 'lanlinwang666@126.com', '工程造价', 44, '1', '1', '2001-04-09 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('狂铁', '17799990013', 'kuangtie@sina.com', '应用数学', 43, '1', '2', '2001-04-10 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('貂蝉', '17799990014', '84958948374@qq.com', '软件工程', 40, '2', '3', '2001-02-12 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('妲己', '17799990015', '2783238293@qq.com', '软件工程', 31, '2', '0', '2001-01-30 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('芈月', '17799990016', 'xiaomin2001@sina.com', '工业经济', 35, '2', '0', '2000-05-03 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('嬴政', '17799990017', '8839434342@qq.com', '化工', 38, '1', '1', '2001-08-08 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('狄仁杰', '17799990018', 'jujiamlm8166@163.com', '国际贸易', 30, '1', '0', '2007-03-12 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('安琪拉', '17799990019', 'jdodm1h@126.com', '城市规划', 51, '2', '0', '2001-08-15 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('典韦', '17799990020', 'ycaunanjian@163.com', '城市规划', 52, '1', '2', '2000-04-12 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('廉颇', '17799990021', 'lianpo321@126.com', '土木工程', 19, '1', '3', '2002-07-18 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('后羿', '17799990022', 'altycj2000@139.com', '城市园林', 20, '1', '0', '2002-03-10 00:00:00'); +INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('姜子牙', '17799990023', '37483844@qq.com', '工程造价', 29, '1', '4', '2003-05-26 00:00:00'); + +-- 执行查询文件tb_user.sql,数据准备好了之后,完成如下需求: +-- +-- 1. name字段为姓名字段,该字段的值可能会重复,为该字段创建索引。 + create unique index tt_a on tb_user(name); +-- 2. phone手机号字段的值,是非空,且唯一的,为该字段创建唯一索引。 + alter table tb_user modify phone varchar(11) unique key; +-- 3. 为profession、age、status创建联合索引。 + create index tt_c on tb_user(profession,age,status); +-- 4. 为email建立合适的索引来提升查询效率。 + create unique index tt_d on tb_user(email); +-- 5. 查看tb_user表的所有的索引数据。 + show index from tb_user; + + +-- 建立一个数据和表(id,name,phone,sex,hobby),想办法插入100万条数据 +create table ttt( + id int primary key auto_increment, + name varchar(20), + phone varchar(11), + sex char(1), + hobby varchar(100) +); + +delimiter // +create procedure tt_idx(in a int) +begin + declare i int default 0; + while i