索引------数据库

目录

1.OLAP三大查询

2.索引的基本原理以及作用

3.索引的优缺点

4.索引的适用场景

5.索引的int、miss(explain命令)


数据库建立的演示

设置为默认库然后进行操作

先创建表并且插入数据 

两个表就建立完成了 

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会有显示

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值