From dc5e97aefcae65434eea897a130774a394da6079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=99=93=E4=B8=9C?= <3465672489@qq.com> Date: Fri, 18 Oct 2024 15:44:56 +0800 Subject: [PATCH 1/2] test --- ...40\344\270\216\344\275\234\344\270\232.md" | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 "\351\231\210\346\231\223\344\270\234/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/\351\231\210\346\231\223\344\270\234/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" "b/\351\231\210\346\231\223\344\270\234/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..6f5e900 --- /dev/null +++ "b/\351\231\210\346\231\223\344\270\234/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,97 @@ +## 一、笔记 + +索引:索引是一种数据结构,它的出现就是为了提高数据查询的效率。 + +以文本的形式存在磁盘中。 + +如何创建一个普通的索引(单列) + +create index idx_username on user_list(user_name); + +添加索引: + +alter table user_list add index idx_phone(phone); + +查看一个表的索引: + +show index fromuser_list; + +在建表的时候指定索引: + +create table aa( + +ID int + +index idx_id(id) + +); + +删除索引: + +drop index idx_id on aaa; + +alter table use_list drop index idx_phone; + +如何创建一个普通索引(多列) + +create index idx_uname_phone on user_list (user_name,phone); + +建立一个唯一索引(单列) + +create index idx_aaa on aaa(id); + +explain:可以查看有没有用到索引 + +```sql +-- 练习和作业 +-- 1.给emp分别建立 普通索引和唯一索引 +create index index1 on employee(eid); +create unique index index2 on employee(ename); +-- 2.查询emp表有哪些索引 +show index from employee; +-- 3. 使用有索引的字段进行查询,再查看这条语句是否使用到了索引。 +explain select *from employee WHERE eid=2; + +-- 4. 删除前面建立的两个索引 +drop index index1 on employee; +drop index index2 on employee; +-- 5. 选择两个字段添加一个复合索引 +create index index3 on employee(ename,birth); + +-- 6. 使用复合索引的字段进行查询 +SELECT *from employee WHERE ename="沈涛" and birth="1993-04-30"; + +-- 作业 +-- 想办法用自己的电脑,生成500万行数据,id,uname,age 尽量随机,并记录时间。 +CREATE table emp( +id int , +uname varchar(255), +age int +); + + +create procedure test1() +BEGIN +declare a int DEFAULT 0; +while a<5000000 do +insert into emp VALUES(a+1,CONCAT("用户",a+1),floor(rand()*100)); +set a=a+1; +end while; +end; + +call test1(); +-- 1. 不用索引查询 一次姓名uname /并记录时间 +SELECT *from emp where uname="用户4999978"; + +-- 2. 建立索引查询 一次姓名uname /并记录时间 +CREATE INDEX index5 ON emp (uname); +SELECT *from emp where uname="用户4999978"; + +``` + + 1.![不用索引 2024-10-18 153705](https://gitee.com/cxd20041029/picture-bed/raw/master/images/202410181543141.png) + +2. + +![屏幕截图 2024-10-18 153516](https://gitee.com/cxd20041029/picture-bed/raw/master/images/202410181543785.png) + -- Gitee From e754d02789e13280ff70fb7fdedc02a895392113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=99=93=E4=B8=9C?= <3465672489@qq.com> Date: Fri, 18 Oct 2024 15:59:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?pys=E5=A4=A7sb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\203\344\271\240\344\270\216\344\275\234\344\270\232.md" | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git "a/\351\231\210\346\231\223\344\270\234/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" "b/\351\231\210\346\231\223\344\270\234/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" index 6f5e900..5a17a16 100644 --- "a/\351\231\210\346\231\223\344\270\234/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" +++ "b/\351\231\210\346\231\223\344\270\234/20241018 \347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" @@ -89,7 +89,11 @@ SELECT *from emp where uname="用户4999978"; ``` - 1.![不用索引 2024-10-18 153705](https://gitee.com/cxd20041029/picture-bed/raw/master/images/202410181543141.png) +![微信图片_20241018155821](https://gitee.com/cxd20041029/picture-bed/raw/master/images/202410181558015.png) + + + +1.![不用索引 2024-10-18 153705](https://gitee.com/cxd20041029/picture-bed/raw/master/images/202410181543141.png) 2. -- Gitee