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

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

  • 相关阅读:
    重构了一波代码,聊聊后端也聊聊游戏后端
    浅谈游戏开发中常见的设计模式
    一次查内存泄露
    sql语句技巧
    python后端链接数据库-----MySQLdb
    web的应用模式
    静态文件
    django配置文件
    视图
    django子应用
  • 原文地址:https://www.cnblogs.com/brain008/p/14487233.html
Copyright © 2020-2023  润新知