技术开发 频道

SQL Server性能调优札记

  分析问题

  将数据库备份到一台测试的服务器上,查看一下数据库的数据文件和日志文件情况,发现日志文件比较大,貌似这也是一个SQL Server 存在的问题,具体原因不清楚,但是解决方法已经非常成熟,该问题暂时不影响使用,先不管它。

  定位出了问题,找出了多条Top SQL,其中这条最过分。

exec oa_SWLIST
'glzyf',
'(s.fileSerialNumber like ''%%'' or s.title like ''%%'' or s.keywords like ''%%'' or s.fileZi like ''%%'') and ',
' (  ft.userid=''glzyf'' ) '

  分析一下这个存储过程,我习惯是先看看SQL语句的结构,而不是马上看执行计划,或者直接跑语句获得统计信息。

ALTER PROCEDURE [dbo].[oa_SWLIST]
    
@userID varchar(20),
    
@sql varchar(1000),
    
@userIDs varchar(1000)

AS
DECLARE @SQL1 nchar(4000) ;
DECLARE @SQL2 nchar(4000) ;

create table #employees(
[id] [int] IDENTITY(1,1) NOT NULL,parentId int,pkId int,status int,title nvarchar(1500),comeOrg nvarchar(100),
fileDate
DateTime,fileName nvarchar(4000),filePath nvarchar(4000),readStatus nvarchar(10),optionStatus nvarchar(10),
depId
nvarchar(20),urgencyLevel nvarchar(10));

set @SQL1='insert into #employees (parentId,pkId,status,title,comeOrg,fileDate,fileName,filePath,readStatus,optionStatus,depId,urgencyLevel)
select distinct s.parentId,s.pkId,0,s.title,s.comeOrg,s.fileDate,
    s.fileName,s.filePath,ft.readStatus,0,s.remark3,
case
when urgencyLevel=
''普通'' then 0
when urgencyLevel=
''急件'' then 1
when urgencyLevel=
''特办'' then 2
when urgencyLevel=
''特急件'' then 3
when urgencyLevel=
''限时'' then 4
else 0
end as urgencyLevel
      from ShouWen as s ,  
FlowTurning as ft where
'+@sql+' ft.status=0 and ft.type=''sw''
and s.pkid=ft.pkid and s.status<>
''4'' and '+@userIDs+' order by urgencyLevel desc,s.filedate desc'

set @SQL2='insert into #employees (parentId,pkId,status,title,comeOrg,fileDate,
fileName,filePath,readStatus,optionStatus,depId,urgencyLevel)
select distinct s.parentId,s.pkId,1,s.title,s.comeOrg,s.fileDate,
    s.fileName,s.filePath,1,l.optionstatus,s.remark3,urgencyLevel
    from shouwen as s, log as l where
'+@sql+' s.status<>''4'' and s.pkid in
(
select distinct(mid) from log where uid=
'''+@userID+''' and typeid=''shouwen''
)and l.mid=s.pkid and uid=
'''+@userID+''' and typeid=''shouwen''
order by s.fileDate desc
'
print (@SQL1);
exec (@SQL1)
print ('+++++++++++++++++++++++++++++++++++');    
print (@SQL2);
exec (@SQL2)
select * from #employees
0
相关文章