zoukankan      html  css  js  c++  java
  • sql server查询优化方法(海量数据)

    此方法只适用于对海量数据查询的优化,对于数据量较少的查询不具有参考性

    一、尽量避免全表扫描,使用索引

    1.在常用字段上面建立索引,方便查找

    2.避免null值判断。eg: where  xx is null。 改进:可以设置默认值代替null

    3.避免使用不等于操作符,如!=,<> 

    4.避免使用or。 可以使用union all代替。 eg:  where  age=1 or age=2 改成 ... where  age=1 union all .... where age=2

    5.避免使用 in/not in

    6.避免左模糊查询 eg: like '%杰'

    7.where 字句中避免使用参数。如果使用参数又想使用索引:

    select * from t with(index(索引名)) where num=@num

    8.避免使用表达式 eg: where  num/2=100

    9.避免对字段进行函数操作,比如substring,convert

    10.避免在等号左侧进行函数、表达式操作

    11.用exists代替in

    select num from a where num in(select num from b)
    
    //转化成
    
    select num from a where exists(select 1 from b where num=a.num)

    12.索引具备的条件:1.常用 2.不存在大量数据重复  3.建立索引不宜过多

    13.索引会降低insert/update效率

    14.对于数字类型的字段尽量使用数字类型,不使用字符串类型

    15.尽量使用varchar/nvarchar 代替char/nchar。因为变长字段存储空间小,易查询

    16.避免使用 * ,使用字段代替

    二、关于临时表

    1.避免频繁操作临时表

    2.一次性插入数据较大,可以使用select into代替create table

    3.使用了临时表之后,必须在最后将所有临时表显示删除,可以使用truncate table,再使用 drop  table

    4.避免使用游标操作大数据量(一万条以上)

    5.避免向客户端返回大数据量

    参考博客:

    https://blog.csdn.net/u013938578/article/details/81412091

     

    记录编程的点滴,体会学习的乐趣
  • 相关阅读:
    AL&&PY&&PWNTOOLS&&C语言
    shellshock—CVE-2014-6271
    2019Hackergame-Shell骇客
    python&C&Assembly 常见函数(持续更新)
    Linux常见的Shell命令
    服务器常见错误代码500、501、502、503、504、505
    vscode常用快捷键总结
    如何用VSCode愉快的写Python
    Git 安装配置及基本操作
    Robot Framework用法全集
  • 原文地址:https://www.cnblogs.com/AduBlog/p/14411233.html
Copyright © 2011-2022 走看看