• Mysql 查询优化


    where 子句中不要使用 !=<> 
    where , order by 操作会涉及到的列上建立索引
    where 子句中不要对字段进行 null 值判断
    
    将可能出现空值的列设置为 0
    在查询时,可以通过
    
    select 字段 from 表名 where 另一个字段=0
    where 子句中,不要在条件上添加 or
    select id from t where num=10 or num=20
    
    优化写法
    select id from t where num=10
    union all
    select id from t where num=20
    where 子句中,使用 like 也会导致进行全表扫描
    where 子句中 innot in ,可以不用尽量不用
    
    对于连续的数值,可以使用 between 进行选择
    
    select id from t where num in(1,2,3)
    
    优化
    
    select id from t where num between 1 and 3
    where 子句中,如果一定要有参数,可以修改为强制查询使用索引
    
    select id from t where num=
    优化
    select id from t with(index(索引名)) where num=
    Mysql
    
    SELECT * FROM t force index(指定的索引名称) WHERE num = 值;

    where 子句中,不要使用表达式操作
    
    select id from t where num/2=100
    
    优化
    
    select id from t where num=100*2

    where 子句的查询条件不要写为 函数
    where 子句的左侧不要进行函数 算术运算 其他表达式运算,可能影响系统无法使用索引
    使用 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)

    建立索引可以提高 select 的效率,但是会降低 insertupdate 的效率
    
    原因为 insertupdate 有时会重新创建索引
    可以使用数值型类型的字段,使用数值型字段
    使用字符串类型的字段,会导致多次判断,降低速度
    尽量不要使用 select * from 表名
    使用 select 字段 from 表名 

    https://www.jb51.net/article/209199.htm

    如果觉得文章不错,可以分享给其他人哟~
  • 相关阅读:
    OpenGL ES着色器语言之静态使用(static use)和预处理
    OpenGL ES着色器语言之着色概览(官方文档)
    OpenGL ES2.0入门详解
    OpenGL ES之glUniform函数
    C++矩阵处理库--Eigen初步使用
    启用PAE后虚拟地址到物理地址的转换
    八款值得尝试的精美的 Linux 发行版(2017 版)
    多了解一下Chrome开发者控制台
    [Win32]一个调试器的实现(五)调试符号
    解析pdb文件得到未导出变量地址(转)
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/15371516.html
Copyright © 2020-2023  润新知