随着企业数据库的急剧膨 胀和日益复杂,DBA为保证数据库性能所付出的努力与日俱增,手工或使用 多种无法集成的管理工具,都会给日常管理和维护带来不必要的困难。 Quest Central for Databases 是一种集成化、图形化、跨平台的数据库管理解决方案,可以管理异构环境下的 Oracle、DB2 和 SQL server 数据库。Quest Central for Databases 消除了企业IT人员管理多种数据库时面临的技术障碍,提高了IT人员工作效率,改善了数据库性能和数据库应用的可用性。为灵活满足用户的不同需求,Quest Central for Databases 采用了模块化产品架构。
该产品包括下列功能模块:
1. 数据库管理(DBA)
2. 数据库监控(Monitoring Pack)
3. 数据库诊断 (Spotlight Diagnostics)
4. 数据库分析 (Database Analysis)
6. 空间管理 (Space Management)
7. 压力测试 (Load Generator)
8. 数据生成 (Data Generator)
9. PL/SQL 开发 (TOAD)
10. 专家建议 (Knowledge Expert)
今天主要介绍其中的SQL优化工具—— SQL Tuning for SQL Server
一、SQL Tuning for SQL Server简介
SQL语句的优化对发挥 数据库的最佳性能非常关键。然而不幸的是,应用优化通常由于时间和资源的因素而被忽略。SQL Tuning (SQL优化)模块可以对比 和评测特定应用中SQL语句的运行性能,提出智能化的优化建议,帮助用户 改善应用的响应时间。SQL优化模块具有非介入式SQL采集、自动优化和专家建议等功能,全面改善SQL优化工作。
二、SQL Tuning for SQL Server的使用
1、打开Quest Database Management Solutions弹出窗口如图1所示
图1
2、在红色标记处打开SQL Tuning 优化SQL
(1)建立连接。
在Quest Central主界面上的“Database”树上选择“SQL Server”,然后在下方出现的“Tools”框中选择“SQL Tuning”选项,打开“Lanch SQL Tuning for SQL Server Connections”对话框(图2、图3)。我们在这 里建立数据库服务器的连接,以后的分析工作都会在它上面完成。
图2 “建立连接”对话框
图3
双击“New Connection”图标,在弹出窗口中输入数据库的信息,单击“OK”,然后单击“Connect”即可。
(2)分析原始SQL语句 ,在单击“Connect”后将弹出一个新窗口,如图4
图4
在打开窗口的“Oriangal SQL”文本框内输入需要分析的原始SQL语句,红色标记处选择对应的数据库名,SQL语句代码如下:
dbo.Graduater_GraduaterRegist.RegistTime AS BaoDaoTime,
dbo.Graduater_GraduaterRegist.RegistMan AS RegistMan,
dbo.Graduater_Business.ComeFrom AS ComeFrom,
dbo.Graduater_Business.Code AS Code, dbo.Graduater_Business.Status AS Status,
dbo.Graduater_Business.ApproveResult AS ApproveResult,
dbo.Graduater_Business.NewCorp AS NewCorp,
dbo.Graduater_Business.CommendNumber AS CommendNumber,
dbo.Graduater_Business.EmployStatus AS EmployStatus,
dbo.Graduater_Business.NewCommendTime AS NewCommendTime,
dbo.Graduater_Business.GetSource AS GetSource,
dbo.Graduater_Business.EmployTime AS EmployTime,
dbo.Graduater_Business.Job AS Job, dbo.Graduater_Business.FillMan AS FillMan,
dbo.Graduater_Business.FillTime AS FillTime,
dbo.Graduater_Business.IsCommendOK AS IsCommendOK,
dbo.Graduater_Business.ApproveUser AS ApproveUser,
dbo.Graduater_Business.ApproveTime AS ApproveTime,
dbo.Graduater_Business.RegistTime AS RegistTime,
dbo.Graduater_Business.EmployCorp AS EmployCorp,
dbo.Graduater_Business.JobRemark AS JobRemark,
CASE WHEN dbo.Graduater_Business.ComeFrom = ‘WS‘ THEN ‘网上登记‘ WHEN dbo.Graduater_Business.ComeFrom
= ‘HP‘ THEN ‘华普大厦‘ WHEN dbo.Graduater_Business.ComeFrom = ‘JD‘ THEN ‘精典大厦‘
WHEN dbo.Graduater_Business.ComeFrom = ‘MC‘ THEN ‘赛马场‘ WHEN ComeFrom =
‘ZX‘ THEN ‘高指中心‘ END AS ComeFromName,
dbo.Person_Contact.Address AS Address, dbo.Person_Contact.Zip AS Zip,
dbo.Person_Contact.Telephone AS Telephone, dbo.Person_Contact.Mobile AS Mobile,
dbo.Person_Contact.Email AS Email, dbo.Person_Contact.IM AS IM,
dbo.Person_Skill.ForeignLanguage AS ForeignLanguage,
dbo.Person_Skill.ForeignLanguageLevel AS ForeignLanguageLevel,
dbo.Person_Skill.CantoneseLevel AS CantoneseLevel,
dbo.Person_Skill.MandarinLevel AS MandarinLevel,
dbo.Person_Skill.Language AS Language,
dbo.Person_Skill.TechnicalTitle AS TechnicalTitle,
dbo.Person_Skill.ComputerLevel AS ComputerLevel,
dbo.Person_EmployPurpose.JobType AS JobType,
dbo.Person_EmployPurpose.Vocation AS Vocation,
dbo.Person_EmployPurpose.JobPlace AS JobPlace,
dbo.Person_EmployPurpose.Salary AS Salary,
dbo.Person_EmployPurpose.OnJobDate AS OnJobDate,
dbo.Person_EmployPurpose.CorpType AS CorpType,
dbo.Person_EmployPurpose.Job AS RequireJob, YEAR(GETDATE())
– YEAR(dbo.Person_BasicInfo.Birthday) AS Age,
dbo.Graduater_Business.EmployType AS EmployType,
dbo.Graduater_Business.EmployTypeCode AS EmployTypeCode,
dbo.Graduater_Business.EmployCorpType AS EmployCorpType,
CASE WHEN dbo.Graduater_Business.PrintStatus = ‘已打印‘ THEN ‘已打印‘ ELSE ‘未打印‘
END AS PrintStatus, dbo.Graduater_Business.PrintTime AS PrintTime,
CASE WHEN dbo.Graduater_Business.EmployStatus = ‘是‘ THEN ‘已就业‘ ELSE ‘未就业‘
END AS EmployStatusView
FROM dbo.Person_BasicInfo INNER JOIN
dbo.Graduater_Business ON
dbo.Person_BasicInfo.PersonID = dbo.Graduater_Business.PersonID LEFT OUTER JOIN
dbo.Graduater_GraduaterRegist ON
dbo.Graduater_Business.GradBusinessID = dbo.Graduater_GraduaterRegist.GraduaterGUID
INNER JOIN
dbo.Person_Contact ON
dbo.Person_BasicInfo.PersonID = dbo.Person_Contact.PersonID INNER JOIN
dbo.Person_Skill ON
dbo.Person_BasicInfo.PersonID = dbo.Person_Skill.PersonID INNER JOIN
dbo.Person_EmployPurpose ON
dbo.Person_BasicInfo.PersonID = dbo.Person_EmployPurpose.PersonID
然后点击工具栏上的“Execute”按钮,执行原始的SQL语句,SQL Tuning会自动分析SQL的执行计划,并把分析结果显示到界面上(图5)。
图5 分析原始SQL语句
(3)优化SQL。
现在我们点击工具栏上的“Optimize Statement”按钮,让SQL Tuning开始优化SQL,完成后,可以看到SQL Tuning产生了19条与原始SQL等价 的优化方案(图6)。
图6 SQL优化 方案
(4)获得最优SQL。
接下来,我们来执行上面产生的优化方案,以 选出性能最佳的等效SQL语句。在列表中选择需要执行的优化方案(默认已全部选中), 然后点击工具栏上的“Execute”按钮旁边的下拉菜单,选择“Execute Selected”。等到所有SQL运行完成后,点击界 面左方的“Tuning Resolution”按钮,
可以看到 最优的SQL已经出来啦,运行时间竟然可以提高21%!(图7)
图7 “Tuning Resolution”界面
最优的SQL语句如下:
dbo.Graduater_GraduaterRegist.RegistNO AS RegistNO,
dbo.Graduater_GraduaterRegist.RegistTime AS BaoDaoTime,
dbo.Graduater_GraduaterRegist.RegistMan AS RegistMan,
dbo.Graduater_Business.ComeFrom AS ComeFrom,
dbo.Graduater_Business.Code AS Code,
dbo.Graduater_Business.Status AS Status,
dbo.Graduater_Business.ApproveResult AS ApproveResult,
dbo.Graduater_Business.NewCorp AS NewCorp,
dbo.Graduater_Business.CommendNumber AS CommendNumber,
dbo.Graduater_Business.EmployStatus AS EmployStatus,
dbo.Graduater_Business.NewCommendTime AS NewCommendTime,
dbo.Graduater_Business.GetSource AS GetSource,
dbo.Graduater_Business.EmployTime AS EmployTime,
dbo.Graduater_Business.Job AS Job,
dbo.Graduater_Business.FillMan AS FillMan,
dbo.Graduater_Business.FillTime AS FillTime,
dbo.Graduater_Business.IsCommendOK AS IsCommendOK,
dbo.Graduater_Business.ApproveUser AS ApproveUser,
dbo.Graduater_Business.ApproveTime AS ApproveTime,
dbo.Graduater_Business.RegistTime AS RegistTime,
dbo.Graduater_Business.EmployCorp AS EmployCorp,
dbo.Graduater_Business.JobRemark AS JobRemark,
CASE WHEN dbo.Graduater_Business.ComeFrom = ‘WS‘ THEN ‘网上登记‘
WHEN dbo.Graduater_Business.ComeFrom = ‘HP‘ THEN ‘华普大厦‘
WHEN dbo.Graduater_Business.ComeFrom = ‘JD‘ THEN ‘精典大厦‘
WHEN dbo.Graduater_Business.ComeFrom = ‘MC‘ THEN ‘赛马场‘
WHEN ComeFrom = ‘ZX‘ THEN ‘高指中心‘ END AS ComeFromName,
dbo.Person_Contact.Address AS Address,
dbo.Person_Contact.Zip AS Zip,
dbo.Person_Contact.Telephone AS Telephone,
dbo.Person_Contact.Mobile AS Mobile,
dbo.Person_Contact.Email AS Email,
dbo.Person_Contact.IM AS IM,
dbo.Person_Skill.ForeignLanguage AS ForeignLanguage,
dbo.Person_Skill.ForeignLanguageLevel AS ForeignLanguageLevel,
dbo.Person_Skill.CantoneseLevel AS CantoneseLevel,
dbo.Person_Skill.MandarinLevel AS MandarinLevel,
dbo.Person_Skill.Language AS Language,
dbo.Person_Skill.TechnicalTitle AS TechnicalTitle,
dbo.Person_Skill.ComputerLevel AS ComputerLevel,
dbo.Person_EmployPurpose.JobType AS JobType,
dbo.Person_EmployPurpose.Vocation AS Vocation,
dbo.Person_EmployPurpose.JobPlace AS JobPlace,
dbo.Person_EmployPurpose.Salary AS Salary,
dbo.Person_EmployPurpose.OnJobDate AS OnJobDate,
dbo.Person_EmployPurpose.CorpType AS CorpType,
dbo.Person_EmployPurpose.Job AS RequireJob,
YEAR(GETDATE()) – YEAR(dbo.Person_BasicInfo.Birthday) AS Age,
dbo.Graduater_Business.EmployType AS EmployType,
dbo.Graduater_Business.EmployTypeCode AS EmployTypeCode,
dbo.Graduater_Business.EmployCorpType AS EmployCorpType,
CASE WHEN dbo.Graduater_Business.PrintStatus = ‘已打印‘ THEN ‘已打印‘
ELSE ‘未打印‘ END AS PrintStatus,
dbo.Graduater_Business.PrintTime AS PrintTime,
CASE WHEN dbo.Graduater_Business.EmployStatus = ‘是‘ THEN ‘已就业‘
ELSE ‘未就业‘ END AS EmployStatusView
FROM dbo.Person_BasicInfo
INNER JOIN dbo.Graduater_Business
ON dbo.Person_BasicInfo.PersonID = dbo.Graduater_Business.PersonID
LEFT OUTER JOIN dbo.Graduater_GraduaterRegist
ON dbo.Graduater_Business.GradBusinessID = dbo.Graduater_GraduaterRegist.GraduaterGUID
INNER JOIN dbo.Person_Contact
ON dbo.Person_BasicInfo.PersonID = dbo.Person_Contact.PersonID
INNER JOIN dbo.Person_Skill
ON dbo.Person_BasicInfo.PersonID = dbo.Person_Skill.PersonID
INNER JOIN dbo.Person_EmployPurpose
ON dbo.Person_BasicInfo.PersonID = dbo.Person_EmployPurpose.PersonID
OPTION (FORCE ORDER)
(5)学习书写专家级的SQL语句 。
通过上面的步骤,我们已经可以实现自动优化SQL语句,但更重要的是,我们还可以学习如何书写这样高性能的SQL语句。点击界面左方的“Compare Scenarios”按钮,我们可以比较优化方案和原始SQL中的任意2条SQL语句,SQL Tuning会将它们之间的不同之处以不同颜色表示出来,
还可以在下方的“执行计划”中,通过比较两条SQL语句的执行计划的不同,来了解其中的差异(图8)。
图8 “Compare Scenarios”界面
三、总结
SQL Tuning等人工智能自动SQL优化工具的出现,为我们节省出大量的时间和精力。借助这些工具的帮助,书写专家级的SQL语句将不再是难事。
Quest.Central.For.Databases下载地址:
http://58.251.57.206/down?cid=1669258673&t=14&fmt=&usrinput=Quest.Central&dt=0&ps=0_0&rt=0kbs&plt=0&spd=9
SQL Tuning操作手册英文版: