zoukankan      html  css  js  c++  java
  • MYSQL优化3

    一、SQL语句优化

    ①  使用group by 分组查询是,默认分组后,还会排序,可能会降低速度,在group by 后面增加 order by null 就可以防止排序.

      explain select * from emp  group by deptno order by null;

    ②  有些情况下,可以使用连接来替代子查询。因为使用join,MySQL不需要在内存中创建临时表。

      select * from dept, emp where dept.deptno=emp.deptno; [简单处理方式]

      select * from dept left join emp on dept.deptno=emp.deptno;  [左外连接,更ok!]

    ③  对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引,应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null。最好不要给数据库留 NULL,尽可能的使用 NOT NULL 填充数据库。备注、描述、评论之类的可以设置为 NULL,其他的,最好不要使用 NULL。

      不要以为 NULL 不需要空间,比如:char(100) 型,在字段建立时,空间就固定了, 不管是否插入值(NULL 也包含在内),都是占用 100 个字符的空间的,如果是 varchar 这样的变长字段, null 不占用空间。可以在 num 上设置默认值 0,确保表中 num列没有 null 值,然后这样查询:select id from t where num = 0

  • 相关阅读:
    程序猿编程,软件设计都干什么?
    工作流——来龙去脉
    开发编码流程
    我的spring-boot开发环境
    JSR 303
    项目中java异常处理
    学习交流,一些数据结构和算法实现!
    C运算符
    js锚点
    玻璃工艺学笔记
  • 原文地址:https://www.cnblogs.com/woniusky/p/11078190.html
Copyright © 2011-2022 走看看