大小表区分按照数据量的大小区分;

通常对于小表,Oracle建议通过全表扫描进行数据访问,对于大表则应该通过索引以加快数据查询,当然如果查询要求返回表中大部分或者全部数据,那么全表扫描可能仍然是最好的选择。
从V$SYSSTAT视图中,我们可以查询得到关于全表扫描的系统统计信息:

SQL> col name for a30 
SQL> select name,value from v$sysstat
2 where name in ('table scans (short tables)','table scans (long tables)');

NAME VALUE
------------------------------ ----------
table scans (short tables) 828
table scans (long tables) 101

其中table scans (short tables)指对于小表的全表扫描的此时;table scans (long tables)指对于大表的全表扫描的次数。
从Statspack的报告中,我们也可以找到这部分信息:

Instance Activity Stats for DB: CELLSTAR Instance: ora8i Snaps: 20 -

Statistic Total per Second per Trans 
--------------------------------- ---------------- ------------ ------------ 
。。。。。。
table scan blocks gotten 38,228,349 37.0 26.9 
table scan rows gotten 546,452,583 528.9 383.8 
table scans (direct read) 5,784 0.0 0.0 
table scans (long tables) 5,990 0.0 0.0 
table scans (rowid ranges) 5,850 0.0 0.0 
table scans (short tables) 1,185,275 1.2 0.8

通常,如果一个数据库的table scans (long tables)过多,那么db file scattered read等待事件可能同样非常显著,和以上数据来自同一个report的Top5等待事件就是如此:

Top 5 Wait Events 
~~~~~~~~~~~~~~~~~ Wait % Total
Event Waits Time (cs) Wt Time
-------------------------------------------- ------------ ------------ -------
log file parallel write 1,436,993 1,102,188 10.80
log buffer space 16,698 873,203 8.56
log file sync 1,413,374 654,587 6.42
control file parallel write 329,777 510,078 5.00
db file scattered read 425,578 132,537 1.30

数据库内部,很多信息和现象都是紧密相关的,只要我们加深对于数据库的了解,在优化和诊断数据库问题时就能够得心应手。

Oracle通过一个内部参数_small_table_threshold来定义大表和小表的界限。缺省的该参数等于2%的Buffer数量,如果表的大小小于该参数定义,Oracle认为该表为小表,否则Oracle认为该表为大表。
我们看一下Oracle9iR2中的情况:

SQL> @@GetParDescrb.sql
Enter value for par: small
old 6: AND x.ksppinm LIKE '%&par%'
new 6: AND x.ksppinm LIKE '%small%'

NAME VALUE DESCRIB
------------------------------ -------------------- ------------------------------------------------------------
_small_table_threshold 200 threshold level of table size for direct reads

以上数据库中,200正好约为Buffer数量的2%:

SQL> show parameter db_cache_size

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_cache_size big integer 83886080
SQL> select (83886080/8192)*2/100 from dual;

(83886080/8192)*2/100
---------------------
204.8

所以要区分大小表(Long/Short)是因为全表扫描可能引起Buffer Cache的抖动,缺省的大表的全表扫描会被置于LRU的末端,以期尽快老化,减少Buffer的占用。从Oracle8i开始,Oracle的多缓冲池管理技术(Default/Keep/Recycle池)给了我们另外一个选择,对于不同大小、不同使用频率的数据表,从建表之初就可以指定其存储Buffer,以使得内存使用更加有效。

有时一个查询结果需要从两个或两个以上表中提取字段数据,此时需要使用多表关联查询。

1)笛卡尔积关联

create table a(
   id number(7),
   name varchar2(20));

create table b(
   id number(7),
   name varchar2(20));

insert into a(id,name) values(1,'a1');
  insert into a(id,name) values(2,'a2');
  insert into a(id,name) values(3,'a3');
  insert into b(id,name) values(1,'b1');
  insert into b(id,name) values(2,'b2');

select * from a,b;

笛卡尔积特点:
   --*代表from后面表中所有列
   --返回结果数量是各个表记录的乘积
   --结果是a每条记录与b每条记录结合形成

*2)等值连接
  参与等值条件的两个字段值,相等时才作为结果返回。
 select a.id,a.name,b.name //3.提取显示的字段
 from a,b //1.形成笛卡尔积结果
 where a.id=b.id; //2.返回id相等的记录

---使用[INNER] JOIN...ON...语法-----
 select a.id,a.name,b.name
 from a join b on(a.id=b.id);

提示:建议采用JOIN...ON语法,INNER JOIN和JOIN作用等价。内连接,等值连接是一个意思。
   
  ----使用JOIN...USING语法(了解)------
 select id,a.name,b.name
 from a join b using(id);

JOIN...USING使用注意事项:
 --关联的两个表中需要有相同的字段.(名字和类型相同)
 --关联的字段在使用时不能加别名

//查询员工名称,工资,所在部门编号,部门名称
  select e.ename,e.sal,e.deptno,d.dname
  from emp e join dept d 
     on(e.deptno=d.deptno);

3)外连接
  等值连接,需要两个表的关联字段等值才将结果返回。如果需要将某一个表记录全部返回,即使
  另一个表找不到对等字段记录,此时可以使用外连接。
  *a.左外连接

----使用LEFT OUTER JOIN...ON...语法------
  select e.ename,e.sal,e.deptno,d.dname
  from emp e left outer join dept d 
     on(e.deptno=d.deptno);
    
 A left outer join B on(...)
 以A表记录显示为主,B表记录为补充.当A表记录在B表找不到对等记录时,B以NULL方式补充。

b.右外连接
 select e.ename,e.sal,e.deptno,d.dname
  from emp e right outer join dept d 
     on(e.deptno=d.deptno);
 上面语句是以dept表显示为主,emp为补充.如果emp没有对等记录,字段值以NULL补充。

select * from a 
 right outer join b on(a.id=b.id);
等价于
select * from b 
 left outer join a on(a.id=b.id)

----在JOIN...ON之前的外连接写法--------
//(+)所在表为补充表,另一方是主表
select * from a,b
where a.id(+)=b.id; //jb为主,ja为补充

c.全外连接
   全外连接=左外连接+右外链接-(重复记录)
  select * from a 
    full outer join b on(a.id=b.id);

//查询部门编号,部门名称,部门员工人数
  select d.deptno,
         d.dname,
         count(e.ename) num
  from DEPT d left outer join EMP e 
    on(d.deptno=e.deptno)
  group by d.deptno,d.dname
  order by d.deptno;

EMPNO ENAME DEPTNO DNAME
...    ...    10   ...
NULL   NULL   40   ...
NULL   NULL   50   ...
//按部门分组统计,count(*)和count(ename)的区别
count(*) = 1 
count(ename) = 0

//查询部门在NEW YORK和CHICAGO的员工编号和员工名称
select e.empno,e.ename
from DEPT d join EMP e on(d.deptno=e.deptno)
where d.loc in ('NEW YORK','CHICAGO');

4)自连接
  关联双方的表是同一个表。

//查询员工编号,员工名,上级编号,上级名称
  select e.empno,e.ename,e.mgr,e1.ename
  from EMP e left outer join 
       EMP e1 on(e.mgr=e1.empno);
  //查询员工编号,员工名,所在部门名,上级编号,上级名称
  select e.empno,e.ename,d.dname,e.mgr,e1.ename
  from EMP e 
     left outer join EMP e1 on(e.mgr=e1.empno)
     left outer join DEPT d on(d.deptno=e.deptno);

 

最新文章

  1. Atitit 知识管理的重要方法 数据来源,聚合,分类,备份,发布 搜索
  2. C#的循环语句
  3. HDU 1788 Chinese remainder theorem again
  4. HashSet 读后感
  5. resin的简单介绍和使用
  6. iOS开发 点击跳转到App Store 或者 点击按钮去评价
  7. 我的Python成长之路---第二天---Python基础(8)---2016年1月9日(晴)
  8. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  9. 转:NSString什么时候用copy,什么时候用strong
  10. 使用计算监控(Using computed observables)
  11. YII2 请求(request)
  12. linux 下 tomcat 之 配置静态资源路径
  13. QT第三天学习
  14. C++编译/运行过程中产生的各种文件
  15. zabbix之微信告警(python版):微信个人报警,微信企业号告警脚本
  16. JSP+Servlet+JavaBean实现数据库的增删改查
  17. Struts标签判断当前用户是否存在
  18. GENA
  19. Python之模块(二)
  20. 【七】ab压测

热门文章

  1. Eclipse上安装websphere
  2. JavaIO模型--装饰者模式
  3. MVC-Application
  4. Python中type()详解:动态创建类
  5. 运维开发笔记整理-JsonResponse对象
  6. KVM虚拟机的管理
  7. 0014SpringBoot结合thymeleaf实现登录功能
  8. 一个s的力量——http与https
  9. WebService在ssm框架中的简单应用
  10. Jmeter+Jenkins持续集成(三、集成到Jenkins)