sql 优化

来源:互联网 发布:管家婆怎么安装数据库 编辑:程序博客网 时间:2024/06/03 18:32
1.尽量不要用between .. and  ... ,使用  》=  和 《= ,
        eg: select billdate from A where  empid  between   1 and 5;   --> select billdate from A where  empid  >=  1 and empid <= 5;

2.尽量SQL不要用小写字母,都用大写字母

3.能用索引就用索引,其作用就相当于告诉数据库直接从哪里取数据就可以了,不用全部查找一遍

4.使用group by时,最后先把不用的数据排除掉,可以先在where 条件里面排除无关数据,在group by

5.in 和 not in ,换为 Exists 和 not Exists  

6.当进行数据需要多表关联时,最好使用别名

7.复杂的SQL往往会消耗大量的资源,能用函数解决,就用函数

8.程序进行select 时,后面都要commit,这会释放占用的资源

9.也是最重要的,不要用 select * from 【表名】 ,用select 【列名】 from 【表名】 。除非不知道表字段时测试用。

原创粉丝点击