求一条SQL语句的优化

msd88 msd88 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
SQL语句如下,有什么办法效率可以高一点?BillFlow表记录数在20万条以上,UserInfo表记录数在5万条左右

Update tmpRep_Area Set AreaBilled=IsNull(a.AreaBilled,0),AreaNoBill=IsNull(c.AreaNoBill,0),AreaIsCardBilled=d.AreaIsCardBilled,AreaNoCardBilled=e.AreaNoCardBilled
From tmpRep_Area a,
(Select Bill.AreaCode,Count(Bill.AddrCode) As AreaBilled
From (Select b.AreaCode,a.AddrCode From BillFlow a
Left Join UserInfo b On b.AddrCode=a.AddrCode
Left Join AreaInfo c On c.AreaCode=b.AreaCode
Where Convert(Char(6),BillDate,112)=@RepDate
And DataFlag=0 And b.UserStatus<>'封表' And b.UserStatus<>'注销'
And a.ID Not In(Select BillFlowID From BillFlowCancel Where Convert(Char(6),CancelDate,112)=@RepDate)
Group By b.AreaCode,a.AddrCode) Bill
Group By Bill.AreaCode) b,
(Select AreaCode,Count(AddrCode) As AreaNoBill From UserInfo
Where UserStatus<>'封表' And UserStatus<>'注销'
And Convert(Char(6),CreatDate,112)<=@RepDate
And AddrCode
Not In (Select AddrCode From BillFlow
Where Convert(Char(6),BillDate,112)=@RepDate
And DataFlag=0
And id Not In(Select BillFlowID From BillFlowCancel
Where Convert(Char(6),CancelDate,112)=@RepDate))
Group By AreaCode) c,
(Select AreaCode,Count(AddrCode) As AreaIsCardBilled From UserInfo
Where UserStatus<>'封表' And UserStatus<>'注销'
And Convert(Char(6),CardCreatDate,112)<=@RepDate
And IsUseCard=1 And AddrCode
In (Select AddrCode From BillFlow
Where Convert(Char(6),BillDate,112)=@RepDate
And DataFlag=0
And id Not In(Select BillFlowID From BillFlowCancel
Where Convert(Char(6),CancelDate,112)=@RepDate))
Group By AreaCode) d,
(Select AreaCode,Count(AddrCode) As AreaNoCardBilled From UserInfo
Where UserStatus<>'封表' And UserStatus<>'注销'
And Convert(Char(6),CreatDate,112)<=@RepDate
And (IsUseCard=0 Or IsUseCard=1 And Convert(Char(6),CardCreatDate,112)>=@RepDate)
And AddrCode
In (Select AddrCode From BillFlow
Where Convert(Char(6),BillDate,112)=@RepDate
And DataFlag=0
And id Not In(Select BillFlowID From BillFlowCancel
Where Convert(Char(6),CancelDate,112)=@RepDate))
Group By AreaCode) e
Where a.AreaCode=b.AreaCode And c.AreaCode=a.AreaCode And a.AreaCode=d.AreaCode And a.AreaCode=e.AreaCode
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
msd88
MS-SQL Server 34,877 成员 254,635 内容