zoukankan      html  css  js  c++  java
  • 数据库优化

    1、对查询进行优化,尽量避免全表扫描(select * from Table), 首先考虑在where及OrderBy使用的列加索引。

    2、尽量避免在 where 语句中对字段进行  null 值条件搜索,否则将导致引擎放弃使用索引,从而进行全表扫描,如:

         select name from Table where name is null;可以在name列设置默认值,确保该列没有null值,

         查询语句修改后:select name from Table where name = "默认值"

    3、并不是所有索引对查询都有效,SQL是根据表中的数据来进行查询优化的,当索引列有大量重复数据时,查询可能不会        去利用索引。

    4、索引并不是越多越好,索引固然可以挺高相应的查询速度,但同时也降低了 Insert和 Update的效率。

         因为Insert和Update时可能会重建索引。一个表的索引数最好不要超过6个。

    5、尽量使用数字型字段,若只含数值的字段尽量不要定义为字符型,这样会降低查询和连接的性能,并会增加存储开销。

    6、尽量避免where语句中使用 "!=" 或 "<>" 操作符,否则引擎将会放弃使用索引而进行全表扫描。

    7、尽量避免where语句中使用 or 来连接条件,否则引擎将会放弃使用索引而进行全表扫描。

          如 select name from Table where num = 1 or num = 2;

          修改后:select name from Table num  = 1 union all select name from Table where num = 2;

    8、int、not in 、like条件查询,引擎也会放弃使用索引而进行全表扫描。

    9、尽量避免where语句中对字段进行表达式操作,引擎也会放弃使用索引而进行全表扫描。

          如:select name from Table where num/2=50 

          修改后:select name from Table where num = 2 * 50;

    10、exists 可代替 in 使用

            select name from Table where id in (1,2,3)

            select name from Table where id exists (1,2,3)

    11、尽量避免使用游标,因为游标的效率低。

  • 相关阅读:
    JAVA 字符处理
    android:visibility 隐藏 可见 不可见
    Android中 int 和 String 互相转换的多种方法
    日期月和日补零
    【程序】程序报错 error:-1610149839 等大负数
    【IE】将IE11改为64位
    【linux】CentOS网络配置
    【linux】CentOS虚拟机eth0 提示Device does not seem to be present
    【SUSE】关闭防火墙
    【走马观花】十月八日通州雾
  • 原文地址:https://www.cnblogs.com/jincieryi/p/9630049.html
Copyright © 2011-2022 走看看