当前位置: 移动技术网 > IT编程>数据库>MSSQL > SQL SERVER性能优化综述(很好的总结,不要错过哦)第1/3页

SQL SERVER性能优化综述(很好的总结,不要错过哦)第1/3页

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

六盘水汽车站,我们诞生在中国下载,爱情保卫战20121124


f、
关于临时表产生使用select into和create table + insert into的选择,我们做过测试,一般情况下,select into会比create table + insert into的方法快很多,但是select into会锁定tempdb的系统表sysobjects、sysindexes、syscolumns,在多用户并发环境下,容易阻塞其他进程,所以我的建议是,在并发系统中,尽量使用create table + insert into,而大数据量的单个语句使用中,使用select into。
g、
注意排序规则,用create table建立的临时表,如果不指定字段的排序规则,会选择tempdb的默认排序规则,而不是当前数据库的排序规则。如果当前数据库的排序规则和tempdb的排序规则不同,连接的时候就会出现排序规则的冲突错误。一般可以在create table建立临时表时指定字段的排序规则为database_default来避免上述问题。
5、
子查询的用法
子查询是一个 select 查询,它嵌套在 select、insert、update、delete 语句或其它子查询中。任何允许使用表达式的地方都可以使用子查询。
子查询可以使我们的编程灵活多样,可以用来实现一些特殊的功能。但是在性能上,往往一个不合适的子查询用法会形成一个性能瓶颈。
如果子查询的条件中使用了其外层的表的字段,这种子查询就叫作相关子查询。相关子查询可以用in、not in、exists、not exists引入。
关于相关子查询,应该注意:
a、not in、not exists的相关子查询可以改用left join代替写法。比如:
select pub_namefrom publishers
where pub_id not in (select pub_id
from titles
where type = 'business')
可以改写成:
select a.pub_namefrom publishers a left join titles b
on b.type = 'business' and a.pub_id=b. pub_id
where b.pub_id is null
select titlefrom titles
where not exists (select title_id
from sales
where title_id = titles.title_id)
可以改写成:
select titlefrom titles left join sales
on sales.title_id = titles.title_id
where sales.title_id is null
b、
如果保证子查询没有重复 ,in、exists的相关子查询可以用inner join 代替。比如:
select pub_namefrom publishers
where pub_id in (select pub_id
from titles
where type = 'business')
可以改写成:
select distinct a.pub_namefrom publishers a inner join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
c、
in的相关子查询用exists代替,比如
select pub_namefrom publishers
where pub_id in (select pub_id
from titles
where type = 'business')
可以用下面语句代替:
select pub_namefrom publishers
where exists (select 1
from titles where type = 'business' and
pub_id= publishers.pub_id)
d、不要用count(*)的子查询判断是否存在记录,最好用left join或者exists,比如有人写这样的语句:
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)=0
应该改成:
select jobs.job_desc from jobs left join employee on employee.job_id=jobs.job_id
where employee.emp_id is null
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)<>0
应该改成:
select job_desc from jobs
where exists (select 1 from employee where job_id=jobs.job_id)
6、
慎用游标
数据库一般的操作是集合操作,也就是对由where子句和选择列确定的结果集作集合操作,游标是提供的一个非集合操作的途径。一般情况下,游标实现的功能往往相当于客户端的一个循环实现的功能,所以,大部分情况下,我们把游标功能搬到客户端。
游标是把结果集放在服务器内存,并通过循环一条一条处理记录,对数据库资源(特别是内存和锁资源)的消耗是非常大的,所以,我们应该只有在没有其他方法的情况下才使用游标。
另外,我们可以用sql server的一些特性来代替游标,达到提高速度的目的。
a、字符串连接的例子
这是论坛经常有的例子,就是把一个表符合条件的记录的某个字符串字段连接成一个变量。比如需要把job_id=10的employee的fname连接在一起,用逗号连接,可能最容易想到的是用游标:
declare @[url=url]name[/url] varchar(20) declare @name varchar(1000)
declare name_cursor cursor for select fname from employee where job_id=10 order by emp_id
open name_cursor fetch next from rname_cursor into @name
while @@fetch_status = 0
begin
set @names = isnull(@names+',','')+@name fetch next from name_cursor into @name
end close name_cursor
deallocate name_cursor
可以如下修改,功能相同:
declare @name varchar(1000) select @names = isnull(@names+',','')+fname
from employee where job_id=10 order by emp_id
b、
用case when 实现转换的例子
很多使用游标的原因是因为有些处理需要根据记录的各种情况需要作不同的处理,实际上这种情况,我们可以用case when语句进行必要的判断处理,而且case when是可以嵌套的。比如:
表结构:
create table 料件表(料号 varchar(30),
名称 varchar(100),主单位 varchar(20),
单位1 varchar(20),单位1参数 numeric(18,4),
单位2 varchar(20),单位2参数 numeric(18,4)
)
go
create table 入库表(时间 datetime,
料号 varchar(30),单位 int,
入库数量 numeric(18,4),损坏数量 numeric(18,4)
)
go
其中,单位字段可以是0,1,2,分别代表主单位、单位1、单位2,很多计算需要统一单位,统一单位可以用游标实现:
declare @料号 varchar(30), @单位 int,
@参数 numeric(18,4),
declare cur cursor for select 料号,单位 from 入库表 where 单位 <>0
open curfetch next from cur into @料号,@单位
while @@fetch_status<>-1
begin
if @单位=1
begin
set @参数=(select 单位1参数 from 料件表 where 料号 =@料号) update 入库表 set 数量=数量*@参数,损坏数量=损坏数量*@参数,单位=1 where current of cur
end if @单位=2
begin set @参数=(select 单位1参数 from 料件表 where 料号 =@料号)
update 入库表 set 数量=数量*@参数,损坏数量=损坏数量*@参数,单位=1 where current of cur end
fetch next from cur into @料号,@单位end
close cur
deallocate cur
可以改写成:
update a set 数量=case a.单位 when 1 then a.数量*b. 单位1参数
when 2 then a.数量*b. 单位2参数 else a.数量
end, 损坏数量= case a.单位 when 1 then a. 损坏数量*b. 单位1参数
when 2 then a. 损坏数量*b. 单位2参数 else a. 损坏数量
end,单位=1
from入库表 a, 料件表 bwhere a.单位<>1 and
a.料号=b.料号
c、
变量参与的update语句的例子
sql erver的语句比较灵活,变量参与的update语句可以实现一些游标一样的功能,比如:

select a,b,c,cast(null as int) as 序号into #t
from 表
order by a ,newid()
产生临时表后,已经按照a字段排序,但是在a相同的情况下是乱序的,这时如果需要更改序号字段为按照a字段分组的记录序号,就只有游标和变量参与的update语句可以实现了,这个变量参与的update语句如下:
declare @a intdeclare @序号 int
update #t set @序号=case when a=@a then @序号+1 else 1 end,
@a=a,
序号=@序号
d、如果必须使用游标,注意选择游标的类型,如果只是循环取数据,那就应该用只进游标(选项fast_forward),一般只需要静态游标(选项static)。
e、
注意动态游标的不确定性,动态游标查询的记录集数据如果被修改,会自动刷新游标,这样使得动态游标有了不确定性,因为在多用户环境下,如果其他进程或者本身更改了纪录,就可能刷新游标的记录集。
2

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

相关文章:

验证码:
移动技术网