From f1e539148a66a6a78d7ea18866c5d5135acede9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B9=B2=E7=BA=AF=E6=AC=A3?= <1759931586@qq.com> Date: Sat, 19 Oct 2024 17:40:49 +0800 Subject: [PATCH] =?UTF-8?q?20241018=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024.1018 \347\264\242\345\274\225.md" | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 "\345\271\262\347\272\257\346\254\243/2024.1018 \347\264\242\345\274\225.md" diff --git "a/\345\271\262\347\272\257\346\254\243/2024.1018 \347\264\242\345\274\225.md" "b/\345\271\262\347\272\257\346\254\243/2024.1018 \347\264\242\345\274\225.md" new file mode 100644 index 0000000..af4f490 --- /dev/null +++ "b/\345\271\262\347\272\257\346\254\243/2024.1018 \347\264\242\345\274\225.md" @@ -0,0 +1,130 @@ +# #1018 索引 + +#### 一、什么是索引? + +索引是一个排序的列表,在这个列表中存储着索引的值和包含这个值的数据所在的物理地址,可以大大加快查询的速度,索引的作用类似于书的目录,可以根据目录中的页码快速找到所需的内容. + +#### 二、索引的分类 + +逻辑角度来划分索引分为普通索引、唯一索引、主键索引、组合索引和全文索引。 + +**普通索引** + +```sql +方法一 +create index index_name on table_name(字段); +方法二 +在创建一个表结构时就创建索引 +create table +``` + +**唯一索引** + +```sql +create unique index unique_name on table_name(字段); +见名知义,索引列中的值必须是唯一的,但是允许为空值,其中字段就是唯一索引 +``` + +**主键索引** + +```sql +当把某个列设为主键的时候则该列就是主键索引 +create table a ( +id int primary key auto_increment, +name varchar(255) not null default +); +或者 +alter table a add primary key(id) +``` + +**组合索引** + +```sql +用多个列组合构建的索引 +alter table table_name add index index_name(a,b,c); +组合索引遵循最左原则,使用时最好把最常用作为检索或者排序的列放在最左,依次递减 +``` + +-- 练习和作业 +-- 1.给emp分别建立 普通索引和唯一索引 + +```sql +create index index_job on emp(job); +create unique index index_ename on emp(ename); +``` + +-- 2.查询emp表有哪些索引 + +```sql +show index from emp ; +``` + +-- 3. 使用有索引的字段进行查询,再查看这条语句是否使用到了索引。 + +```sql +explain select * from emp where ename = '刘备'; +``` + +-- 4. 删除前面建立的两个索引 + +```sql +drop index index_job on emp; +drop index index_ename on emp; +show index from emp; +``` + +-- 5. 选择两个字段添加一个复合索引 + +``` +create index index_two on emp(ename,job); + +``` + +-- 6. 使用复合索引的字段进行查询 + +``` +select * from emp where ename = '刘备' and job = '经理'; + +``` + +-- 作业 +-- 想办法用自己的电脑,生成500万行数据,id,uname,age 尽量随机,并记录时间。 + +```sql +CREATE TABLE tab_random( + id int primary key auto_increment , + uname varchar(255), + age int +); +drop procedure if exists pro_random; +delimiter // +create procedure pro_random() +begin +declare i int default 0; +while i < 5000000 do +insert into tab_random(uname,age) +values(floor(rand()*500000),floor(rand()*100)); +set i = i+1; +end while; +end// +delimiter; +call pro_random(); +``` + +![image-20241019172003188](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202410191720257.png) + +-- 1. 不用索引查询 一次姓名uname /并记录时间 + +```sql +select * from tab_random where uname = '263027'; +``` + +![image-20241019173637447](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202410191736488.png) + +-- 2. 建立索引查询 一次姓名uname /并记录时间 + +```sql +create index index_uname on tab_random(uname); +``` + +![image-20241019173905089](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202410191739180.png) \ No newline at end of file -- Gitee