SQL技巧(三) - CTE实战之代替临时表

简介: 一段复杂的逻辑,原先的代码我使用#tmp临时表来实现,性能是不好的,而且要考虑到多用户时的锁的问题代码如下:declare @StartDate datetimedeclare @EndDate datetimeselect @StartDate='2012-09-28'select...

一段复杂的逻辑,原先的代码我使用#tmp临时表来实现,性能是不好的,而且要考虑到多用户时的锁的问题

代码如下:

declare  @StartDate  datetime
declare  @EndDate  datetime
select  @StartDate = ' 2012-09-28 '
select  @EndDate = ' 2012-10-03 '

if  exists ( select  *  from tempdb..sysobjects  where name  like  ' #tmpPolicyId% ')
     drop  table #tmpPolicyId
if  exists ( select  *  from tempdb..sysobjects  where name  like  ' #tmpSeasonFee% ')
     drop  table #tmpSeasonFee
if  exists ( select  *  from tempdb..sysobjects  where name  like  ' #tmpSpecialFee% ')
     drop  table #tmpSpecialFee
 
select PolicyId
into #tmpPolicyId
from GvInterPolicy
group  by PolicyId

select tmp.PolicyId,  max(AddFee)  as SeasonFee
into #tmpSeasonFee
from #tmpPolicyId tmp  inner  join GvSeasonFee season
     on tmp.PolicyId =season.PolicyId
where season.StartDate <= @StartDate  and season.EndDate >= @StartDate 
group  by tmp.PolicyId

select tmp.PolicyId,  max(AddFee)  as SpecialFee
into #tmpSpecialFee
from #tmpPolicyId tmp  inner  join GvSpecialFee special
     on tmp.PolicyId =special.PolicyId
where special.SpecialDate = @StartDate
group  by tmp.PolicyId


select p.PolicyId, p.AirComCode, p.DepStartTime, p.DepEndTime, p.SellStartTime, p.SellEndTime,
        p.StartCityCode, p.EndCityCode, r.FlightNo, r.SCityCode, r.ECityCode, r.STime, r.ETime,
        r.TerminalBuilding, r.RouteOrder, r.SCityName, r.ECityName, r.PlaneModel, r.IsBack,
        c.FirstCabin, c.SecondCabin, c.Price  as AdultPrice, 
         isnull(season.SeasonFee,  0as SeaFee,  isnull(special.SpecialFee,  0as SpFee,
        AddPrice = case  when  isnull(season.SeasonFee,  0) >= isnull(special.SpecialFee,  0then  isnull(season.SeasonFee,  0else  isnull(special.SpecialFee,  0end
from GvInterPolicy p  inner  join GvRouteInfo r
     on p.PolicyId =r.PolicyId  inner  join GvCabinInfo c
     on p.PolicyId =c.PolicyId  left  join #tmpSeasonFee season
     on p.PolicyId =season.PolicyId  left  join #tmpSpecialFee special
     on p.PolicyId =special.PolicyId 
where p.DepStartTime <= @StartDate  and p.DepEndTime >= @EndDate
order  by p.PolicyId, c.FirstCabin, c.SecondCabin, r.RouteOrder

 

使用CTE改进后的代码:

declare  @StartDate  datetime
declare  @EndDate  datetime
select  @StartDate = ' 2012-09-28 '
select  @EndDate = ' 2012-10-03 ';

with ctePolicyId(PolicyId)
as
(
     select PolicyId
     from GvInterPolicy
     group  by PolicyId
),
cteSeasonFee
as
(
     select p.PolicyId,  max(AddFee)  as SeasonFee
     from ctePolicyId p  inner  join GvSeasonFee season
         on p.PolicyId =season.PolicyId
     where season.StartDate <= @StartDate  and season.EndDate >= @StartDate 
     group  by p.PolicyId
),
cteSpecialFee
as
(
     select p.PolicyId,  max(AddFee)  as SpecialFee
     from ctePolicyId p  inner  join GvSpecialFee special
         on p.PolicyId =special.PolicyId
     where special.SpecialDate = @StartDate
     group  by p.PolicyId

)

select p.PolicyId, p.AirComCode, p.DepStartTime, p.DepEndTime, p.SellStartTime, p.SellEndTime,
        p.StartCityCode, p.EndCityCode, r.FlightNo, r.SCityCode, r.ECityCode, r.STime, r.ETime,
        r.TerminalBuilding, r.RouteOrder, r.SCityName, r.ECityName, r.PlaneModel, r.IsBack,
        c.FirstCabin, c.SecondCabin, c.Price  as AdultPrice, 
         isnull(season.SeasonFee,  0as SeaFee,  isnull(special.SpecialFee,  0as SpFee,
        AddPrice = case  when  isnull(season.SeasonFee,  0) >= isnull(special.SpecialFee,  0then  isnull(season.SeasonFee,  0else  isnull(special.SpecialFee,  0end
from GvInterPolicy p  inner  join GvRouteInfo r
     on p.PolicyId =r.PolicyId  inner  join GvCabinInfo c
     on p.PolicyId =c.PolicyId  left  join cteSeasonFee season
     on p.PolicyId =season.PolicyId  left  join cteSpecialFee special
     on p.PolicyId =special.PolicyId 
where p.DepStartTime <= @StartDate  and p.DepEndTime >= @EndDate
order  by p.PolicyId, c.FirstCabin, c.SecondCabin, r.RouteOrder

 

 

目录
相关文章
|
5月前
|
SQL
SQL如何在CTE中使用Order By的功能
SQL Server如何在CTE中使用Order By的功能
|
9月前
|
SQL 运维 监控
SQL查询太慢?实战讲解YashanDB SQL调优思路
本文是Meetup第十期“调优实战专场”的第二篇技术文章,上一篇《高效查询秘诀,解码YashanDB优化器分组查询优化手段》中,我们揭秘了YashanDB分组查询优化秘诀,本文将通过一个案例,助你快速上手YashanDB慢日志功能,精准定位“慢SQL”后进行优化。
|
6月前
|
SQL 关系型数据库 PostgreSQL
CTE vs 子查询:深入拆解PostgreSQL复杂SQL的隐藏性能差异
本文深入探讨了PostgreSQL中CTE(公共表表达式)与子查询的选择对SQL性能的影响。通过分析两者底层机制,揭示CTE的物化特性及子查询的优化融合优势,并结合多场景案例对比执行效率。最终给出决策指南,帮助开发者根据数据量、引用次数和复杂度选择最优方案,同时提供高级优化技巧和版本演进建议,助力SQL性能调优。
574 1
|
SQL 数据库 UED
SQL性能提升秘籍:5步优化法与10个实战案例
在数据库管理和应用开发中,SQL查询的性能优化至关重要。高效的SQL查询不仅可以提高应用的响应速度,还能降低服务器负载,提升用户体验。本文将分享SQL优化的五大步骤和十个实战案例,帮助构建高效、稳定的数据库应用。
1024 3
|
SQL 关系型数据库 MySQL
sql注入原理与实战(三)数据库操作
sql注入原理与实战(三)数据库操作
sql注入原理与实战(三)数据库操作
|
SQL 缓存 监控
SQL性能提升指南:五大优化策略与十个实战案例
在数据库性能优化的世界里,SQL优化是提升查询效率的关键。一个高效的SQL查询可以显著减少数据库的负载,提高应用响应速度,甚至影响整个系统的稳定性和扩展性。本文将介绍SQL优化的五大步骤,并结合十个实战案例,为你提供一份详尽的性能提升指南。
1177 0
|
SQL 存储 数据处理
"SQL触发器实战大揭秘:一键解锁数据自动化校验与更新魔法,让数据库管理从此告别繁琐,精准高效不再是梦!"
【8月更文挑战第31天】在数据库管理中,确保数据准确性和一致性至关重要。SQL触发器能自动执行数据校验与更新,显著提升工作效率。本文通过一个员工信息表的例子,详细介绍了如何利用触发器自动设定和校验薪资,确保其符合业务规则。提供的示例代码展示了在插入新记录时如何自动检查并调整薪资,以满足最低标准。这不仅减轻了数据库管理员的负担,还提高了数据处理的准确性和效率。触发器虽强大,但也需谨慎使用,以避免复杂性和性能问题。
238 1
|
SQL 数据处理 数据库
SQL语句优化与查询结果优化:提升数据库性能的实战技巧
在数据库管理和应用中,SQL语句的编写和查询结果的优化是提升数据库性能的关键环节
1149 0
|
SQL 监控 关系型数据库
SQL语句性能分析:实战技巧与详细方法
在数据库管理中,分析SQL语句的性能是优化数据库查询、提升系统响应速度的重要步骤
1185 0
|
SQL 关系型数据库 Serverless
sql注入原理与实战(四)数据表操作
sql注入原理与实战(四)数据表操作