优化SQL SERVER系统性能

简介: 原文 http://blog.csdn.net/guoxuepeng123/article/details/7849681 1、子查询的用法 子查询是一个 select 查询,它嵌套在 SELECT、INSERT、UPDATE、DELETE 语句或其它子查询中。

原文 http://blog.csdn.net/guoxuepeng123/article/details/7849681

1、子查询的用法
子查询是一个 select 查询,它嵌套在 SELECT、INSERT、UPDATE、DELETE 语句或其它子查询中。任何允许使用表达式的地方都可以使用子查询。子查询可以使我们的编程灵活多样,可以用来实现一些特殊的功能。但是在性能上,往往一个 不合适的子查询用法会形成一个性能瓶颈。
A、NOT IN、NOT EXISTS的相关子查询可以改用LEFT JOIN代替写法。比如:
select pub_name
from publishers
where pub_id not in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select a.pub_name
from publishers a left join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
where b.pub_id is null

select title
from titles
where not exists
(select title_id
from sales
where title_id = titles.title_id)
--可以改写成:
select title
from titles left join sales
on sales.title_id = titles.title_id
where sales.title_id is null

B、 如果保证子查询没有重复 ,IN、EXISTS的相关子查询可以用INNER JOIN 代替。比如:
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select distinct a.pub_name
from publishers a inner join titles b
on b.type = 'business' and
a.pub_id=b. pub_id

C、IN的相关子查询用EXISTS代替,比如
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以用下面语句代替:
select pub_name
from publishers
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

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、注意数据库函数的限制以及其性能 1、子查询的用法
子查询是一个 select 查询,它嵌套在 SELECT、INSERT、UPDATE、DELETE 语句或其它子查询中。任何允许使用表达式的地方都可以使用子查询。子查询可以使我们的编程灵活多样,可以用来实现一些特殊的功能。但是在性能上,往往一个 不合适的子查询用法会形成一个性能瓶颈。
A、NOT IN、NOT EXISTS的相关子查询可以改用LEFT JOIN代替写法。比如:
select pub_name
from publishers
where pub_id not in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select a.pub_name
from publishers a left join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
where b.pub_id is null

select title
from titles
where not exists
(select title_id
from sales
where title_id = titles.title_id)
--可以改写成:
select title
from titles left join sales
on sales.title_id = titles.title_id
where sales.title_id is null

B、 如果保证子查询没有重复 ,IN、EXISTS的相关子查询可以用INNER JOIN 代替。比如:
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select distinct a.pub_name
from publishers a inner join titles b
on b.type = 'business' and
a.pub_id=b. pub_id

C、IN的相关子查询用EXISTS代替,比如
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以用下面语句代替:
select pub_name
from publishers
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

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、注意数据库函数的限制以及其性能

目录
相关文章
|
7月前
|
SQL 存储 监控
SQL日志优化策略:提升数据库日志记录效率
通过以上方法结合起来运行调整方案, 可以显著地提升SQL环境下面向各种搜索引擎服务平台所需要满足标准条件下之数据库登记作业流程综合表现; 同时还能确保系统稳健运行并满越用户体验预期目标.
403 6
|
SQL 关系型数据库 MySQL
MySQL进阶突击系列(07) 她气鼓鼓递来一条SQL | 怎么看执行计划、SQL怎么优化?
在日常研发工作当中,系统性能优化,从大的方面来看主要涉及基础平台优化、业务系统性能优化、数据库优化。面对数据库优化,除了DBA在集群性能、服务器调优需要投入精力,我们研发需要负责业务SQL执行优化。当业务数据量达到一定规模后,SQL执行效率可能就会出现瓶颈,影响系统业务响应。掌握如何判断SQL执行慢、以及如何分析SQL执行计划、优化SQL的技能,在工作中解决SQL性能问题显得非常关键。
|
9月前
|
SQL 容灾 安全
云时代SQL Server的终极答案:阿里云 RDS SQL Server如何用异地容灾重构系统可靠性
在数字化转型的浪潮中,数据库的高可用性已成为系统稳定性的生命线。作为经历过多次生产事故的资深开发者,肯定深知传统自建SQL Server架构的脆弱性——直到遇见阿里云 RDS SQL Server,其革命性的异地容灾架构彻底改写了游戏规则。
|
SQL 存储 自然语言处理
SQL的解析和优化的原理:一条sql 执行过程是什么?
SQL的解析和优化的原理:一条sql 执行过程是什么?
SQL的解析和优化的原理:一条sql 执行过程是什么?
|
SQL 关系型数据库 MySQL
如何优化SQL查询以提高数据库性能?
这篇文章以生动的比喻介绍了优化SQL查询的重要性及方法。它首先将未优化的SQL查询比作在自助餐厅贪多嚼不烂的行为,强调了只获取必要数据的必要性。接着,文章详细讲解了四种优化策略:**精简选择**(避免使用`SELECT *`)、**专业筛选**(利用`WHERE`缩小范围)、**高效联接**(索引和限制数据量)以及**使用索引**(加速搜索)。此外,还探讨了如何避免N+1查询问题、使用分页限制结果、理解执行计划以及定期维护数据库健康。通过这些技巧,可以显著提升数据库性能,让查询更高效流畅。
|
SQL 关系型数据库 MySQL
基于SQL Server / MySQL进行百万条数据过滤优化方案
对百万级别数据进行高效过滤查询,需要综合使用索引、查询优化、表分区、统计信息和视图等技术手段。通过合理的数据库设计和查询优化,可以显著提升查询性能,确保系统的高效稳定运行。
835 9
|
SQL 关系型数据库 索引
SQL优化常用方法53
分离表和索引
1477 0
|
SQL
SQL优化常用方法51
使用显式的游标(CURSORs)
1254 0
|
SQL
SQL优化常用方法49
优化GROUP BY
1227 0