如何提高存储过程的速度?

时间:2016-08-19 09:05:28

标签: sql sql-server tsql sql-server-2012

我有这个存储过程,显示近70,000条记录,但需要相当长的时间才能运行,即 1分4秒

如何提升它的表现?

SELECT
    CASE WHEN c.CardID IS NULL THEN 0 ELSE c.CardID END AS CardID,
    CASE WHEN c.Description IS NULL THEN '' ELSE c.Description END AS Description,
    CASE WHEN c.Employee_ID IS NULL THEN 0 ELSE Employee_ID END AS Employee_ID,
    CASE WHEN c.ExpiryDate IS NULL THEN '1900-01-01' ELSE DATEADD(HOUR,4,c.ExpiryDate) END AS ExpiryDate,
    CASE WHEN c.IsActive IS NULL THEN '0' ELSE c.IsActive END AS IsActive,
    CASE WHEN c.IssueDate IS NULL THEN '1900-01-01' ELSE DATEADD(HOUR,4,c.IssueDate) END AS 'IssueDate',
    CASE WHEN c.PinCode IS NULL THEN 0 ELSE c.PinCode END AS PinCode,
    e.EmployeeUniqueID, e.EmployeeName,
    ER.EmployerName,
    CASE WHEN c.IsActive = '1' THEN 'Activate' ELSE 'Deactivate' END AS Status   
FROM
    Cards c  
RIGHT JOIN
    RegisteredEmployees e ON c.Employee_ID = e.ID  
INNER JOIN 
    POSSystem.DBO.Employers ER ON E.Employer_ID = ER.ID

3 个答案:

答案 0 :(得分:0)

很可能在Cards.Employee_ID上没有非唯一索引 添加它可以加快查询速度。

使用左连接比右连接更常见 大多数情况下可以用COALESCE代替。但这不应该对速度产生影响

SELECT
 COALESCE(c.CardID, 0) as CardID,
 COALESCE(c.Description, '') as Description,
 COALESCE(c.Employee_ID, 0) as Employee_ID,
 CASE when c.ExpiryDate IS NULL then '1900-01-01' else DATEADD(HOUR,4,c.ExpiryDate) end as ExpiryDate,
 COALESCE(c.IsActive, '0') as IsActive,
 CASE when c.IssueDate IS NULL then '1900-01-01' else DATEADD(HOUR,4,c.IssueDate) end as IssueDate,
 COALESCE(c.PinCode, 0) as PinCode,
 e.EmployeeUniqueID,
 e.EmployeeName,
 er.EmployerName,
 case when c.IsActive='1' then 'Activate' else 'Deactivate' end as Status
FROM RegisteredEmployees e
INNER JOIN POSSystem.DBO.Employers er ON (er.ID = e.Employer_ID)
LEFT JOIN Cards c on (e.ID = c.Employee_ID);

顺便说一句,我很惊讶有一个e.EmployeeUniqueID但是联接在e.ID上

答案 1 :(得分:0)

另外,请确保索引正在敲击的表格。

有关所有详细信息,请参阅以下链接。

https://www.simple-talk.com/sql/performance/14-sql-server-indexing-questions-you-were-too-shy-to-ask/

答案 2 :(得分:-3)

您可以使用sqls的并行执行。

像,

SELECT /*+ Parallel(s,8) */ 
       Case When c.CardID IS NULL then 0 else c.CardID end CardID  
      ,Case When c.Description IS NULL then '' else c.Description end Description  
      ,CASE when c.Employee_ID IS NULL then 0 else Employee_ID end Employee_ID 
      ,CASE when c.ExpiryDate IS NULL then '1900-01-01' else DATEADD(HOUR,4,c.ExpiryDate) end as ExpiryDate
      ,Case when c.IsActive IS NULL then '0' else c.IsActive end as IsActive  
      ,CASE when c.IssueDate IS NULL then '1900-01-01' else DATEADD(HOUR,4,c.IssueDate) end as 'IssueDate'
      ,CASE When c.PinCode IS NULL then 0 else c.PinCode end as PinCode  
      ,e.EmployeeUniqueID
      ,e.EmployeeName  
      ,ER.EmployerName
      ,case when c.IsActive='1' then 'Activate' else 'Deactivate' end as Status   
    from Cards c  
    right join RegisteredEmployees e  
    on c.Employee_ID=e.ID  
    INNER JOIN POSSystem.DBO.Employers ER
    ON E.Employer_ID=ER.ID

参考:Click Here

希望它会有所帮助。