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

一段复杂的逻辑,原先的代码我使用#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

 

 

### Flink 大数据处理优化技巧与最佳实践 #### 调优原则与方法概述 对于Flink SQL作业中的大状态导致的反压问题,调优的核心在于减少状态大小以及提高状态访问效率。通过合理配置参数和调整逻辑设计可以有效缓解此类瓶颈[^1]。 #### 参数设置建议 针对不同版本下的具体特性差异,在实施任何性能改进措施前应当充分理解当前使用的Flink版本特点及其局限性;同时也要考虑特定应用场景的需求特征来定制化解决方案。这包括但不限于并行度设定、内存分配策略等方面的选择[^2]。 #### 数据流模式优化 采用广播变量机制可作为一种有效的手段用于降低主数据流转过程中所需维护的状态量级。当存在一对多关系的数据集间需频繁交互时,将较小规模的一方作为广播状态保存下来供另一方查询匹配使用不失为明智之举。此方式特别适用于维表Join操作中,其中一方变动相对较少但又必须保持最新记录的情况[^3]。 ```sql -- 创建临时视图以支持后续JOIN操作 CREATE TEMPORARY VIEW dim_table AS SELECT * FROM kafka_source; -- 定义Temporal Table Function以便获取指定时间点上的历史快照 CREATE FUNCTION hist_dim_table AS 'com.example.HistoricalDimTableFunction'; -- 执行带有时态条件约束的JOIN语句 SELECT o.order_id, d.product_name FROM orders o LEFT JOIN LATERAL TABLE(hist_dim_table(o.event_time)) AS d ON o.product_id = d.id; ``` 上述代码片段展示了如何利用Flink SQL实现基于时间戳的历史维度表连接功能,从而确保每次都能准确捕捉到事件发生瞬间对应的最恰当的产品名称信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值