首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化一个select查询,该查询需要1分钟才能执行

优化一个select查询,该查询需要1分钟才能执行
EN

Stack Overflow用户
提问于 2017-07-29 11:22:19
回答 2查看 41关注 0票数 1

我有一个很短的选择查询,但是它花了1分钟来执行,如何优化它?

代码语言:javascript
复制
SELECT dsss.[distID],
       dsss.[distname],
       (select count(applicationnumb) from tbl_applicant_case with(nolock) where tbl_applicant_case.[CourtDistId]=dsss.[distID] and tbl_applicant_case.case_numb is not null and [category]=N'अ-27') as ttlcaseentrered,
       (select count(applicationnumb) from tbl_applicant_case  with(nolock) where tbl_applicant_case.[CourtDistId]=dsss.[distID] and tbl_applicant_case.FinalOrder is not null and [category]=N'अ-27') as ttlcasedispopsed
FROM [tbl_district] as dsss
group by dsss.[distID], dsss.[distname]
order by ttlcaseentrered desc
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-29 11:37:08

看起来您可以简化对单个left join的查询,而不是两个子查询(因为null值不是count()编辑的):

代码语言:javascript
复制
select 
    dsss.[distID]
  , dsss.[distname]
  , ttlcaseentrered  = count(ac.case_numb)
  , ttlcasedispopsed = count(ac.FinalOrder)
from [tbl_district] as dsss
  left join tbl_applicant_case as ac with(nolock) 
    on ac.[CourtDistId]=dsss.[distID] 
   and ac.[category]=N'अ-27'
group by dsss.[distID], dsss.[distname]

这样的索引可以支持count()

代码语言:javascript
复制
create nonclustered index ix_tbl_applicant_case_CourtDistId_Category_cover 
  on tbl_applicant_case(CourtDistId, Category)
    include (CaseNumb, FinalOrder);

如果applicaitonnumb为空,则若要计数两者均为非空,可以使用条件聚合,如下所示:

代码语言:javascript
复制
select 
    dsss.[distID]
  , dsss.[distname]
  , ttlcaseentrered  = sum(case when ac.applicationnumb is not null and ac.case_numb is not null then 1 else 0 end)
  , ttlcasedispopsed = sum(case when ac.applicationnumb is not null and ac.FinalOrder is not null then 1 else 0 end)
from [tbl_district] as dsss
  left join tbl_applicant_case as ac with(nolock) 
    on ac.[CourtDistId]=dsss.[distID] 
   and ac.[category]=N'अ-27'
group by dsss.[distID], dsss.[distname]

而且索引还需要包括applicationnumb

代码语言:javascript
复制
create nonclustered index ix_tbl_applicant_case_CourtDistId_Category_cover 
  on tbl_applicant_case(CourtDistId, Category)
    include (CaseNumb, FinalOrder, applicationnumb);
票数 0
EN

Stack Overflow用户

发布于 2017-07-29 11:26:36

对于此查询:

代码语言:javascript
复制
select dsss.[distID],
       dsss.[distname],
       (select count(applicationnumb)
        from tbl_applicant_case ac
        where ac.[CourtDistId] = dsss.[distID] and
              ac.case_numb is not null and
              ac.[category] = N'अ-27'
      ) as ttlcaseentrered,
      (select count(applicationnumb) 
       from tbl_applicant_case ac
       where ac.[CourtDistId] = dsss.[distID] and
             ac.FinalOrder is not null and
             ac.[category]=N'अ-27'
      ) as ttlcasedispopsed
from [tbl_district] dsss
group by dsss.[distID], dsss.[distname]
order by ttlcaseentrered desc;

首先,对于子查询,您需要tbl_applicant_case(CourtDistId, category)上的索引。这个索引还可以包括:FinalOrdercase_numbapplicationnumb

其次,我不明白为什么在外部查询中需要GROUP BY。我建议移除它,除非你真的知道你需要删除重复的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45388689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档