关注程序员小乐带你玩爆编程技术
哈喽,各位新来的小伙伴们,大家好!由于公众号做了改版,为了保证公众号的资源能准时推送到你手里,大家记得将程序员小乐的公众号 加星标置顶 ,在此真诚的表示感谢~
来源:网络
上一篇干货:一个月薪12000的北京程序员的真实生活
正文
SQL 作为关系型数据库的标准语言,是 IT 从业人员必不可少的技能之一。SQL 本身并不难学,编写查询语句也很容易,但是想要编写出能够高效运行的查询语句却有一定的难度。
EXPLAIN ANALYZE SELECT emp_id, emp_name FROM employee e WHERE salary > ( SELECT AVG(salary) FROM employee WHERE dept_id = e.dept_id);-> Filter: (e.salary > (select #2)) (cost=2.75 rows=25) (actual time=0.232..4.401 rows=6 loops=1) -> Table scan on e (cost=2.75 rows=25) (actual time=0.099..0.190 rows=25 loops=1) -> Select #2 (subquery in condition; dependent) -> Aggregate: avg(employee.salary) (actual time=0.147..0.149 rows=1 loops=25) -> Index lookup on employee using idx_emp_dept (dept_id=e.dept_id) (cost=1.12 rows=5) (actual time=0.068..0.104 rows=7 loops=25)
EXPLAIN ANALYZE SELECT e.emp_id, e.emp_name FROM employee e JOIN (SELECT dept_id, AVG(salary) AS dept_average FROM employee GROUP BY dept_id) t ON e.dept_id = t.dept_id WHERE e.salary > t.dept_average;-> Nested loop inner join (actual time=0.722..2.354 rows=6 loops=1) -> Table scan on e (cost=2.75 rows=25) (actual time=0.096..0.205 rows=25 loops=1) -> Filter: (e.salary > t.dept_average) (actual time=0.068..0.076 rows=0 loops=25) -> Index lookup on t using <auto_key0> (dept_id=e.dept_id) (actual time=0.011..0.015 rows=1 loops=25) -> Materialize (actual time=0.048..0.057 rows=1 loops=25) -> Group aggregate: avg(employee.salary) (actual time=0.228..0.510 rows=5 loops=1) -> Index scan on employee using idx_emp_dept (cost=2.75 rows=25) (actual time=0.181..0.348 rows=25 loops=1)
-- MySQLSELECT * FROM large_table ORDER BY id LIMIT 10 OFFSET N;
-- MySQLSELECT * FROM large_table WHERE id > last_id ORDER BY id LIMIT 10;
(6)SELECT [DISTINCT | ALL] col1, col2, agg_func(col3) AS alias(1) FROM t1 JOIN t2(2) ON (join_conditions)(3) WHERE where_conditions(4) GROUP BY col1, col2(5)HAVING having_condition(7) UNION [ALL] ...(8) ORDER BY col1 ASC,col2 DESC(9)OFFSET m ROWS FETCH NEXT num_rows ROWS ONLY;
-- 错误示例SELECT emp_name AS empname FROM employee WHERE empname ='张飞';
-- GROUP BY 错误示例SELECT dept_id, emp_name, AVG(salary) FROM employee GROUP BY dept_id;
📝如果使用了 GROUP BY 分组,之后的 SELECT、ORDER BY 等只能引用分组字段或者聚合函数;否则,可以引用 FROM 和 JOIN 表中的任何字段。
SELECT e.emp_name, d.dept_name FROM employee e LEFT JOIN department d ON (e.dept_id = d.dept_id) WHERE e.emp_name ='张飞';emp_name|dept_name|--------|---------|张飞 |行政管理部|SELECT e.emp_name, d.dept_name FROM employee e LEFT JOIN department d ON (e.dept_id = d.dept_id AND e.emp_name ='张飞');emp_name|dept_name|--------|---------|刘备 | [NULL]|关羽 | [NULL]|张飞 |行政管理部|诸葛亮 | [NULL]|...
--END--
硬核!一套基于SpringBoot + Vue 的开源物联网智能家居系统(附源码)!
Java 8 一行代码解决了空指针问题,太厉害了...
为什么国内 996 干不过国外的 955 呢?
阿里面试:“说一下从 url 输入到返回请求的过程”
还发愁画流程图?IDEA这款神仙插件全部帮你搞定!
“不使用的对象应手动赋值为null”
都在建议你不要直接使用 @Async 注解,为什么?
文章有问题?点此查看未经处理的缓存