数据库教程:SQL Server多表查询优化方案集锦

sql server多表查询的优化方案是本文我们主要要介绍的内容,本文我们给出了优化方案和具体的优化实例,接下来就让我们一起来了解一下这部分内容。 1.执行路径 ora

sql server多表查询的优化方案是数据库技术:SQL Server多表查询优化方案集锦我们主要要介绍的内容,数据库技术:SQL Server多表查询优化方案集锦我们给出了优化方案和具体的优化实例,接下来就让我们一起来了解一下这部分内容。

1.执行路径

oracle的这个功能大大地提高了sql的执行性能并节省了内存的使用:我们发现,单表数据的统计比多表统计的速度完全是两个概念.单表统计可能只要0.02秒,但是2张表联合统计就

可能要几十秒了.这是因为oracle只对简单的表提供高速缓冲(cache buffering) ,这个功能并不适用于多表连接查询..数据库管理员必须在init.ora中为这个区域设置合适的参数,当这个内存区域越大,就可以保留更多的语句,当然被共享的可能性也就越大了.

2.选择最有效率的表名顺序(记录少的放在后面)

oracle的解析器按照从右到左的顺序处理from子句中的表名,因此from子句中写在最后的表(基础表 driving table)将被最先处理. 在from子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表.当oracle处理多个表时, 会运用排序及合并的方式连接它们.首先,扫描第一个表(from子句中最后的那个表)并对记录进行派序,然后扫描第二个表(from子句中最后第二个表),最后将所有从第二个表中检索出的记录与第一个表中合适记录进行合并.

例如:

表 tab1 16,384 条记录

表 tab2 1条记录

选择tab2作为基础表 (最好的方法)

select count(*) from tab1,tab2 执行时间0.96秒

选择tab2作为基础表 (不佳的方法)

select count(*) from tab2,tab1    执行时间26.09秒

如果有3个以上的表连接查询, 那就需要选择交叉表(intersection table)作为基础表, 交叉表是指那个被其他表所引用的表.

例如:    emp表描述了location表和category表的交集.

  select *   from location l ,       category c,       emp e   where e.emp_no between 1000 and 2000   and e.cat_no = c.cat_no   and e.locn = l.locn 

将比下列sql更有效率

  select *   from emp e ,   location l ,       category c   where  e.cat_no = c.cat_no   and e.locn = l.locn   and e.emp_no between 1000 and 2000   

3.where子句中的连接顺序(条件细的放在后面)

oracle采用自下而上的顺序解析where子句,根据这个原理,表之间的连接必须写在其他where条件之前, 那些可以过滤掉最大数量记录的条件必须写在where子句的末尾.

例如:

(低效,执行时间156.3秒)

  select …   from emp e   where  sal > 50000   and   job = ‘manager'   and   25 < (select count(*) from emp   where mgr=e.empno);   (高效,执行时间10.6秒)   select …   from emp e   where 25 < (select count(*) from emp          where mgr=e.empno)   and   sal > 50000   and   job = ‘manager';   

4.select子句中避免使用’* ‘

当你想在select子句中列出所有的column时,使用动态sql列引用 ‘*’ 是一个方便的方法.不幸的是,这是一个非常低效的方法. 实际上,oracle在解析的过程中, 会将’*’ 依次转换成所有的列名, 这个工作是通过查询数据字典完成的, 这意味着将耗费更多的时间.

5.减少访问数据库的次数

当执行每条sql语句时, oracle在内部执行了许多工作: 解析sql语句, 估算索引的利用率, 绑定变量 , 读数据块等等. 由此可见, 减少访问数据库的次数 , 就能实际上减少oracle的工作量.

 方法1 (低效)

  select emp_name , salary , grade      from emp      where emp_no = 342;      select emp_name , salary , grade      from emp      where emp_no = 291;   

方法2 (高效)

  select a.emp_name , a.salary , a.grade,          b.emp_name , b.salary , b.grade      from emp a,emp b      where a.emp_no = 342      and  b.emp_no = 291;   

6.删除重复记录

最高效的删除重复记录方法 ( 因为使用了rowid)

  delete from emp e   where e.rowid > (select min(x.rowid)             from emp x             where x.emp_no = e.emp_no);   

7.用truncate替代delete

当删除表中的记录时,在通常情况下, 回滚段(rollback segments ) 用来存放可以被恢复的信息. 如果你没有commit事务,oracle会将数据恢复到删除之前的状态(准确地说是恢复到执行删除命令之前的状况),而当运用truncate时, 回滚段不再存放任何可被恢复的信息.当命令运行后,数据不能被恢复.因此很少的资源被调用,执行时间也会很短.

8.尽量多使用commit

只要有可能,在程序中尽量多使用commit, 这样程序的性能得到提高,需求也会因为commit所释放的资源而减少:

commit所释放的资源:

a.  回滚段上用于恢复数据的信息.

b.  被程序语句获得的锁

c.  redo log buffer 中的空间

d.  oracle为管理上述3种资源中的内部花费(在使用commit时必须要注意到事务的完整性,现实中效率和事务完整性往往是鱼和熊掌不可得兼)

9.减少对表的查询

在含有子查询的sql语句中,要特别注意减少对表的查询.

例如:

低效:

  select tab_name         from tables         where tab_name = ( select tab_name                    from tab_columns                    where version = 604)         and db_ver= ( select db_ver                 from tab_columns                 where version = 604   

高效:

  select tab_name         from tables         where  (tab_name,db_ver)   = ( select tab_name,db_ver)             from tab_columns             where version = 604) 


update 多个column 例子:

低效:

  update emp         set emp_cat = (select max(category) from emp_categories),           sal_range = (select max(sal_range) from emp_categories)         where emp_dept = 0020;   

高效:

  update emp         set (emp_cat, sal_range)   = (select max(category) , max(sal_range)   from emp_categories)         where emp_dept = 0020;   

10.用exists替代in,用not exists替代not in

在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接.在这种情况下, 使用exists(或not exists)通常将提高查询的效率.

低效:

  select *   from emp (基础表)   where empno > 0   and deptno in (select deptno   from dept   where loc = ‘melb')   

高效:

  select *   from emp (基础表)   where empno > 0   and exists (select ‘x'   from dept   where dept.deptno = emp.deptno   and loc = ‘melb')   

(相对来说,用not exists替换not in 将更显著地提高效率)

在子查询中,not in子句将执行一个内部的排序和合并. 无论在哪种情况下,not in都是最低效的 (因为它对子查询中的表执行了一个全表遍历).   为了避免使用not in ,我们可以把它改写成外连接(outer joins)或not exists.

例如:

  select …   from emp   where dept_no not in (select dept_no                from dept                where dept_cat='a');   

为了提高效率.改写为:

(方法一: 高效)

  select ….   from emp a,dept b   where a.dept_no = b.dept(+)   and b.dept_no is null   and b.dept_cat(+) = 'a'   

(方法二: 最高效)

  select ….   from emp e   where not exists (select 'x'              from dept d              where d.dept_no = e.dept_no              and dept_cat = 'a');   

当然,最高效率的方法是有表关联.直接两表关系对联的速度是最快的!

11.识别’低效执行’的sql语句

用下列sql工具找出低效sql:

  select executions , disk_reads, buffer_gets,        round((buffer_gets-disk_reads)/buffer_gets,2) hit_radio,        round(disk_reads/executions,2) reads_per_run,        sql_text   from  v$sqlarea   where  executions>0   and   buffer_gets > 0   and (buffer_gets-disk_reads)/buffer_gets < 0.8   order by 4 desc;   

(虽然目前各种关于sql优化的图形化工具层出不穷,但是写出自己的sql工具来解决问题始终是一个最好的方法)

以上就是sql server多表查询优化方案的相关知识,希望本次的介绍能够对你有所收获!

需要了解更多数据库技术:SQL Server多表查询优化方案集锦,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/615433.html

(0)
上一篇 2021年5月20日
下一篇 2021年5月20日

精彩推荐