SQL优化(数据库的优化)

 1. 尽量使用列名,少用 *

 2. where解析顺序: 右--》 左

 3. 尽量使用where

SQL> --求平均工资大于2000的部门
SQL> select deptno,avg(sal)
  2  from emp
  3  group by deptno
  4  having avg(sal) > 2000;

    DEPTNO   AVG(SAL)                                                                                                                                                                                   
---------- ----------                                                                                                                                                                                   
        20       2175                                                                                                                                                                                   
        10 2916.66667                                                                                                                                                                                   

SQL> --where和having的区别:where后面不能使用多行函数
SQL> --查询10号部门的平均工资
SQL> select deptno,avg(sal)
  2  from emp
  3  group by deptno
  4  having deptno=10;

    DEPTNO   AVG(SAL)                                                                                                                                                                                   
---------- ----------                                                                                                                                                                                   
        10 2916.66667                                                                                                                                                                                   

SQL> ed
已写入 file afiedt.buf

  1  select deptno,avg(sal)
  2  from emp
  3  where deptno=10
  4* group by deptno
SQL> /

    DEPTNO   AVG(SAL)                                                                                                                                                                                   
---------- ----------                                                                                                                                                                                   
        10 2916.66667                                                                                                                                                                                   

SQL> --SQL 优化原则3. 尽量使用where

 

posted @ 2017-08-10 15:17  代码改变心态  阅读(202)  评论(0编辑  收藏  举报