如何让我的查询更快
Posted
技术标签:
【中文标题】如何让我的查询更快【英文标题】:How to make my query faster 【发布时间】:2016-03-03 06:43:16 【问题描述】:我在这里的查询花费了 30 多分钟。告诉我如何让它更快?我应该怎么做才能将它优化到 1 分钟?
SELECT DISTINCT b.BLDGID AS 'CBRE Building Id',
e.CLIENTCODE AS 'DEXUS Building Id',
b.BLDGNAME AS 'Building Name',
cmgr.RMGRNAME AS 'Snr Manager AS',
cmgr.RMGREMAIL AS 'Snr Manager AS Email',
cmigr.IMGRNAME AS 'Manager AS',
cmigr.IMGREMAIL AS 'Manager AS Email',
ttj.JOBID AS 'TSR No',
ttj.JobType,
wws.JobActivity,
wrj.[Description] AS 'JobActivity Sub-Category',
ttj.JobDetail AS 'TSR Details',
wc.contname AS 'Tenant Contact',
wc.Name AS 'Tenant Company NAME',
wc.ContName AS 'Tenant Contact',
wc.Phone AS 'Tenant Phone',
wc.Mobile AS 'Tenant Mobile',
wc.Email AS 'Tenant Email',
TS.Q_Id,
TA.Answer
FROM BLDG b
JOIN dbo.ENTITY e ON e.ENTITYID = b.ENTITYID
JOIN dbo.CBRERMGR cmgr ON cmgr.RMGRID = e.RMGRID
JOIN dbo.CBREIMGR cmigr ON cmigr.USERID = cmgr.USERID
JOIN WSM_AUNZ..wsm_Ref_Buildings wsmbldg ON wsmbldg.BLDGID = b.BLDGID
JOIN WSM_AUNZ..TSP_TSR_Job ttj ON ttj.BuildingID = wsmbldg.BuildingId
JOIN WSM_AUNZ..wsm_WorkOrderSchedule wws ON wws.BuildingID = wsmbldg.BuildingId
JOIN WSM_AUNZ..wsm_Ref_JobActivityAction wrj ON wrj.JobActivityActionID = wws.JobActivityActionID
JOIN WSM_AUNZ..wsm_contact wc ON wc.BuildingID = ttj.BuildingID
JOIN WSM_AUNZ..wsm_TSRSurveyAnswer TA ON TA.JobId = ttj.JobID --AND WSM_AUNZ.dbo.wsm_TSRSurveyAnswer.clientId = wsmbldg.CLIENTID
JOIN WSM_AUNZ..wsm_TSRSurvey TS ON TS.Q_Id = TA.Q_Id
JOIN LOCA l ON e.LOCAID = l.LOCAID
AND l.LOCADESC LIKE 'DEX%'
AND b.INACTIVE <> 'Y'
【问题讨论】:
请发布执行计划simple-talk.com/sql/performance/execution-plan-basics 【参考方案1】:我很难在我的数据库中创建类似的结构,但以下建议很少。
在所有表名末尾写入“With (Nolock)”(也添加到联接中) 不断练习写Inner join、Left Join、等关键字 需要检查索引 每天早上运行所有餐桌的统计数据 (https://msdn.microsoft.com/en-IN/library/ms187348.aspx) 需要正确的索引。 在 Where 子句中添加条件,而不是像 'AND l.LOCADESC like 'DEX%' AND b.INACTIVE 'Y'' 这样的连接 让我进一步了解改进了多少,以便我们进一步研究。【讨论】:
(1)不要把 NOLOCK 放在任何可能给你脏数据的地方。 (2) 练习编写 INNER JOIN , LEFT JOIN 它不会给你带来性能上的好处 (3) 每天早上更新统计数据真的是在什么工作量下?你的答案不符合答案 - 'With (No Lock)' 提供了良好的性能,以防数据插入不连续。【参考方案2】:1) 首先删除 distinct 子句并将所有列放在 group by 中,这样它就可以通过 group by 只返回不同的值。 2)检查任何缺失的索引 3)分享解释计划。
【讨论】:
以上是关于如何让我的查询更快的主要内容,如果未能解决你的问题,请参考以下文章