Oracle查询优化-多表查询
--合并结果集
--1.union all
SELECT * FROM emp WHERE empno=7369
UNION ALL--单纯合并
SELECT * FROM emp WHERE empno=7369;
--2.union
SELECT * FROM emp WHERE empno=7369
UNION --将重复结果集合并
SELECT * FROM emp WHERE empno=7369;
--------------使用命令窗口执行,查看union与or的执行计划
SQL> alter session set "_b_tree_bitmap_plans" = FALSE;
SQL> explain plan for select empno,ename from emp where empno =7788 or ename='SCOTT';
SQL> select * from table(dbms_xplan.display);
SQL> explain plan for select empno,ename from emp where empno=7788 UNION select empno,ename from emp where ename = 'SCOTT';
SQL> select * from table(dbms_xplan.display);
--可见union会使用索引
--union会强制将结果集进行合并,从而使数据出现错误
SQL> select empno,deptno from emp where mgr=7698 order by 1; EMPNO DEPTNO
----- ------
7499 30
7521 30
7654 30
7844 30
7900 30 SQL> select empno,deptno from emp where job='SALESMAN' order by 1; EMPNO DEPTNO
----- ------
7499 30
7521 30
7654 30
7844 30
--从上面两条语句看出有4条结果重复,看看union和or的区别
SQL> select deptno from emp where mgr=7698 or job='SALESMAN'; DEPTNO
------
30
30
30
30
30 SQL> select deptno from emp where mgr=7698
2 union
3 select deptno from emp where job='SALESMAN'; DEPTNO
------
30
/**************************************************************************************************************************************
* 结论:
* 1.不仅两个数据集间重复的数据会被去重,而且单个数据集里重复的数据也会被去重;
* 2.有重复数据的数据集用union后得到的数据与预期会不一致;
* 3.可以这样,select deptno from (select empno,deptno from emp where mgr=7698 union select empno,deptno from emp where job='SALESMAN')
* 或者将empno改为rowid也行。
**************************************************************************************************************************************/
--4.[inner] join与=
SELECT e.empno,e.ename,d.dname,d.loc
FROM emp e
INNER JOIN dept d ON (e.deptno=d.deptno)
WHERE e.deptno=10;
SELECT e.empno,e.ename,d.dname,d.loc
FROM emp e,dept d
WHERE e.deptno=d.deptno
AND e.deptno=10;--以上两句结果相同,但join是SQL-92标准,可以清晰反应表与表之间的关联关系,推荐使用join
--5.in、exists和inner join
CREATE TABLE emp2 AS--创建相关table
SELECT ename,job,sal,comm FROM emp WHERE job = 'CLERK';
--6.要求查询与emp2相匹配的emp中的数据,可以使用in、exists或inner join
SELECT * FROM emp WHERE (ename,job,sal) IN (SELECT ename,job,sal FROM emp2);
SELECT * FROM emp a WHERE EXISTS (SELECT NULL FROM emp2 b WHERE b.ename=a.ename
AND b.job=a.job
AND b.sal=a.sal);
SELECT * FROM emp a
INNER JOIN emp2 b
ON (b.ename=a.ename
AND b.job=a.job
AND b.sal=a.sal);
/********************************************************************************************************
* 结论:
* 1.以上三条语句可以返回相同条数的结果集;
* 2.通过查看plan可以看出,exists和in都使用了HASH JOIN SEMI(哈希半连接)而inner join使用了哈希连接;
* 3.exists和in的执行效率是一样的,如果不确定,可以通过查看plan来判断,不要死记硬背。
********************************************************************************************************/
--7.内、左、右、外连接
--建立测试表
--左表
DROP TABLE L;
CREATE TABLE L AS SELECT 'L_1' AS str,'' AS v FROM dual UNION ALL
SELECT 'L_2','' AS v FROM dual UNION ALL
SELECT 'L_3','' AS v FROM dual UNION ALL
SELECT 'L_4','' AS v FROM dual;
--右表
DROP TABLE R;
CREATE TABLE R AS
SELECT 'R_3' AS str,'' AS v,1 AS STATUS FROM dual UNION ALL
SELECT 'R_4','' AS v,0 AS STATUS FROM dual UNION ALL
SELECT 'R_5','' AS v,0 AS STATUS FROM dual UNION ALL
SELECT 'R_6','' AS v,0 AS STATUS FROM dual;
--INNER JOIN
SELECT l.str AS l_str,r.str AS r_str
FROM l
INNER JOIN r ON l.v=r.v
ORDER BY 1,2;
SELECT l.str AS l_str,r.str AS r_str
FROM l,r
WHERE l.v=r.v
ORDER BY 1,2;
--LEFT JOIN
SELECT l.str AS l_str,r.str AS r_str
FROM l
LEFT JOIN r ON l.v=r.v
ORDER BY 1,2;
SELECT l.str AS l_str,r.str AS r_str
FROM l,r
WHERE l.v=r.v(+)
ORDER BY 1,2;
--RIGHT JOIN
SELECT l.str AS l_str,r.str AS r_str
FROM l
RIGHT JOIN r ON l.v=r.v
ORDER BY 1,2;
SELECT l.str AS l_str,r.str AS r_str
FROM l,r
WHERE l.v(+)=r.v
ORDER BY 1,2;
--FULL JOIN
SELECT l.str AS l_str,r.str AS r_str
FROM l
FULL JOIN r ON l.v=r.v
ORDER BY 1,2;
--8.自连接
SELECT a.empno,a.ename,b.ename mgr,a.deptno FROM emp a,emp b WHERE a.mgr=b.empno(+) ORDER BY a.empno;
--9.NOT IN,NOT EXISTS和LEFT JOIN
SELECT * FROM DEPT WHERE DEPTNO NOT IN(SELECT DEPTNO FROM EMP WHERE EMP.DEPTNO IS NOT NULL);
SELECT * FROM DEPT WHERE NOT EXISTS (SELECT NULL FROM EMP WHERE EMP.DEPTNO = DEPT.DEPTNO);
SELECT DEPT.* FROM DEPT LEFT JOIN EMP ON EMP.DEPTNO=DEPT.DEPTNO WHERE EMP.DEPTNO IS NULL;
--10.外连接中的条件
--沿用7的测试表,查询左表所有内容,使用V关联右表,但只显示右表中STATUS为1的值,期望结果如下:
STR STR
--- ---
L_1
L_2
L_3 R_3
L_4
--错误写法:
SELECT L.STR,R.STR FROM L LEFT JOIN R ON(L.V=R.V) WHERE R.STATUS=1 ORDER BY 1;
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 688663707
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 27 | 8 (25)| 00:00:01 |
| 1 | SORT ORDER BY | | 1 | 27 | 8 (25)| 00:00:01 |
|* 2 | HASH JOIN | | 1 | 27 | 7 (15)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| R | 1 | 21 | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| L | 4 | 24 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("L"."V"="R"."V")
3 - filter("R"."STATUS"=1)
Note
----- PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
- dynamic sampling used for this statement 21 rows selected
--正确写法:
SELECT L.STR,R.STR FROM L LEFT JOIN R ON (L.V=R.V AND R.STATUS=1) ORDER BY 1;
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 2310059642
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 108 | 8 (25)| 00:00:01 |
| 1 | SORT ORDER BY | | 4 | 108 | 8 (25)| 00:00:01 |
|* 2 | HASH JOIN OUTER | | 4 | 108 | 7 (15)| 00:00:01 |
| 3 | TABLE ACCESS FULL| L | 4 | 24 | 3 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL| R | 1 | 21 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("L"."V"="R"."V"(+))
4 - filter("R"."STATUS"(+)=1)
Note
----- PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
- dynamic sampling used for this statement 21 rows selected
/*外层条件不要随便加,会影响结果集*/
--11.检测两个表中的数据及对应数据的条数是否相同
CREATE OR REPLACE VIEW V AS
SELECT * FROM EMP WHERE DEPTNO != 10
UNION ALL
SELECT * FROM EMP WHERE ENAME='SCOTT';
--要求查处视图V与表EMP中不同的数据(注意:视图中'SCOTT'有两行数据,而EMP表中只有一条数据)
--分析:除了EMP表中DEPTNO为10的数据,还有ENAME为SCOTT的数据与V中不一致,因为EMP表中只有一条而V中有两条
SELECT *
FROM (SELECT EMPNO,ENAME,COUNT(*) AS CNT FROM V GROUP BY EMPNO,ENAME) V --加一列统计条数用以区分不同
FULL JOIN (SELECT EMPNO,ENAME,COUNT(*) AS CNT FROM EMP GROUP BY EMPNO,ENAME) E
ON V.EMPNO=E.EMPNO AND V.CNT=E.CNT
WHERE V.EMPNO IS NULL OR E.EMPNO IS NULL;
EMPNO ENAME CNT EMPNO ENAME CNT
---------- ---------- ---------- ----- ---------- ----------
7788 SCOTT 2
7782 CLARK 1
7839 KING 1
7788 SCOTT 1
7934 MILLER 1
--12.聚集与内连接
CREATE TABLE EMP_BONUS (EMPNO INT,RECEIVED DATE,TYPE INT);
INSERT INTO EMP_BONUS VALUES (7934,DATE '2005-5-17',1);
INSERT INTO EMP_BONUS VALUES (7934,DATE '2005-2-15',2);
INSERT INTO EMP_BONUS VALUES (7839,DATE '2005-5-17',3);
INSERT INTO EMP_BONUS VALUES (7782,DATE '2005-5-17',1);
--要求返回上述员工的工资及奖金,奖金根据TYPE来定,TYPE为1,奖金为10%...
SELECT E.DEPTNO,E.EMPNO,E.ENAME,E.SAL,B.TYPE*0.1*E.SAL AS BONUS
FROM EMP E,EMP_BONUS B
WHERE E.EMPNO(+)=B.EMPNO;
DEPTNO EMPNO ENAME SAL BONUS
------ ----- ---------- --------- ----------
10 7934 MILLER 1300.00 130
10 7934 MILLER 1300.00 260
10 7839 KING 5000.00 1500
10 7782 CLARK 2450.00 245
--若此时想要计算6月份部门为10的总工资
--错误做法:
SELECT E.DEPTNO,SUM(E.SAL) TOTAL_SAL,SUM(B.TYPE*0.1*E.SAL) AS TOTAL_BONUS
FROM EMP E,EMP_BONUS B
WHERE E.EMPNO(+)=B.EMPNO
GROUP BY E.DEPTNO;
DEPTNO TOTAL_SAL TOTAL_BONUS
------ ---------- -----------
10 10050 2135
--原因,MILLER的工资和奖金都重复计算了,因为他5月份已经降职了,所以有两条不同的TYPE对应他的相关信息!
--正确做法:
SELECT E.DEPTNO,SUM(E.SAL) TOTAL_SAL,SUM(B.TYPE*0.1*E.SAL) AS TOTAL_BONUS
FROM EMP E
INNER JOIN
(SELECT EB.EMPNO,EB.TYPE,EB.RECEIVED
FROM EMP_BONUS EB
INNER JOIN
(SELECT MAX(C.EMPNO) EMPNO,MAX(C.RECEIVED) RECEIVED FROM EMP_BONUS C GROUP BY C.EMPNO) EBG --先统计出员工最新的职位变更时间
ON EB.EMPNO=EBG.EMPNO AND EB.RECEIVED=EBG.RECEIVED) B
ON E.EMPNO=B.EMPNO
GROUP BY E.DEPTNO;
DEPTNO TOTAL_SAL TOTAL_BONUS
------ ---------- -----------
10 8750 1875
--13.聚集与外连接
SELECT E.DEPTNO,SUM(E.SAL) TOTAL_SAL,SUM(B.TYPE*0.1*E.SAL) AS TOTAL_BONUS
FROM EMP E
LEFT JOIN --只改此处为LEFT JOIN即可
(SELECT EB.EMPNO,EB.TYPE,EB.RECEIVED
FROM EMP_BONUS EB
INNER JOIN
(SELECT MAX(C.EMPNO) EMPNO,MAX(C.RECEIVED) RECEIVED FROM EMP_BONUS C GROUP BY C.EMPNO) EBG --先统计出员工最新的职位变更时间
ON EB.EMPNO=EBG.EMPNO AND EB.RECEIVED=EBG.RECEIVED) B
ON E.EMPNO=B.EMPNO
GROUP BY E.DEPTNO
ORDER BY 1;
DEPTNO TOTAL_SAL TOTAL_BONUS
------ ---------- -----------
10 8750 1875
20 10875
30 9400
--14.多表查询时的空值处理
--要求返回比ALLEN提成低的员工
--错误做法:
SELECT E.ENAME,E.COMM
FROM EMP E
WHERE E.COMM<(SELECT COMM FROM EMP WHERE ENAME='ALLEN');
ENAME COMM
---------- ---------
TURNER 0.00
--错误原因:有些员工的COMM一项为NULL,但并未返回
--正确做法:
SELECT E.ENAME,E.COMM
FROM EMP E
WHERE COALESCE(E.COMM,0)<(SELECT COMM FROM EMP WHERE ENAME='ALLEN');
ENAME COMM
---------- ---------
SMITH
JONES
BLAKE
CLARK
SCOTT
KING
TURNER 0.00
ADAMS
JAMES
FORD
MILLER
Oracle查询优化-多表查询的更多相关文章
- Oracle查询优化--单表查询
--查询所有 select * from emp; select * from emp where comm is null; --错误表达 --select * from emp where com ...
- Oracle笔记 多表查询
Oracle笔记 多表查询 本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查 ...
- Oracle的多表查询
多表查询概念: 所谓多表查询,又称表联合查询,即一条语句涉及到的表有多张,数据通过特定的连接进行联合显示. 基本语法: select column_name,.... from table1,tabl ...
- oracle SQL多表查询
SQL多表查询 1.集合理论 1.1 什么是集合 具有某种特定性质的事物的总体. 集合的特性:无序性.互异性.确定性. 一个集合可以小到从一个表中取出一行中的一列. 1 ro ...
- mysql 查询优化 ~ 多表查询基础知识
一 什么是驱动表 1)指定了联接条件时,满足查询条件的记录行数少的表为[驱动表]: 2)未指定联接条件时,行数少的表为[驱动表](Important!). 表现 explain第一行出现的 ...
- mysql 查询优化 ~ 多表查询改写思路
一 简介:在之前我们从基础可知,现在咱们聊一下改写的几种思路二 分类: 1 left join 2 inner join 3 right join三 具体改写思路:思路1 本身不包含子查询,将多 ...
- Oracle之多表查询
-多表查询 1.交叉连接 select * from t_class for update; select * from t_student for update; select for update ...
- oracle数据库单表查询
今天给大家分享的是关于数据库的单表查询,像单表查询/多表查询/分组查询/子查询,这些方法的使用在实际项目过程中会经常用到,作为一名合格的测试人员如果不会数据库那肯定是不行的,行走江湖可能随时会面临被侮 ...
- oracle习题-emp表查询练习
emp表查询练习 1 查询emp表的全部记录 Select * from emp; 2 查询出每个雇员的编号.姓名.基本工资 Select empno,ename,sal from emp; 3 查询 ...
随机推荐
- 查杀病毒的NB命令
kill - `pgrep linux|awk '{++S[$1]}END{for (i in S) print i}'` teamkill #!/bin/bash set -x kill - `pg ...
- 08、通过自定义依赖属性,用 StateTrigger 修改全局主题样式
在 Win 10 的 UWP 中,需要在 xaml 中,通过使用 StateTrigger 修改全局的文本大小.Background.画刷等依赖属性等主题样式.下面只针对字体大小进行描述,其它依赖属性 ...
- 在cocos2d-x-3.0 android 平台编译时提示CocosGUI.h: No such file or directory
分类是个让人蛋疼的事情,所幸自己的博客自己做主.这是个高兴的开始. 每天抽空玩2048,终于忍受不住,于是决定自己从网上download下源码,自己编译一个出来.所有的事情都很容易,除了操蛋的中文注释 ...
- eclipse中类和方法添加作者日期说明
1.类添加作者日期说明 依次点击window—>preferences—>Java—>Code Stype—>Code Templates-Comments-Types 2.方 ...
- Python中的相对文件路径的调用
先让我们来看看一个用到相对文件路径的函数调用的问题.假设现在有两个脚本文件main.py和func.py,他们的路径关系是: . |--dir1 |--main.py |--dir2 |--func. ...
- 关于SVN提交时报out-of-date错误的解决方法
提交项目文件时,报如下的信息:Item is out-of-datesvn: Commit failed (details follow):svn: Item '/xxx/xxx/xxx/xxx/xx ...
- Android开发日记(二)
HashMap<String, Object> map;定义一个HashMap用来传递字符 TextView textView_JobTitle=(TextView)findViewByI ...
- dp之最长递增、公共子序列总结
1.最长递增子序列模板poj2533(时间复杂度O(n*n)) #include<iostream> #include<stdio.h> #include<string. ...
- linux搭建git服务器
服务端配置 1.安装git 2.新建一个用户,只能用来上传代码,而不能通过ssh登录,比如git用户 adduser git chsh -s $(command -v git-shell) git 使 ...
- C语言 · 提货单(P1101)
算法训练 P1101 时间限制:1.0s 内存限制:256.0MB 有一份提货单,其数据项目有:商品名(MC).单价(DJ).数量(SL).定义一个结构体prut,其成员是上面的三项 ...