zoukankan      html  css  js  c++  java
  • 一条超长的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 产业名称
  • 相关阅读:
    CentOs 7.3下ELK日志分析系统搭建
    01 使用Git基本方法
    02 网页换肤
    运行代码
    01 使用JavaScript原生控制div属性
    excel 删除了自动保存的文件,如何恢复
    Eclipse错误: 找不到或无法加载主类或项目无法编译10种解决大法!
    eclipse启动错误
    centos已安装gcc 4.8.2,gcc -version 也是4.8.2 但是被其他工具调用时,显示未调用4.8.2
    centos 6.4安装cmake
  • 原文地址:https://www.cnblogs.com/zhlei616/p/1641507.html
Copyright © 2011-2022 走看看