SQL Turning 是Quest公司出品的Quest Central軟件中的一個工具。Quest Central是一款集成化、圖形化、跨平台的數據庫管理解決方案,可以同時管理 Oracle、DB2 和 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語句代碼如下:
圖5 分析原始SQL語句
原始SQL語句
然後點擊工具欄上的“Execute”按鈕,執行原始的SQL語句,SQL Tuning會自動分析SQL的執行計劃,並把分析結果顯示到界面上(圖5)。
(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語句如下:
5)學習書寫專家級的SQL語句 。
優化後的SQL語句
SELECT dbo.Person_BasicInfo.*,
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)
(
通過上面的步驟,我們已經可以實現自動優化SQL語句,但更重要的是,我們還可以學習如何書寫這樣高性能的SQL語句。點擊界面左方的