一条超长的select语句

根据用户需求,憋了2个多小时憋出这么一条sql语句,涉及数据70多万行
创建索引前,放在存储过程里还需要执行40多秒
创建了14个索引后,执行时间缩短到20几秒...困惑
 
select 总数量表.产业名称
,总数量表.大陆总数量
,总数量表.港澳台总数量
,总数量表.国外总数量
,增加数量表.大陆增加数量
,增加数量表.港澳台增加数量
,增加数量表.国外增加数量
,删除数量表.大陆删除数量
,删除数量表.港澳台删除数量
,删除数量表.国外删除数量
from
(select a.IndustryName as 产业名称,a.大陆总数量,b.港澳台总数量,c.国外总数量
from
(select count(distinct Tab_BuyerRegForm.BuyerRegFormID) as 大陆总数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm
left join dbo.Tab_Country
on Tab_BuyerRegForm.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct
on Tab_BuyerRegForm.BuyerRegFormID=Tab_BuyerRegProduct.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where Tab_BuyerRegForm.CountryEng ='中国'
and Tab_BuyerRegForm.Province
not in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
--and (Tab_BuyerRegForm.CreateDate>=@StartDate or @StartDate is null)
--and (Tab_BuyerRegForm.CreateDate<=@EndDate or @EndDate is null)
group by Tab_Industry.IndustryName) a
left join
--港澳台的
(select count(distinct Tab_BuyerRegForm.BuyerRegFormID) as 港澳台总数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm
left join dbo.Tab_Country
on Tab_BuyerRegForm.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct
on Tab_BuyerRegForm.BuyerRegFormID=Tab_BuyerRegProduct.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where  Tab_BuyerRegForm.Province
in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
--and (Tab_BuyerRegForm.CreateDate>=@StartDate or @StartDate is null)
--and (Tab_BuyerRegForm.CreateDate<=@EndDate or @EndDate is null)
group by Tab_Industry.IndustryName) b
on a.IndustryName= b.IndustryName
left join
--国外的
(select count(distinct Tab_BuyerRegForm.BuyerRegFormID) as 国外总数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm
left join dbo.Tab_Country
on Tab_BuyerRegForm.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct
on Tab_BuyerRegForm.BuyerRegFormID=Tab_BuyerRegProduct.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where Tab_BuyerRegForm.CountryEng <>'中国'
and Tab_BuyerRegForm.Province
not in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
--and (Tab_BuyerRegForm.CreateDate>=@StartDate or @StartDate is null)
--and (Tab_BuyerRegForm.CreateDate<=@EndDate or @EndDate is null)
group by Tab_Industry.IndustryName) c
on a.IndustryName= c.IndustryName) 总数量表
left join
(
select a.IndustryName as 产业名称,a.大陆增加数量,b.港澳台增加数量,c.国外增加数量
from
(select count(distinct Tab_BuyerRegForm.BuyerRegFormID) as 大陆增加数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm
left join dbo.Tab_Country
on Tab_BuyerRegForm.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct
on Tab_BuyerRegForm.BuyerRegFormID=Tab_BuyerRegProduct.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where Tab_BuyerRegForm.CountryEng ='中国'
and Tab_BuyerRegForm.Province
not in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
and (Tab_BuyerRegForm.CreateDate>=@StartDate )
and (Tab_BuyerRegForm.CreateDate<=@EndDate)
group by Tab_Industry.IndustryName) a
left join
--港澳台的
(select count(distinct Tab_BuyerRegForm.BuyerRegFormID) as 港澳台增加数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm
left join dbo.Tab_Country
on Tab_BuyerRegForm.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct
on Tab_BuyerRegForm.BuyerRegFormID=Tab_BuyerRegProduct.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where  Tab_BuyerRegForm.Province
in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
and (Tab_BuyerRegForm.CreateDate>=@StartDate )
and (Tab_BuyerRegForm.CreateDate<=@EndDate)
group by Tab_Industry.IndustryName) b
on a.IndustryName= b.IndustryName
left join
--国外的
(select count(distinct Tab_BuyerRegForm.BuyerRegFormID) as 国外增加数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm
left join dbo.Tab_Country
on Tab_BuyerRegForm.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct
on Tab_BuyerRegForm.BuyerRegFormID=Tab_BuyerRegProduct.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where Tab_BuyerRegForm.CountryEng <>'中国'
and Tab_BuyerRegForm.Province
not in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
and (Tab_BuyerRegForm.CreateDate>=@StartDate )
and (Tab_BuyerRegForm.CreateDate<=@EndDate )
group by Tab_Industry.IndustryName) c
on a.IndustryName= c.IndustryName) 增加数量表
on 总数量表.产业名称=增加数量表.产业名称
left join
(select a.IndustryName as 产业名称,a.大陆删除数量,b.港澳台删除数量,c.国外删除数量
from
(select count(distinct Tab_BuyerRegForm_Deleted.BuyerRegFormID) as 大陆删除数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm_Deleted
left join dbo.Tab_Country
on Tab_BuyerRegForm_Deleted.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct_Deleted
on Tab_BuyerRegForm_Deleted.BuyerRegFormID=Tab_BuyerRegProduct_Deleted.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct_Deleted.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where Tab_BuyerRegForm_Deleted.CountryEng ='中国'
and Tab_BuyerRegForm_Deleted.Province
not in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
and (Tab_BuyerRegForm_Deleted.DeleteDate>=@StartDate )
and (Tab_BuyerRegForm_Deleted.DeleteDate<=@EndDate)
group by Tab_Industry.IndustryName) a
left join
--港澳台的
(select count(distinct Tab_BuyerRegForm_Deleted.BuyerRegFormID) as 港澳台删除数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm_Deleted
left join dbo.Tab_Country
on Tab_BuyerRegForm_Deleted.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct_Deleted
on Tab_BuyerRegForm_Deleted.BuyerRegFormID=Tab_BuyerRegProduct_Deleted.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct_Deleted.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where  Tab_BuyerRegForm_Deleted.Province
in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
and (Tab_BuyerRegForm_Deleted.DeleteDate>=@StartDate )
and (Tab_BuyerRegForm_Deleted.DeleteDate<=@EndDate)
group by Tab_Industry.IndustryName) b
on a.IndustryName= b.IndustryName
left join
--国外的
(select count(distinct Tab_BuyerRegForm_Deleted.BuyerRegFormID) as 国外删除数量
,Tab_Industry.IndustryName
from Tab_BuyerRegForm_Deleted
left join dbo.Tab_Country
on Tab_BuyerRegForm_Deleted.CountryEng =Tab_Country.CountryEng
left join dbo.Tab_BuyerRegProduct_Deleted
on Tab_BuyerRegForm_Deleted.BuyerRegFormID=Tab_BuyerRegProduct_Deleted.BuyerRegFormID
left join dbo.Tab_IndustryProduct
on Tab_BuyerRegProduct_Deleted.ProductID like Tab_IndustryProduct.ProductID +'%'
left join Tab_Industry
on Tab_IndustryProduct.IndustryID=Tab_Industry.IndustryID
where Tab_BuyerRegForm_Deleted.CountryEng <>'中国'
and Tab_BuyerRegForm_Deleted.Province
not in (select ProvinceName from dbo.Tab_RegionInChinaProvince
where RegionInChinaID=8) --8为港澳台
and Tab_IndustryProduct.IndustryID in (1,2,3,5,6) --服装,面料,辅料,礼品,建材
and (Tab_BuyerRegForm_Deleted.DeleteDate>=@StartDate )
and (Tab_BuyerRegForm_Deleted.DeleteDate<=@EndDate )
group by Tab_Industry.IndustryName) c
on a.IndustryName= c.IndustryName) 删除数量表
on 总数量表.产业名称=删除数量表.产业名称
order by 产业名称
原文地址:https://www.cnblogs.com/zhlei616/p/1641507.html