From dec9e6d3f98578212336cc1a0477873e588c3069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E5=85=88=E4=BC=98?= <1967671450@qq.com> Date: Fri, 18 Oct 2024 16:27:36 +0800 Subject: [PATCH] test --- ...40\344\270\216\344\275\234\344\270\232.md" | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 "\345\255\231\345\205\210\344\274\230/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" diff --git "a/\345\255\231\345\205\210\344\274\230/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" "b/\345\255\231\345\205\210\344\274\230/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" new file mode 100644 index 0000000..8a5113a --- /dev/null +++ "b/\345\255\231\345\205\210\344\274\230/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" @@ -0,0 +1,133 @@ +# 笔记 + +```sql +-- 普通索引 +create index 索引名 on 表名(字段名);-- 括号里可以有多个字段,运行时遵行左边优先 +-- 查询索引 +show index from 表名; +-- 添加索引(多列) +alter table 表名 add index 索引名(字段名1,字段名2……); +-- 删除索引 +drop index 索引名 on 表名; +-- 在建表时指定索引 +create table 表名( +id int +index 索引名(字段名) +); +-- 建立唯一索引(单列) +create unique index 字段名 on 表名(字段名); +-- 查看有没有用到索引 +explain + 查询语句; +``` + + + + + +# 练习 + +```sql +CREATE DATABASE if not exists emp_mp; +use emp_mp; + +create table if not exists `employee` +( + `eid` int not null auto_increment comment '员工id' primary key, + `ename` varchar(20) not null comment '员工名称', + `dname` varchar(50) not null comment '部门名称', + `hiredate` datetime not null comment '入职日期', + `birth` date not null comment '生日', + `salary` double null comment '基本薪资', + `start_sal` double null comment '入职薪资' +); + +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('傅嘉熙', '开发部', '2002-08-20 12:00:04','1980-12-10', 9000,6500); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('武晟睿', '开发部', '2002-06-12 13:54:12', '1984-2-5',9500,6000); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('孙弘文', '开发部', '2003-10-16 08:27:06','1979-8-7', 9400,8000); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('潘乐驹', '开发部', '2004-04-22 03:56:11','1980-5-12', 9500,6800); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('潘昊焱', '人事部', '2007-02-24 03:40:02','1987-2-12', 5000,4500); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('沈涛', '人事部', '2012-12-14 09:16:37','1993-4-30', 6000,5500); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('江峻熙', '人事部', '2018-05-12 01:17:48','1990-6-8', 5000,3000); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('陆远航', '人事部', '2018-04-14 03:35:57','1989-11-13', 5500,5000); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('姜煜祺', '销售部', '2020-03-23 03:21:05','1995-1-1', 6000,5500); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('邹明', '销售部', '2015-11-23 23:10:06','1996-2-19', 6800,6000); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('董擎苍', '销售部', '2012-02-12 07:54:32','1985-10-7', 6500,4800); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('钟俊驰', '销售部', '2010-04-10 12:17:06','1981-3-25', 6000,3500); + +-- 练习和作业 +-- 1.给emp分别建立 普通索引和唯一索引 +create index emp_ename on employee(ename); +CREATE unique index emp_id on employee(eid); + +-- 2.查询emp表有哪些索引 +show index from employee; + +-- 3. 使用有索引的字段进行查询,再查看这条语句是否使用到了索引。 +explain select * from employee where eid=2; + +-- 4. 删除前面建立的两个索引 +drop index emp_ename on employee; +drop index emp_id on employee; + +-- 5. 选择两个字段添加一个复合索引 +CREATE index emp_fh on employee(eid,ename); + +-- 6. 使用复合索引的字段进行查询 +select * from employee where eid=3 and ename='孙弘文'; + + +``` + + + +# 作业 + +-- 作业 +-- 想办法用自己的电脑,生成500万行数据,id,uname,age 尽量随机,并记录时间。 + +```sql +create table suiji( +id int auto_increment PRIMARY key, +uname VARCHAR(255) not null, +age int not null +); + +select now();-- 开始的时间 +create procedure wbw() +begin + DECLARE i int default 0; + while i<5000000 do + insert into suiji(uname,age) values + ( + concat('user',i), + floor(rand()*100) + ); + set i=i+1; + end while; + +end; +call wbw(); + + +``` + +![22042e1268f92757c5046da3fe7b13c](https://gitee.com/sxy20031002/picture-bed-warehouse/raw/master/imgss/202410181608978.png) + +-- 1. 不用索引查询 一次姓名uname /并记录时间 + +```sql +select uname from suiji; +``` + +![a52fcdf01240ec58a37278bae281a78](https://gitee.com/sxy20031002/picture-bed-warehouse/raw/master/imgss/202410181608487.png) + +-- 2. 建立索引查询 一次姓名uname /并记录时间 + +```sql +CREATE index emp_uname on suiji(uname); +SELECT * from suiji where uname='user22'; +``` + +![e43a342296e6d9ad34631fc9480d382](https://gitee.com/sxy20031002/picture-bed-warehouse/raw/master/imgss/202410181608461.png) + +![cbada65fd5d1eb861aa0d6a84dc41f9](https://gitee.com/sxy20031002/picture-bed-warehouse/raw/master/imgss/202410181609471.png) -- Gitee