优化SQL SERVER系统性能(二)

2014-11-24 13:26:34 · 作者: · 浏览: 1
where exists
(select 1
from titles
where type = 'business' and
pub_id= publishers.pub_id)
D、不要用COUNT(*)的子查询判断是否存在记录,最好用LEFT JOIN或者EXISTS,比如有人写这样的语句:
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)=0
--应该改成:
select jobs.job_desc from jobs left join employee
on employee.job_id=jobs.job_id
where employee.emp_id is null
www.2cto.com
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)<>0
--应该改成:
select job_desc from jobs
where exists (select 1 from employee where job_id=jobs.job_id)
作为程序员还应该注意:
1、注意、关心各表的数据量。
2、编码过程和单元测试过程尽量用数据量较大的数据库测试,最好能用实际数据测试。
3、每个SQL语句尽量简单
4、不要频繁更新有触发器的表的数据
5、注意数据库函数的限制以及其性能
作者 郭学鹏