视图优化,输出中具有计数功能

问题描述 投票:0回答:1

我直接在SQL Server Management Studio中使用此以下视图,它可以正常工作,但我无法找出其变慢的原因我控制了IO和磁盘CPU和服务器有足够的RAM,内部表具有正确的索引您可以通过优化方式重写/查看它来帮助我吗?

SELECT instances.id, 
       instances.unlisted, 
       instances.unread, 
       instances.folderid, 
       instances.partitionfactor, 
       instances.draftid, 
       instances.targetletterid, 
       instances.targetpendingletterid, 
       instances.flag, 
       instances.comments, 
       instances.viewdate, 
       drafts.attachmentcount                AS [DraftAttachmentCount], 
       drafts.bodyfilecount                  AS [DraftBodyFileCount], 
       drafts.[date], 
       drafts.sendertitle, 
       drafts.[browsetoreceiverlist], 
       drafts.[priority], 
       drafts.[priorityid], 
       drafts.subject, 
       drafts.[toreceiverlist], 
       drafts.[ccreceiverlist], 
       drafts.[security], 
       drafts.[securityid], 
       drafts.[body], 
       destinationSender.[fulltitle]         AS [DestinationSenderTitle], 
       incompleteDrafts.frozenid, 
       incompleteDrafts.frozenaction, 
       ( CASE WHEN Isnull(instances.[notecount], 0) = 0 THEN '' ELSE 'Note' END 
         + CASE 
         WHEN Isnull(instances.[alertcount], 0) = 0 THEN '' ELSE 'Alert' END + 
         CASE 
         WHEN Isnull([categorycount], 0) = 0 THEN '' ELSE 'Category' END ) 
                                             [AttachedObjects], 
       drafts.[guid]                         DraftGuid, 
       (SELECT Count(*) 
        FROM   dbo.myperfixdraftdestinationreceivers 
        WHERE  draftid = drafts.id)          [DraftDestinationReceivers], 
       (SELECT Count(*) 
        FROM   dbo.myperfixdraftdestinationreceivers 
        WHERE  draftid = drafts.id 
               AND ( receivertype = 4 
                      OR receivertype = 5 )) [ExternalReceivers] 
FROM   myperfixdraftinstances instances 
       LEFT JOIN myperfixdrafts drafts 
              ON ( drafts.[partitionfactor] = instances.[partitionfactor] 
                   AND drafts.[id] = instances.[draftid] ) 
       LEFT JOIN com_staff destinationSender 
              ON ( destinationSender.[id] = drafts.[destinationsenderid] ) 
       LEFT JOIN myperfixincompletesavedrafts incompleteDrafts 
              ON ( instances.id = incompleteDrafts.draftinstanceid ) 

go 
sql performance tsql ssms sql-server-2016
1个回答
0
投票

我的猜测是相关的子查询将是罪魁祸首。您是否尝试过删除它。

并且您是否有理由进行外部联接?您从草稿中返回了很多值,但只有外部将其加入实例中。您有很多没有草稿的实例吗?

© www.soinside.com 2019 - 2024. All rights reserved.