目录
2.索引的基本原理以及作用
数据库建立的演示
设置为默认库然后进行操作
先创建表并且插入数据
两个表就建立完成了
1.1聚合查询
聚合查询具有:
a.聚合函数count(*)、max()、min()、sun()、avg()
b.聚合字段 group by 分组
会统计表的行数
所以有三个为null的qq_mail没有统计
![]()
这种不算是聚合(因为是横向的)
多字段聚合
写name是不合理的
1.2联表查询 (一般的联表都带有链表条件,这样才有意义)
默认情况下的联表,是俩张表的笛卡尔积
这两个语句是完全等价的
这样写才有意义(三个语句也是完全等价的)
-- 在 from 后边直接跟 2 张表(2 张以上也可以) -- 视为同时从 2 张表中查询数据 select * from users, articles; -- 一共 20 条数据 = 4 * 5 select * from users join articles; -- 添加 联表 条件后,得到的结果才是有意义的 select * from users, articles where users.uid = articles.author_id and users.name = '小红'; select * from users, articles where uid = author_id and users.name = '小红'; select * from users join articles on uid = author_id where users.name = '小红';
1.2.1内联 inner join(只保留交集部分)
-- 添加 联表 条件后,得到的结果才是有意义的 select * from users, articles where users.uid = articles.author_id and users.name = '小红'; select * from users, articles where uid = author_id and users.name = '小红'; select * from users join articles on uid = author_id where users.name = '小红'; -- 以上这些全部是内联 select * from users inner join articles on uid = author_id; -- inner 可以省略
1.2.2左外联(保留左表中的数据) outer join
-- 左外联 select * from users left outer join articles on uid = author_id; select * from users left join articles on uid = author_id;
1.2.3右外联(保留右表中的数据)
-- 右外联 select * from users right outer join articles on uid = author_id; select * from users right join articles on uid = author_id;
1.2.4全外联
表全保留,不知道的填null
1.2.5给表起别名(做联表时,一般会给表起别名,不做联表,一般不会)
1.3子查询
![]()
这里还用到了exists的使用
先通过外部的sql得到结果,然后将每一行的结果的结果带入到内部sql中进行 查询,如果可以说明满足exists条件
![]()
2.索引的作用
索引的分类:
2.1索引的基本原理(涉及索引的分类)
数据量大的情况下,用索引可以更加高效的查询。
3.索引的优缺点
4.索引的适用场景
创建索引
5.索引的hit、miss(explain命令)
explain可以判断我们是否命中聚合
命中之后,workbanch会有显示