zoukankan      html  css  js  c++  java
  • sqlserver 数据库之性能优化

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

           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毫秒!!!

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

  • 相关阅读:
    便携版Mysql安装
    Markdown 语法学习
    布局页中的特殊情况(比如说只有某页有的banner)
    Jsp Layout 布局页
    eclipse变量名自动补全
    java中的final关键字(2013-10-11-163 写的日志迁移
    java的重载(overload) (2013-10-11-163 写的日志迁移
    java 的多态(2013-10-11-163 写的日志迁移
    java中类与对象的概念(2013-05-04-bd 写的日志迁移
    java的一些相关介绍(2013-10-07-163 写的日志迁移
  • 原文地址:https://www.cnblogs.com/brain008/p/14487233.html
Copyright © 2011-2022 走看看