优化SQL SERVER系统性能

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,基础系列 2核4GB
简介: 原文 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、注意数据库函数的限制以及其性能

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
9天前
|
SQL 存储 UED
系统里这个同时查冷热表的sql,动动手指,从8s降到3s
系统将交易数据按交易时间分为热表(最近3个月)和冷表(3个月前)。为保证用户体验,当企业门户端查询跨越冷热表时,尤其针对大客户,查询性能优化至关重要。以下是程序的SQL查询语句及其优化版本。
21 1
|
22天前
|
SQL 存储 监控
SQLServer事务复制延迟优化之并行(多线程)复制
【9月更文挑战第12天】在SQL Server中,事务复制延迟会影响数据同步性。并行复制可通过多线程处理优化这一问题,提高复制效率。主要优化方法包括:配置分发代理参数、优化网络带宽、调整系统资源、优化数据库设计及定期监控维护。合理实施这些措施可提升数据同步的及时性和可靠性。
|
2月前
|
前端开发 C# 设计模式
“深度剖析WPF开发中的设计模式应用:以MVVM为核心,手把手教你重构代码结构,实现软件工程的最佳实践与高效协作”
【8月更文挑战第31天】设计模式是在软件工程中解决常见问题的成熟方案。在WPF开发中,合理应用如MVC、MVVM及工厂模式等能显著提升代码质量和可维护性。本文通过具体案例,详细解析了这些模式的实际应用,特别是MVVM模式如何通过分离UI逻辑与业务逻辑,实现视图与模型的松耦合,从而优化代码结构并提高开发效率。通过示例代码展示了从模型定义、视图模型管理到视图展示的全过程,帮助读者更好地理解并应用这些模式。
58 0
|
2月前
|
Java XML Maven
跨越时代的飞跃:Struts 2 升级秘籍——从旧版本无缝迁移到最新版,焕发应用新生!
【8月更文挑战第31天】随着软件技术的发展,Struts 2 框架也在不断更新。本文通过具体案例指导开发者如何从旧版平滑升级到 Struts 2.6.x。首先更新 `pom.xml` 中的依赖版本,并执行 `mvn clean install`。接着检查 `struts.xml` 配置,确保符合新版本要求,调整包扫描器等设置。审查 Action 类及其注解,检查配置文件中的弃用项及插件。更新自定义拦截器实现,并验证日志配置。最后,通过一系列测试确保升级后的系统正常运行。通过这些步骤,可以顺利完成 Struts 2 的版本升级,提升应用的安全性和性能。
94 0
|
2月前
|
SQL 存储 数据库
|
2月前
|
SQL 数据管理 关系型数据库
SQL与云计算:利用云数据库服务实现高效数据管理——探索云端SQL应用、性能优化、安全性与成本效益,为企业数字化转型提供全方位支持
【8月更文挑战第31天】在数字化转型中,企业对高效数据管理的需求日益增长。传统本地数据库存在局限,而云数据库服务凭借自动扩展、高可用性和按需付费等优势,成为现代数据管理的新选择。本文探讨如何利用SQL和云数据库服务(如Amazon RDS、Google Cloud SQL和Azure SQL Database)实现高效的数据管理。通过示例和最佳实践,展示SQL在云端的应用、性能优化、安全性及成本效益,助力企业提升竞争力。
45 0
|
2月前
|
SQL 关系型数据库 MySQL
SQL性能调优的神奇之处:如何用优化技巧让你的数据库查询飞起来,实现秒级响应?
【8月更文挑战第31天】在现代软件开发中,数据库性能至关重要。本文通过一个实战案例,展示了从慢查询到秒级响应的全过程。通过对查询的详细分析与优化,包括创建索引、改进查询语句及数据类型选择等措施,最终显著提升了性能。文章还提供了示例代码及最佳实践建议,帮助读者掌握SQL性能调优的核心技巧。
46 0
下一篇
无影云桌面