如何让我的查询更快

时间:2016-03-03 06:43:17

标签: sql sql-server sql-server-2008 stored-procedures query-optimization

我的查询时间超过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'

2 个答案:

答案 0 :(得分:0)

我很难在我的数据库中创建类似的结构,但下面的建议很少。

  • 写着(Nolock)&#39;在所有表名结束(也加入连接)
  • 继续练习写关键词如内连接,左连接,
  • 需要检查索引
  • 每天早上运行所有表格的统计信息(https://msdn.microsoft.com/en-IN/library/ms187348.aspx
  • 正确的索引要求。
  • 在Where子句中添加条件而不是像&#39; AND l.LOCADESC这样的连接,例如&#39; DEX%&#39; AND b.INACTIVE&lt;&gt; &#39; Y&#39;&#39; 让我进一步了解改进程度,以便我们进一步了解。

答案 1 :(得分:-1)

1)首先删除distinct子句并将所有列放在group by中,这样它只能通过group by返回不同的值。 2)检查是否有任何缺失的索引 3)分享解释paln。