SQL SERVER中一些常见性能问题的总结

上一篇 / 下一篇  2007-06-06 14:34:51 / 个人分类:SQL

   51Testing软件测试网}1Y!Ll&O'L d-C,ah
  作者:pbsql(风云)  51Testing软件测试网(}9DyR:W&L
  日期:2005-12-06  51Testing软件测试网#R f?"Ul
   
0_X5I c6j;Y0  1.对查询进行优化,应尽量避免全表扫描,首先应考虑在   where   及   order   by   涉及的列上建立索引。  
h2[$x7_q;IX q0   51Testing软件测试网La |&L6so%eH
  2.应尽量避免使用   left   join   和   null   值判断。left   join   比   inner   join   消耗更多的资源,因为它们包含与   null   (不存在)数据匹配的数据,所以如果可以重新编写查询以使得该查询不使用任何   inner   join   ,则会得到相应的回报。  51Testing软件测试网zaiTs/`t
  例如有两表:  51Testing软件测试网0A \0oH!ma `O([`-Re
  product(product_id   int   not   null,product_type_id   int   null,...),产品表,   product_id   为大于0的整数,   product_type_id   与表   product_type   关联,但可为空,因为有的产品没有类别  51Testing软件测试网8cH?O5O8CP
  product_type(product_type_id   not   null,product_type_name   null,...),产品类别表  51Testing软件测试网.Jf+ugw-xh
  此时要关联两表后查询   product   的内容,马上会想到使用   inner   join   ,但下面有一种方法可避免使用   inner   join   :  51Testing软件测试网7bU7]prf6D
  在   product_type   中增加一条记录:0,'',...,并将   product   的   product_type_id   设置为   not   null   ,当产品没有类别时将其   product_type_id   设为0,这样查询就可以使用   inner   join   了。  
Rp6Hi([5a9T0   51Testing软件测试网M _2\"t5n"{
  3.应尽量避免在   where   子句中使用!=或<>操作符,否则引擎可能放弃使用索引而进行全表扫描。  51Testing软件测试网My#j#z5y6W7d.g*q
   51Testing软件测试网D;l:h${m h
  4.应尽量避免在   where   子句中使用   or   来连接条件,否则将可能导致引擎放弃使用索引而进行全表扫描,如有表   t   ,   key1   、   key2   上建有索引,需要下面的存储过程:  
%u2zjoR%NY0  create   procedure   select_proc1   @key1   int=0,@key2   int=0  
W }BZD0  as  
-^:oMIMu |Y8yu!r0  begin  51Testing软件测试网0@+pW.}0HW4T
      select   key3   from   t  51Testing软件测试网&Ksk$Ju7WT C;?
      where   (@key1=0   or   key1=@key1)  51Testing软件测试网&?Mo,B8d0|'k@
          and   (@key2=0   or   key2=@key2)  51Testing软件测试网 p m)C6n}7]i1{u
  end  51Testing软件测试网:q iu'H5XH
  go  51Testing软件测试网:W0@;k(Na/@,eE4tog G
  这个存储过程会导致全表扫描,可作如下修改:  51Testing软件测试网u9R~4C \cz(_ [
  create   procedure   select_proc2   @key1   int=0,@key2   int=0  51Testing软件测试网-~2uA2}];zwl
  as  51Testing软件测试网M)O,I"nO~6FWB
  begin  51Testing软件测试网;\a!}W t"T
      if   @key1   <>0   and   @key2<>0  
/} Z_xW0          select   key3   from   t  
I1p _6n E%Yh0          where   key1=@key1   and   key2=@key2  51Testing软件测试网2Ep7V)S5P^ b'Y
      else  51Testing软件测试网^L5I\ s5m"VW
          if   @key1<>0  51Testing软件测试网&Al,B ~H8\2~.Qi
              select   key3   from   t   where   key1=@key1  51Testing软件测试网 ap+h y @7t s H
          else  
2[m'][(DRT9q8Q;E0              select   key3   from   t   where   key2=@key2  51Testing软件测试网c&Z/w ?~"yl"\+f
  end  51Testing软件测试网Z;i-uc S.|NF
  go  51Testing软件测试网2l2b`y6|u&{#vJz*P?
  更改后虽然代码增加了,但效率提高了。  
1O/b.@8LdL#T/O(o0   51Testing软件测试网.R!Gl+g/g%Z3Tv
  5.in   和   not   in   也要慎用,如:  
;L7a%Ph$l:y x0  select   id   from   t   where   num   in(1,2,3)  51Testing软件测试网n-B7BUxU5kwf
  对于连续的数值,能用   between   就不要用   in   了:  51Testing软件测试网4CZ/UxXa.r
  select   id   from   t   where   num   between   1   and   3  51Testing软件测试网 Y2F GDs x5R;H1j
   51Testing软件测试网#o ^2m8Gs3x
  6.下面的查询也将导致全表扫描:  
'd)x fc#Xm"d:Nf0  select   id   from   t   where   name   like   '%abc%'  51Testing软件测试网j2g e8v#c ^`t b
  若要提高效率,可以考虑全文检索。  51Testing软件测试网/]0qwR^;N9e
   51Testing软件测试网8k },@N\
  7.如果在   where   子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:  51Testing软件测试网j#Rqf7NK:C)C
  select   id   from   t   where   num=@num  51Testing软件测试网9iE i)k }K
  可以改为强制查询使用索引:  51Testing软件测试网hl5w fK,N9M
  select   id   from   t   with(index(索引名))   where   num=@num  51Testing软件测试网+J.gE [[}
   
u X0@@%O%w0  8.应尽量避免在   where   子句中对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描。如:  51Testing软件测试网+K![A1f:TL!G
  select   id   from   t   where   num/2=100  
MN D8?WB0  应改为:    
W B5tGD wQ| }0  select   id   from   t   where   num=100*2  
2nw&JH(NS0   51Testing软件测试网]J-w6B5R
  9.应尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描。如:  
[ in!a j2?0  select   id   from   t   where   substring(name,1,3)='abc'--name以abc开头的id  51Testing软件测试网!bdt N hAZ(z3Px
  select   id   from   t   where   datediff(day,createdate,'2005-11-30')=0--‘2005-11-30’生成的id  
T0E@"Y4t*T4\-A ^:m0  应改为:  
Kz_ G @nI!C"N0  select   id   from   t   where   name   like   'abc%'  
3Q7EJ9z5V'Dr}&I!BT9q0  select   id   from   t   where   createdate>='2005-11-30'   and   createdate<'2005-12-1'  51Testing软件测试网-Y0D"R;Bde.|R3J
   
!}/dE'l,gw,M^0  10.不要在   where   子句中的“=”左边进行函数、算术运算或其他表达式运算,否则系统将可能无法正确使用索引。  
g6r!V7CB~ R${6_0   51Testing软件测试网Xpx@GYH`E{
  11.在使用索引字段作为条件时,如果该索引是复合索引,那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引,否则该索引将不会被使用,并且应尽可能的让字段顺序与索引顺序相一致。  51Testing软件测试网 ld3L\%_M k
   
+`~"EL.?;j J g$x0  12.不要写一些没有意义的查询,如需要生成一个空表结构:  51Testing软件测试网O:}` w5j{7k-P+t*@
  select   col1,col2   into   #t   from   t   where   1=0  51Testing软件测试网G0x7p(p*ZR
  这类代码不会返回任何结果集,但是会消耗系统资源的,应改成这样:  51Testing软件测试网'{%Q!J/Ut(p w
  create   table   #t(...)  51Testing软件测试网4K9C7ca:x']B-v
   
Pr;^\0l0s4P?0  13.很多时候用   exists   代替   in   是一个好的选择:  
Gl5s8TopT0  select   num   from   a   where   num   in(select   num   from   b)  51Testing软件测试网8A;C#I ?2g,V O^
  用下面的语句替换:  
La:^F J9U0  select   num   from   a   where   exists(select   1   from   b   where   num=a.num)  51Testing软件测试网g4EBR6yg
   51Testing软件测试网*z2WEq,E1R0M'o-g9l5m
  14.并不是所有索引对查询都有效,   SQL   是根据表中数据来进行查询优化的,当索引列有大量数据重复时,SQL查询可能不会去利用索引,如一表中有字段   sex   ,   male   、   female   几乎各一半,那么即使在sex上建了索引也对查询效率起不了作用。  51Testing软件测试网iQx&`1r V,u V
   
kk(WJ"KS c^ ]0  15.索引并不是越多越好,索引固然可以提高相应的   select   的效率,但同时也降低了   insert   、   update   及   delete   的效率,因为   insert   或   update   时有可能会重建索引,所以怎样建索引需要慎重考虑,视具体情况而定。一个表的索引数最好不要超过6个,若太多则应考虑一些不常使用到的列上建的索引是否有必要。  
O4^Q PhQE0   51Testing软件测试网6f^ A3WM|k7w%s K
  16.应尽可能的避免更新   clustered   索引数据列,因为   clustered   索引数据列的顺序就是表记录的物理存储顺序,一旦该列值改变将导致整个表记录的顺序的调整,会耗费相当大的资源。若应用系统需要频繁更新   clustered   索引数据列,那么需要考虑是否应将该索引建为   clustered   索引。  
/b5|;P/A-x0   51Testing软件测试网WCpf,IFn#e
  17.尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能,并会增加存储开销。这是因为引擎在处理查询和连接时会逐个比较字符串中每一个字符,而对于数字型而言只需要比较一次就够了。  
,K,R*jR*d/Q0   51Testing软件测试网1s(HF"Tt3|v2ie
  18.尽可能的使用   varchar/nvarchar   代替   char/nchar   ,因为首先变长字段存储空间小,可以节省存储空间(定长字段即使在数据为null时也需要定长的存储空间(7.0及更高版本)),其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些,而且每页(8KB)可能存储更多的记录数,这样也可以减少I/O的消耗而提高性能。  51Testing软件测试网4~y!BJ&M"l
   51Testing软件测试网K~;]pjm+~w8o(d
  19.任何地方都不要使用   select   *   from   t   ,用具体的字段列表代替“*”,不要返回用不到的任何字段。  
Nx$F(_ |J A"D0   51Testing软件测试网4z-O]i&P
  20.尽量使用表变量来代替临时表。如果表变量包含大量数据,请注意索引非常有限(只有主键索引)。  51Testing软件测试网0C._S5Hd%tB|6u
   
a6R!U6R,w0  21.避免频繁创建和删除临时表,以减少系统表资源的消耗。  
K"o8F6})Uq)z0   51Testing软件测试网 w$JToB*T
  22.临时表并不是不可使用,适当地使用它们可以使某些例程更有效,例如,当需要重复引用大型表或常用表中的某个数据集时。但是,对于一次性事件,最好使用导出表。  51Testing软件测试网5~fC#~!c
   51Testing软件测试网;Jx6k)A6A
  23.在新建临时表时,如果一次性插入数据量很大,那么可以使用   select   into   代替   create   table,避免造成大量   log   ,以提高速度;如果数据量不大,为了缓和系统表的资源,应先   create   table   ,然后   insert   。  
(o^ pC o0   51Testing软件测试网1\ W2g8NR1H*d
  24.如果使用到了临时表,在存储过程的最后务必将所有的临时表显式删除,先   truncate   table   ,然后   drop   table   ,这样可以避免系统表的较长时间锁定。  51Testing软件测试网h?\5mxs-N
   51Testing软件测试网\ gT?"p7H#H Z
  25.尽量避免使用游标,因为游标的效率较差,如果游标操作的数据超过1万行,那么就应该考虑改写。  
Gp {Lm(eR`0   51Testing软件测试网/as#t1g`+Q2_8rw
  26.使用基于游标的方法或临时表方法之前,应先寻找基于集的解决方案来解决问题,基于集的方法通常更有效。  51Testing软件测试网"S:bb!|&W7c,j
   
0Hp0gmpx4J0  27.与临时表一样,游标并不是不可使用。对小型数据集使用   FAST_FORWARD   游标通常要优于其他逐行处理方法,尤其是在必须引用几个表才能获得所需的数据时。在结果集中包括“合计”的例程通常要比使用游标执行的速度快。如果开发时间允许,基于游标的方法和基于集的方法都可以尝试一下,看哪一种方法的效果更好。  51Testing软件测试网+~)G0f!i,}+VP
   51Testing软件测试网-o2O0H5j1HB"^
  28.在所有的存储过程和触发器的开始处设置   SET   NOCOUNT   ON   ,在结束时设置   SET   NOCOUNT   OFF   。无需在执行存储过程和触发器的每个语句后向客户端发送   DONE_IN_PROC   消息。  51Testing软件测试网B\ a"D.T G
   51Testing软件测试网kR7zH u
  29.尽量避免大事务操作,提高系统并发能力。当使用约束和触发器都能完成同样的功能时,优先考虑使用约束。  51Testing软件测试网i*W!pekUU
   
7d-U[p0d/n:Z(l+Jkj0  30.尽量避免向客户端返回大数据量,若数据量过大,应该考虑相应需求是否合理。

TAG: SQL

 

评分:0

我来说两句

Open Toolbar