diff --git "a/\345\256\213\345\207\257/2220241018\347\264\242\345\274\225\347\273\203\344\271\240\344\270\216\344\275\234\344\270\232.md" "b/\345\256\213\345\207\257/2220241018\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 0000000000000000000000000000000000000000..850d117df7dfbf2624f98918fc71744a73317711 --- /dev/null +++ "b/\345\256\213\345\207\257/2220241018\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,53 @@ +``` +单列索引 +create index 索引名 on 表名(字段); +多列索引 +create index 索引名 on 表名(字段1,字段2); +唯一索引 +create unique index 字段名 on 表名(字段); +删除索引 +drop index 索引名 on 表名; +查看索引 +show index from 表名; +-- 练习和作业 +-- 1.给emp分别建立 普通索引和唯一索引 +create index ind_emp on emp (ename); +create unique index inde_emp on emp (empno); +-- 2.查询emp表有哪些索引 +show index from emp; +-- 3. 使用有索引的字段进行查询,再查看这条语句是否使用到了索引。 +select * from emp where ename = '诸葛亮'; +-- 4. 删除前面建立的两个索引 +drop index ind_emp on emp; +drop index inde_emp on emp; +-- 5. 选择两个字段添加一个复合索引 +create index index_emp on emp (ename,sal); +-- 6. 使用复合索引的字段进行查询 +select * +from emp where ename = '诸葛亮' and sal = 30000; +-- 作业 +-- 想办法用自己的电脑,生成500万行数据,id,uname,age 尽量随机,并记录时间。 +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; +``` + + + +``` +-- 1. 不用索引查询 一次姓名uname /并记录时间 +select * from emp where ename = '用户3' +``` + + + +``` +-- 2. 建立索引查询 一次姓名uname /并记录时间 +CREATE index ie_ename on emp (ename); +``` +