当前位置: 移动技术网 > IT编程>数据库>MSSQL > SQLSERVER SQL性能优化技巧

SQLSERVER SQL性能优化技巧

2017年12月12日  | 移动技术网IT编程  | 我要评论

古典吉他入门,男子杀妻自缢身亡,轩家逗比二莫

1.选择最有效率的表名顺序(只在基于规则的优化器中有效)

sqlserver的解析器按照从右到左的顺序处理from子句中的表名,因此from子句中写在最后的表(基础表driving table)将被最先处理,在from子句中包含多个表的情况下,必须选择记录条数最少的表作为基础表,当sqlserver处理多个表时,会运用排序及合并的方式连接它们,

首先,扫描第一个表(from子句中最后的那个表)并对记录进行排序;然后扫描第二个表(from子句中最后第二个表);最后将所有从第二个表中检索出的记录与第一个表中合适记录进行合并
例如: 表 tab1 16,384 条记录表 tab2 5 条记录,选择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 

2.where子句中的连接顺序

sqlserver采用自下而上的顺序解析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'; 

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


4.减少访问数据库的次数。当执行每条sql语句时,sqlserver在内部执行了许多工作:解析sql语句,估算索引的利用率,绑定变量,读数据块等等
由此可见,减少访问数据库的次数,就能实际上减少sqlserver的工作量,例如:
以下有三种方法可以检索出雇员号等于0342或0291的职员
方法1 (最低效)

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

方法2 (次低效)

declare 
cursor c1 (e_no number) is 
select emp_name,salary,grade 
from emp 
where emp_no = e_no; 
begin 
open c1(342); 
fetch c1 into …,…,…; 
… 
open c1(291); 
fetch c1 into …,…,…; 
… 
close c1; 
end; 

方法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; 

5.使用decode函数来减少处理时间

使用decode函数可以避免重复扫描相同记录或重复连接相同的表

例如:

select count(*), sum(sal) 
from emp 
where dept_no = '0020' 
and ename like 'smith%'; 
 
select count(*), sum(sal) 
from emp 
where dept_no = '0030' 
and ename like 'smith%'; 

你可以用decode函数高效地得到相同结果

select count(decode(dept_no, '0020', 'x', null)) d0020_count, 
count(decode(dept_no, '0030', 'x', null)) d0030_count, 
sum(decode(dept_no, '0020', sal, null)) d0020_sal, 
sum(decode(dept_no, 0030, sal, null)) d0030_sal 
from emp 
where ename like 'smith%'; 

'x'表示任何一个字段
类似的,decode函数也可以运用于group by和order by子句中

6.用where子句替换having子句

避免使用having子句,having只会在检索出所有记录之后才对结果集进行过滤,这个处理需要排序、统计等操作

如果能通过where子句限制记录的数目,那就能减少这方面的开销

例如:
低效

select region, avg(log_size) 
from location 
group by region 
having region region != 'sydney' 
and region != 'perth' 
 
高效 
select region, avg(log_size) 
from location 
where region region != 'sydney' 
and region != 'perth' 
group by region 

7.减少对表的查询

在含有子查询的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; 

8.使用表的别名(alias),当在sql语句中连接多个表时,请使用表的别名并把别名前缀于每个column上,这样可以减少解析的时间并减少那些由column歧义引起的语法错误

9.用exists替代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') 

10.用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.用表连接替换exists

通常来说,采用表连接的方式比exists更有效率

例如:

select ename 
from emp e 
where exists (select 'x' 
from dept 
where dept_no = e.dept_no 
and dept_cat = 'a'); 
 
更高效 
select ename 
from dept d, emp e 
where e.dept_no = d.dept_no 
and dept_cat = 'a'; 

12.用exists替换distinct

当提交一个包含多表信息(比如部门表和雇员表)的查询时,避免在select子句中使用distinct,一般可以考虑用exist替换

例如:

低效 
select distinct dept_no, dept_name 
from dept d, emp e 
where d.dept_no = e.dept_no 
 
高效 
select dept_no, dept_name 
from dept d 
where exists (select 'x' 
from emp e 
where e.dept_no = d.dept_no); 

exists使查询更为迅速,因为rdbms核心模块将在子查询的条件一旦满足后,立刻返回结果

13.用索引提高效率

索引是表的一个概念部分,用来提高检索数据的效率。实际上,sqlserver使用了一个复杂的自平衡b-tree结构

通常,通过索引查询数据比全表扫描要快。当sqlserver找出执行查询和update语句的最佳路径时,sqlserver优化器将使用索引

同样,在联结多个表时使用索引也可以提高效率。另一个使用索引的好处是,它提供了主键(primary key)的唯一性验证

除了那些long或long raw数据类型,你可以索引几乎所有的列

通常在大型表中使用索引特别有效,当然,在扫描小表时,使用索引同样能提高效率

虽然使用索引能得到查询效率的提高,但是我们也必须注意到它的代价

索引需要空间来存储,也需要定期维护,每当有记录在表中增减或索引列被修改时,索引本身也会被修改

这意味着每条记录的insert、delete、update将为此多付出4、5次的磁盘i/o

因为索引需要额外的存储空间和处理,那些不必要的索引反而会使查询反应时间变慢

sqlserver对索引有两种访问模式:

1).索引唯一扫描(index unique scan)

大多数情况下, 优化器通过where子句访问index

例如:
表lodging有两个索引:建立在lodging列上的唯一性索引lodging_pk和建立在manager列上的非唯一性索引lodging$manager
select *
from lodging
where lodging = 'rose hill';

在内部,上述sql将被分成两步执行:

首先,lodging_pk索引将通过索引唯一扫描的方式被访问,获得相对应的rowid;然后通过rowid访问表的方式执行下一步检索

如果被检索返回的列包括在index列中,sqlserver将不执行第二步的处理(通过rowid访问表)

因为检索数据保存在索引中,单单访问索引就可以完全满足查询结果

2).索引范围查询(index range scan)

适用于两种情况:

1>.基于唯一性索引的一个范围的检索

2>.基于非唯一性索引的检索

例1
select lodging
from lodging
where lodging like 'm%';

where子句条件包括一系列值,sqlserver将通过索引范围查询的方式查询lodging_pk

由于索引范围查询将返回一组值,它的效率就要比索引唯一扫描低一些

例2
select lodging
from lodging
where manager = 'bill gates';

这个sql的执行分两步,lodging$manager的索引范围查询(得到所有符合条件记录的rowid),通过rowid访问表得到lodging列的值

由于lodging$manager是一个非唯一性的索引,数据库不能对它执行索引唯一扫描

where子句中,如果索引列所对应的值的第一个字符由通配符(wildcard)开始,索引将不被采用
select lodging
from lodging
where manager like '%hanman';

在这种情况下,sqlserver将使用全表扫描

14.避免在索引列上使用计算

where子句中,如果索引列是函数的一部分,优化器将不使用索引而使用全表扫描

例如:

低效
select …
from dept
where sal * 12 > 25000;

高效
select …
from dept
where sal > 25000/12;

请务必注意,检索中不要对索引列进行处理,如:trim,to_date,类型转换等操作,破坏索引,使用全表扫描,影响sql执行效率

15.避免在索引列上使用is null和is not null

避免在索引中使用任何可以为空的列,sqlserver将无法使用该索引

对于单列索引,如果列包含空值,索引中将不存在此记录;

对于复合索引,如果每个列都为空,索引中同样不存在此记录。如果至少有一个列不为空,则记录存在于索引中

如果唯一性索引建立在表的a列和b列上,并且表中存在一条记录的a,b值为(123,null),

sqlserver将不接受下一条具有相同a,b值(123,null)的记录插入

如果所有的索引列都为空,sqlserver将认为整个键值为空,而空不可能等于空,因此你可以插入1000条具有相同键值的记录,当然它们都是空!

因为空值不存在于索引列中,所以where子句中对索引列进行空值比较将使sqlserver停用该索引

低效(索引失效)
select …
from department
where dept_code is not null

16.使用union-all和union

当sql语句需要union两个查询结果集合时,这两个结果集合会以union-all的方式被合并,然后在输出最终结果前进行排序

如果用union all替代union,这样排序就不是必要了,效率就会因此得到提高

需要注意的是,union all将重复输出两个结果集合中相同记录,因此还是要从业务需求分析使用union all的可行性

关于索引下列经验请参考:

1).如果检索数据量超过30%的表中记录数,使用索引将没有显著的效率提高

2).在特定情况下,使用索引也许会比全表扫描慢,但这是同一个数量级上的差距;而通常情况下,使用索引比全表扫描要快几倍乃至几千倍!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网