diff --git "a/\351\231\210\346\226\207\347\220\263/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\226\207\347\220\263/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 0000000000000000000000000000000000000000..4ef13079f03002f1f77471e1311694d4377418cc --- /dev/null +++ "b/\351\231\210\346\226\207\347\220\263/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,92 @@ +## 窗口函数练习与作业 + +### SQL语句: + +``` +drop database if exists db1; + +CREATE database if not exists db1; + +use db1; + +drop TABLE if exists emp; + +CREATE TABLE if not exists emp( + +id int auto_increment PRIMARY key, +ename VARCHAR(20) not null, +age int not null + +); + +-- 练习和作业 +-- 1.给emp分别建立 普通索引和唯一索引 + +CREATE index idx_num1 on emp(id); +CREATE UNIQUE index idx_uni1 on emp(ename); + +-- 2.查询emp表有哪些索引 + +show index from emp; + +-- 3. 使用有索引的字段进行查询,再查看这条语句是否使用到了索引 + +SELECT * from emp WHERE id > 2; +explain SELECT * from emp WHERE id > 2; + +-- 4. 删除前面建立的两个索引 + +drop index idx_num1 on emp; +drop index idx_uni1 on emp; + +-- 5. 选择两个字段添加一个复合索引 + +CREATE index idx_num2 on emp(id,age); + +-- 6. 使用复合索引的字段进行查询 + +SELECT * from emp where id > 1 and age >12; + +EXPLAIN SELECT * from emp where id > 1 and age >12; + +-- 作业 +-- 想办法用自己的电脑,生成500万行数据,id,uname,age 尽量随机,并记录时间。 +-- 1. 不用索引查询 一次姓名uname /并记录时间 +set GLOBAL log_bin_trust_function_creators=true; + +drop FUNCTION insert_data; + +delimiter $$ + +CREATE FUNCTION insert_data() +returns VARCHAR(20) +DETERMINISTIC +BEGIN + +DECLARE i int DEFAULT 0; +DECLARE limit_num int DEFAULT 5000000; + +a: LOOP + + IF i > limit_num THEN + LEAVE a; + else + insert into emp (ename,age) VALUES (concat('用户',i),FLOOR(RAND()*100)); + set i = i + 1; -- 自增 + END IF; +END LOOP a; + +return limit_num; +END$$ + +delimiter ; + +SELECT insert_data(); + +SELECT * from emp; +-- 索引 + +CREATE UNIQUE index index_uni3 on emp(id); + +SELECT * from emp where id > 0; +``` \ No newline at end of file