2016-06-10 116 views
0

我有以下看法:改善視圖性能

SELECT DISTINCT 
    IdOpportunity, IsNull(ShortName,'-') as ShortName, 
    OpportunityDesc,IdClientSolic, ClientSolicDesc, IdSocResp, 
    SocRespName, IdGroupAdm, 
    GroupAdmDesc, IdOffice, OfficeDesc, 
    IdArea, AreaDesc, OperationType 
FROM 
    (SELECT 
     [OPER_ID] as IdOpportunity, 
     o.CASETITLE as OpportunityDesc, 
     o.CLIENTSOLICID as IdClientSolic, 
     o.CLIENTSOLICNAME as ClientSolicDesc, 
     o.GROUPADMID as IdGroupAdm, 
     o.GROUPADMDESC as GroupAdmDesc, o.OFFICEID as IdOffice, 
     o.OFFICEDESCRIPTION as OfficeDesc, 
     o.AREAID as IdArea, o.AREADESC as AreaDesc, 
     CASE 
      WHEN LEN(partner_no) = 10 
       THEN (SELECT DISTINCT shortname 
         FROM [SAP].dbo.tb_user_list_prod 
         WHERE empid = right(partner_no, 8) 
         AND year(enddate) = 9999) 
      ELSE partner_no 
     END as ShortName, 
     o.TYPEOPERATION as OperationType, u.shortname as IdSocResp, 
     u.NAME + ' ' + u.Lastname_1 + ' ' + u.lastname_2 as SocRespName 
    FROM 
     [SAP].[dbo].[ZTB_OPPMATT_BP] 
    INNER JOIN 
     [SAP].dbo.tb_opportunity_list_prod o ON o.OPPORTUNITYID = [oper_id] 
    INNER JOIN 
     [SAP].[dbo].[tb_user_list_prod] u ON u.empid = SUBSTRING(o.[LEADINGID], 3, LEN(o.[LEADINGID])) 
    WHERE 
     partner_fct IN ('ZCREA','Z6','Z2','ZLEAD','00000014','00000032') 
     AND PROCESS_TYPE IN ('ZOSE','ZOPB','ZOPP')) as WorkGroupView 
WHERE 
    ShortName <> '' AND ShortName <> '-' 

基表具有以下信息:

  • ZTB_OPPMATT_BP:145000行
  • tb_opportunity_list_prod:46000行
  • tb_user_list_prod :120000行

如果我嘗試在sql server中執行這個視圖,需要大約16-20秒才能完成150.000行(現在,未來會增加)。

如何提高視圖性能?

我閱讀了索引視圖,但基表並不是靜態的,所以在創建新用戶/機會/任何時候需要更新的索引看起來很危險。此外,我不能修改基表,只是閱讀它們。

我希望你能提供任何幫助。

感謝

+2

你不必做任何事情的索引視圖以保持最新狀態,這是自動的。看看你的實際執行計劃,看看有什麼花時間。確保您的索引是最新的。另外,從該CASE表達式中取出該子查詢,將其作爲交叉連接添加,如果您需要 –

+1

該查詢非常依賴於表中的索引結構。向我們展示指數捍衛。 – xdd

+0

查看每個「DISTINCT」。其次是敏感 - 它必須是TOP 1或者因爲結果中有多行而必須失敗。當然,連接中的子串是一個壞主意。 –

回答

0

您可以嘗試建立自己的視圖,而不子查詢:

SELECT [OPER_ID] as IdOpportunity, 
     CASE WHEN LEN(partner_no) = 10 THEN u1.shortname ELSE partner_no END as ShortName, 
     o.CASETITLE as OpportunityDesc, 
     o.CLIENTSOLICID as IdClientSolic, 
     o.CLIENTSOLICNAME as ClientSolicDesc, 
     u.shortname as IdSocResp, 
     u.NAME + ' ' + u.Lastname_1 + ' ' + u.lastname_2 as SocRespName, 
     o.GROUPADMID as IdGroupAdm, 
     o.GROUPADMDESC as GroupAdmDesc, 
     o.OFFICEID as IdOffice, 
     o.OFFICEDESCRIPTION as OfficeDesc, 
     o.AREAID as IdArea, 
     o.AREADESC as AreaDesc, 
     o.TYPEOPERATION as OperationType 
FROM [SAP].[dbo].[ZTB_OPPMATT_BP] 
INNER JOIN [SAP].dbo.tb_opportunity_list_prod o 
    ON o.OPPORTUNITYID = [oper_id] 
INNER JOIN [SAP].[dbo].[tb_user_list_prod] u 
    ON u.empid = SUBSTRING(o.[LEADINGID], 3, LEN(o.[LEADINGID])) 
LEFT JOIN [SAP].dbo.tb_user_list_prod u1 
    ON u1.empid = right(partner_no, 8) AND year(u1.enddate) = 9999 
WHERE partner_fct IN ('ZCREA','Z6','Z2','ZLEAD','00000014','00000032') 
     AND PROCESS_TYPE IN ('ZOSE','ZOPB','ZOPP') 
     AND (CASE WHEN LEN(partner_no) = 10 THEN u1.shortname ELSE partner_no END) NOT IN ('','-')