如何提高此select查询的性能

时间:2014-05-08 11:09:30

标签: sql-server

如果有人建议如何使用以下查询来提高存储过程的性能,我将不胜感激(功能非常小,而且我认为它们不会影响性能):

    SELECT  DISTINCT REPLACE(REPLACE(REPLACE(c.[description], ' Org',''), ' group',''),'Organization full name','Org') 
                                                                                                AS OrgColumn1, 

                REPLACE(REPLACE(c.[description], ' Org',''), ' group','') + ' Organization'     AS OrgColumn2, 

                CASE 
                    WHEN dbo.fn_OrgGrantPageAccess(c.id,@contactId) = 1 
                    AND (c.id <> 13 AND c.id <> 31) 
                        THEN '<a href="OrgMainPage.aspx?id='+CAST((c.id) as varchar(15))+'">'+
                        replace(replace(c.[description], ' Org',''), ' group','') + ' Org</a>' 

                    WHEN dbo.fn_OrgGrantPageAccess(c.id,@contactId) = 1 
                    AND (c.id = 13 OR c.id = 31) 
                        THEN '<a href="OrgMainPage.aspx?id='+CAST((c.id) as varchar(15))+'">'+
                        c.[description]+'</a>'
                    ELSE 
                    replace(replace(c.[description], ' Org',''), ' group','') + ' Org' 
                END                                                                             AS OrgColumn3, 

                c.id                                                                            AS OrgColumn4,
                dbo.fn_OrgGetColumn5(c.id)                                                      AS OrgColumn5,
                dbo.fn_OrgGetColumn6(c.id)                                                      AS OrgColumn6,
                dbo.fn_OrgGetColumn7(c.id)                                                      AS OrgColumn7,
                dbo.fn_OrgGetColumn8(c.id)                                                      AS OrgColumn8,
                dbo.fn_OrgGetColumn9(c.id)                                                      AS OrgColumn9,
                dbo.fn_OrgGetColumn10(c.id)                                                     AS OrgColumn10

INTO            #T

FROM            table_c                 c

INNER JOIN      table_s                 s 
ON              c.id                    =s.id

INNER JOIN      table_t                 t 
ON              s.type_id               = t.type_id

LEFT OUTER JOIN table_m                 m 
ON              c.id                    = m.OrgID

LEFT OUTER JOIN table_cal               cal 
ON              m.meetingid             = cal.calid

WHERE           t.type_id               = 3
AND             active                  = 1
AND             (c.org_1                = 1 
OR              c.org_1                 = 3)

ORDER BY        OrgColumn1

1 个答案:

答案 0 :(得分:0)

我建议你将dbo.fn_OrgGetColumn函数从标量值重写为表值,然后将cross apply重写为主结果集。 如果您可以提供dbo.fn_OrgGetColumn函数的源代码,我可以帮助您将其重写为表值。

您可以通过示例找到很好的解释:Boost Your T-SQL with the APPLY