sqlserver 数据库之性能优化

时间:2021-03-06 14:30:22   收藏:0   阅读:0

    数据库性能优化有一下几个方面:

       1、把数据、日志、索引放到不同的I/O设备上,增加读取速度;

  2、纵向、横向分割表,减少表的尺寸;

  3、根据查询条件,建立索引,优化索引、优化访问方式,限制结果集的数据量。注意填充因子要适当(最好是使用默认值0);

       4、注意UNion和UNion all 的区别。UNION all好;

  5、注意使用DISTINCT,在没有必要时不要用,它同UNION一样会使查询变慢。重复的记录在查询里是没有问题的 ;

  6、查询时不要返回不需要的行、列 ;

 

测试:

create table person1 (UserID int,pwd char(20),OtherInfo char(360),modifydate datetime)
declare @i int
set @i=0
while @i<800000
begin
insert into person1
select cast(floor(rand()*100000) as int), cast(floor(rand()*100000) as varchar(20)),
cast(floor(rand()*100000) as char(360)), GETDATE()
set @i=@i+1
end


declare @d datetime
set @d=getdate()
/*你的SQL脚本开始* 1387毫秒*/
select * from person1 where userid=‘22004‘ and modifydate>=‘2020-04-01 00:00:00.000‘
/*你的SQL脚本结束*/
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())


----- 在modifynum 上创建聚集索引
declare @d datetime
set @d=getdate()
/*你的SQL脚本开始* 20毫秒*/
select * from person1 where userid=‘22004‘ and modifydate>=‘2020-04-01 00:00:00.000‘
/*你的SQL脚本结束*/
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())

在按照日期查询的字段上创建聚集索引,速度提升10倍
CREATE clustered index index_name_test1
ON person1(字段)  --建立Id列聚集索引

删掉之前的聚集索引,(因为一张表只能有一个聚集索引)
在用户ID和日期上创建聚集索引,时间进一步得到提升,1毫秒!!!

这里只是一个简单的例子,表明创建聚集索引可以提升性能,此外,大家还可以根据具体的环境创建合适的索引

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!