2019-04-25-day040-数据库的查询
内容回顾
- 数据的增删改查
- 插入数据
- insert into 表 values (值1,值2...)
- insert into 表(指定字段名1,字段名2) values (值1,值2...)
- 删除数据
- delete from 表名 where 条件
- 更新数据
- update 表名 set 字段=新的值 where 条件
- 查询数据
- select * from 表
- where 条件
- group by 分组 having 过滤条件(可以使用聚合函数)
- order by 排序(默认是升序asc,降序desc)
- limit m,n (默认m=0,从m+1开始,取n个)
- limit n offset m(与上面的写法完全一致)
- select * from 表
多表查询
- 连表查询
- 表和表是怎么连在一起的
- 通过笛卡尔积得到一个全量拼接的大表
- 内连接(inner join) 双方能够互相匹配的项才会被显示出来
- select * from 表1 inner join 表2 on 条件
- select * from employee inner join department
- on employee.dep_id = department.id
- 给表重命名
- select t1.name,t2.name from employee as t1 inner join department as t2
- on t1.dep_id = t2.id
- 外连接
- 左外连接(left join) 只完整的显示左表中的所有内容,以及右表中与左表匹配的项
- select * from 表1 left join 表2 on 条件
- select * from employee left join department
- on employee.dep_id = department.id
- 右外连接(right join) 只完整的显示右表中的所有内容,以及左表中与右表匹配的项
- select * from 表1 right join 表2 on 条件
- select * from employee right join department
- on employee.dep_id = department.id
- 全外连接 永远显示左表和右表中所有的项
- select * from employee left join department
- on employee.dep_id = department.id
- union
- select * from employee right join department
- on employee.dep_id = department.id
- 左外连接(left join) 只完整的显示左表中的所有内容,以及右表中与左表匹配的项
- 表和表是怎么连在一起的
- 子查询
- 总是在一个select中 套着另一个select语句
- 嵌套着的这个select语句就是一个子查询语句
- 查"技术"部的所有员工的名字
- 查询技术部的id
- select id from department where name = '技术'
- select name from employee where dep_id = (select id from department where name = '技术');
- 如果一个需求 子查询和连表查询都能实现,选择连表or子查询?
- 选连表查询
- 查"技术"和"销售"部的所有员工的名字
- select id from department where name in ('技术','销售');
- select name from employee where dep_id in (select id from department where name in ('技术','销售'));
- 总是在一个select中 套着另一个select语句
练习
- 连表查询
- 1.找出年龄大于25岁的员工以及员工所在的部门
- select e.name,d.name from employee e inner join department d
- on e.dep_id = d.id where e.age>25
- 2.以内连接的方式查询employee和department表,并且以age字段的升序方式显示
- select * from employee e inner join department d
- on e.dep_id = d.id order by e.age
- 子查询
- 查询平均年龄在25岁以上的部门名
- select dep_id from employee group by dep_id having avg(age)>25
- select name from department where id in (select dep_id from employee group by dep_id having avg(age)>25);
- 查看技术部员工姓名
- select name from employee where dep_id = (select id from department where name = '技术');
- 查看不足1人的部门名(子查询得到的是有人的部门id)
- 在员工表中不存在的一个部门id,在department表里
- 在department表里的id字段中找到一个在员工表的dep_id中不存在的项
- select name from department where id not in (select dep_id from employee group by dep_id);
- 把员工表里所有的人所在的dep_id都查出来
- 带比较运算的子查询
- 1.查询大于所有人平均年龄的员工名与年龄
- select avg(age) from employee;
- select name,age from employee where age > (select avg(age) from employee);
- 2.查询大于部门内平均年龄的员工名、年龄
- select dep_id,avg(age) from employee group by dep_id;
- select * from employee inner join (select dep_id,avg(age) as avg_age from employee group by dep_id) as t2
- on employee.dep_id = t2.dep_id where employee.age > t2.avg_age;
练习
查询每个部门最新入职的那位员工
- 入职时间最大的员工
- 每个部门最新入职员工的时间
- select post,max(hire_date) from emp group by post;
- select emp.name,emp.hire_date from emp inner join (select post,max(hire_date) latest_date from emp group by post) as t
- on emp.post = t.post where emp.hire_date = t.latest_date;
select
(select t2.name from emp as t2 where t2.post=t1.post order by hire_date desc limit 1)
from emp as t1 group by post;
索引原理
读还是写操作多
- 博客园
- 百度
- 豆瓣
- 知乎
###读操作更多 - 读写 :10:1
###IO一次 写更浪费时间 - 数据库的瓶颈 : 读取数据的时间
- 磁盘的读效率
- 上百万条数据
- 如何存储
- 如何读取
alex 50 alex@oldboy.com
wusir 30 wusir@oldboy.com
jin 70 jin@oldboy.com
baoyuan 45 baoyuan@oldboy.com
ning 91 ning@oldboy.com
xiaohei 53 xiaohei@oldboy.com
lihua 21 lihua@oldboy.com
1
2
3
存储
- 树型结构里
- balance tree 平衡树
- 平衡树能够提高读的速度,但是降低了写的速度
- 存储行数据和树中数字的关系
- 树中的数字指向数据的具体地址
- 树中的数字节点内直接存储行内容
- 如果在非叶子节点 存储数值和表中的具体行,那么会导致每个节点能够存储的数据量降低
- 无形中增加了树的高度
- 查找数据的效率不稳定
- 一种新的存储数据的办法
alex 50 alex@oldboy.com
wusir 30 wusir@oldboy.com
jin 70 jin@oldboy.com
baoyuan 45 baoyuan@oldboy.com
ning 91 ning@oldboy.com
xiaohei 53 xiaohei@oldboy.com
lihua 21 lihua@oldboy.com
- 所有的数据都存储在叶子节点上而不是存储在根节点或者分支中
- select * from 表 where age between 20 and 70
- 在叶子节点之间添加了双向链表,让范围查询变得更加容易
树型结构里
- balance tree 平衡树
- 平衡树能够提高读的速度,但是降低了写的速度
- 存储行数据和树中数字的关系
- 叶子节点中的数字指向数据的具体地址
- 叶子节点内直接存储行内容
- 所有的数据都存储在叶子节点上而不是存储在根节点或者分支中
- 为了降低树的高度
- 让每一个数据被找到的时间变得稳定
- 在叶子节点之间添加了双向链表,为了能够更加快速的进行范围查询
b+树
- 影响查找速度的最根本的原因是什么
- IO次数 - 树的高度
- 为了降低树的高度
- 1.增加数据与数据之间的区分度
- 2.降低目录列的数据的长度
- 索引 : 实际上就是一个搜索表时使用的目录
- 聚集索引 : 叶子节点内直接存储行内容
- 只有innodb存储引擎才有聚集索引
- 主键
- 辅助索引 : 叶子节点中的数字指向数据的具体地址
- innodb中和myisam中都可以存在
# innodb 对应2个文件 表结构 数据 + 索引
# myisam 对应3个文件 表结构 纯数据 辅助索引
- innodb中和myisam中都可以存在
- 聚集索引 : 叶子节点内直接存储行内容
2019-04-25-day040-数据库的查询的更多相关文章
- Work 4(通知类) (2019.04.25)
- [2019.03.25]Linux中的查找
TMUX天下第一 全世界所有用CLI Linux的人都应该用TMUX,我爱它! ======================== 以下是正文 ======================== Linu ...
- ORACLE数据库常用查询二
ORACLE数据库常用查询 1.查看表空间对应数据文件情况: SQL MB,AUTOEXTENSIBLE FROM DBA_DATA_FILES; TABLESPACE_NAME FILE_NAME ...
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决(转载)
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- MS数据库优化查询最常见的几种方法
1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 5.网络速度慢 6.查询出的数据量过大 ...
- 优化SQLServer数据库加快查询速度
查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 ...
- SQL Server数据库 优化查询速度
查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 ...
- 转载:30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- 【MySQL笔记】数据库的查询
数据库的查询 注:文中 [ ...] 代表该部分可以去掉. 理论基础:对表对象的一组关系运算,即选择(selection).投影(projection)和连接(join) 1.select语句 子语句 ...
- MYSQL通过索引优化数据库的查询
#转载请联系 索引是什么? 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的位置信息. 更通俗的说,数据库索引好比是一本书前面的目录,能加快数据 ...
随机推荐
- SQL添加事务处理
--modi by lmt declare @errorSum int --记录错误数 begin Create table #CheckreqAccState(CheckReqID varchar( ...
- Mysql报错[Warning] TIMESTAMP with implicit DEFAULT value is deprecated和Buffered warning: Changed limits
报错2019-04-24 12:06:46 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use -- ...
- 成功解决internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'express'
^ Error: Cannot find module 'express'根据提示我们就可以知道,没有找到express这个模块,解决办法就是:npm install express
- for in 中的index
自己学艺不精... 数据是个对象,想要里面的key 原来 for (item, index) in items 中的index 就是key
- cmd命令行结果保存到txt里,屏幕显示一行就保存一行到txt
#coding:utf-8 """ 1.重定向print 2.python与cmd命令 """ import sys import os i ...
- pycharm配置QtDesigner
QtDesigner C:\Qt\Qt5.12.2\5.12.2\mingw73_64\bin\designer.exe $ProjectFileDir$ Pyuic C:\Anaconda3\pyt ...
- ceph使用对象网关
1. 介绍 三种验证客户端的方式选一种就行了 2. 安装 2.1 创建对象存储网关系统 步骤1:安装文档安装ceph 步骤2:初始化缓存网关 [root@node1 my-cluster]# ...
- (整理4)RPC服务和HTTP服务简单说明
很长时间以来都没有怎么好好搞清楚RPC(即Remote Procedure Call,远程过程调用)和HTTP调用的区别,不都是写一个服务然后在客户端调用么?这里请允许我迷之一笑~Naive!本文简单 ...
- GDAL——命令使用专题——gdallocationinfo命令
GDAL——命令使用专题——gdallocationinfo命令 前言 GDAL(Geospatial Data Abstraction Library)是一个在X/MIT许可协议下的开源栅格空间数 ...
- MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents
MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents 一.前言 当在m ...