From a5a23eb0ec1037a4598728391af6b7dda40fc76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=8E=AE=E9=93=AD?= <2373854303@qq.com> Date: Fri, 18 Oct 2024 12:57:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A41018=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\345\274\225\347\273\203\344\271\240.md" | 221 ++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 "\350\224\241\347\216\256\351\223\255/20241018 \347\264\242\345\274\225\347\273\203\344\271\240.md" diff --git "a/\350\224\241\347\216\256\351\223\255/20241018 \347\264\242\345\274\225\347\273\203\344\271\240.md" "b/\350\224\241\347\216\256\351\223\255/20241018 \347\264\242\345\274\225\347\273\203\344\271\240.md" new file mode 100644 index 0000000..7b2c6f2 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241018 \347\264\242\345\274\225\347\273\203\344\271\240.md" @@ -0,0 +1,221 @@ +## 课堂笔记: + +#### 索引: + +1. ##### 概念 + + 索引是数据库表中一个特殊的数据结构,用于快速查找和访问表中的数据。它类似于书籍的目录。加速数据检索,优化查询性能,避免全表扫描。 + +2. ##### 类型 + + - 单列索引:只包含单个列的索引。 + - 复合索引:由多个列组成的索引,用于加速涉及多个列的查询,使用要遵循最左原则。 + - 唯一索引:确保索引列中的值是唯一的,不允许重复。 + - 主键索引:一种特殊的唯一索引,且不允许为空,通常用于唯一标识一行数据。 + - 全文索引:用于对文本数据进行全文检索,主要用于查找大文本字段中的关键词。 + - 空间索引:用于地理数据类型的索引,加速空间查询。 + +3. ##### 语法: + + ```sql + -- 创建索引 + create index 索引名 on 表名(字段名); + alter table 表名 add index 索引名(字段名); + + -- 建表时 + create table 表名( + id int primary key auto_increment, + 字段名 类型, + index 索引名 (字段名) + ); + + -- 删除索引 + drop index 索引名 on 表名; + alter table 表名 drop index 索引名; + + -- 查看查询的执行计划,分析索引的使用情况 + explain select * from 表名 WHERE 条件; + ``` + +## 课堂练习: + +-- 练习和作业 +-- 1.给emp分别建立 普通索引和唯一索引 + +```sql +-- 建立 普通索引 +create index index_hiredate on emp(hiredate); + +-- 建立 唯一索引 +create unique index index_ename on emp(ename); +``` + +![image-20241018103728976](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241018_1729219049.png) + +-- 2.查询emp表有哪些索引 + +```sql +-- 查询emp表有哪些索引 +show index from emp; +``` + +![image-20241018103933901](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241018_1729219174.png) + +-- 3. 使用有索引的字段进行查询,再查看这条语句是否使用到了索引。 + +```sql +-- 使用有索引的字段进行查询 +select * from emp where ename = '关羽'; + +-- 查看这条语句是否使用到了索引 +explain select * from emp where ename = '关羽'; +``` + +![image-20241018104131695](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241018_1729219291.png) + +![image-20241018104147819](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241018_1729219307.png) + +-- 4. 删除前面建立的两个索引 + +```sql +-- 删除前面建立的两个索引 +drop index index_hiredate on emp; +drop index index_ename on emp; +``` + +![image-20241018104307822](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241018_1729219387.png) + +-- 5. 选择两个字段添加一个复合索引 + +```sql +-- 选择两个字段添加一个复合索引 +create index index_ename_job on emp(ename,job); +``` + +![image-20241018104627221](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241018_1729219597.png) + +-- 6. 使用复合索引的字段进行查询 + +```sql +-- 使用复合索引的字段进行查询 +select * from emp where ename = '关羽' and job = '经理'; + +-- 查看这条语句是否使用到了索引 +explain select * from emp where ename = '关羽' and job = '经理'; +``` + +![image-20241018104743638](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241018_1729219663.png) + +![image-20241018104814145](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241018_1729219694.png) + +-- 作业 +-- 想办法用自己的电脑,生成500万行数据,id,uname,age 尽量随机,并记录时间。 + +```sql +-- 判断是否存在并删除 +drop function if exists rand_name; + +-- 修改分隔符 +delimiter // + +-- 创建随机生成姓名函数 rand_name +create function rand_name(n int) returns varchar(10) +deterministic +begin + -- 初始化一个16姓氏字符串,作为姓氏字符库 + declare family_str varchar (128) default '赵钱孙李周吴郑王冯陈蒋沈韩杨朱秦肖'; + -- 初始化一个32名字字符串,作为名字字符库 + declare name_str varchar (128) default '平书文若山向秋凡白斌绮烟从蕾天曼润又亦从语绮彤之玉凡梅依琴沛槐敏飞鸟'; + -- 记录当前是第几个 + declare i int default 0; + -- 记录生成结果 + declare full_name varchar(10) default ''; + -- 随机名字1、2位标记 + declare rand_num int default 0; + + while i < n do + -- 若获取多个姓名,则用逗号','区分 + set full_name = if(i > 0, concat(full_name, ','), full_name); + -- 随机取姓氏 + set full_name = concat(full_name, SUBSTR(family_str, floor(1+rand()*16), 1)); + -- 随机取名字 + set full_name = concat(full_name, SUBSTR(name_str, floor(1+rand()*16), 1)); + -- 名字是否为双字 + set rand_num = rand()*10; + set full_name = if(rand_num > 5, concat(full_name, SUBSTR(name_str, floor(1+rand()*16), 1)), full_name); + set i = i + 1; + end while; + + -- 返回姓名 + return full_name; + +end // + +-- 还原分隔符 +delimiter ; + +-- 判断是否存在并删除存储过程 +drop procedure if exists rand_insert; + +-- 修改分隔符 +delimiter // + +create procedure rand_insert() +begin + -- 记录当前是第几个 + declare i int default 0; + -- 定义一个年龄变量 + declare age_number int; + + -- 新建表 + create table user_info( + id int primary key auto_increment, + username varchar(10), + age int + ); + + + -- 开启事务 + start transaction; + + -- 循环生成数据 + while i < 5000000 do + -- 当前次数增加 + set i = i + 1; + + -- 插入数据 + insert into user_info(username,age) + values(rand_name(1),floor(1+rand()*101)); + end while; + + -- 事物提交 + commit; +end // + +-- 还原分隔符 +delimiter ; + +-- 调用存储过程 +call rand_insert(); +``` + +![image-20241018122037664](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202410181220697.png) + +-- 1. 不用索引查询 一次姓名uname /并记录时间 + +```sql +-- 不用索引查询 一次姓名uname /并记录时间 +select * from user_info where username = '王斌'; +``` + +![image-20241018122252429](C:/Users/23738/AppData/Roaming/Typora/typora-user-images/image-20241018122252429.png) + +-- 2. 建立索引查询 一次姓名uname /并记录时间 + +```sql +-- 建立索引查询 一次姓名uname /并记录时间 +create index index_user_ename on user_info(username); +select * from user_info where username = '王斌'; +``` + +![image-20241018124259154](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202410181242202.png) -- Gitee