包裹实时统计sql优化

优化前的sql
 <select id="realTimeStatistics" resultType="com.fpx.acs.pojo.vo.manage.PackageRealTimeStatisticsVo">
        SELECT
        COUNT(
        CASE
        WHEN create_time &gt;= #{statisticalTimeBegin} and create_time &lt; #{statisticalTimeEnd}
        THEN 1
        else null
        END
        ) as forecast_count ,

        COUNT(
        CASE
        WHEN arrived_time &gt;= #{statisticalTimeBegin} and arrived_time &lt; #{statisticalTimeEnd}
        THEN 1
        else null
        END
        ) as in_storage_count ,

        COUNT(
        CASE
        WHEN on_shelves_time &gt;= #{statisticalTimeBegin} and on_shelves_time &lt; #{statisticalTimeEnd}
        THEN 1
        else null
        END
        ) as on_shelves_count ,

        COUNT(
        CASE
        WHEN outbound_time &gt;= #{statisticalTimeBegin} and outbound_time &lt; #{statisticalTimeEnd}
        THEN 1
        else null
        END
        ) as out_storage_count
        FROM acs_package ap
        WHERE warehouse_code = #{warehouseCode}
    </select>

explain结果

在这里插入图片描述

优化后的sql
 select
        A.forecast_count,
        B.in_storage_count,
        C.on_shelves_count,
        D.out_storage_count
        from
        (
        SELECT
        count(1) as forecast_count
        FROM
        acs_package ap
        where
        warehouse_code = #{warehouseCode}
        and create_time >= #{statisticalTimeBegin}
        and create_time &lt;= #{statisticalTimeEnd}
        ) A,
        (
        SELECT
        count(1) as in_storage_count
        FROM
        acs_package ap
        where
        warehouse_code = #{warehouseCode}
        and arrived_time >= #{statisticalTimeBegin}
        and arrived_time &lt;= #{statisticalTimeEnd}
        ) B,
        (
        SELECT
        count(1) as on_shelves_count
        FROM
        acs_package ap
        where
        warehouse_code = #{warehouseCode}
        and on_shelves_time >= #{statisticalTimeBegin}
        and on_shelves_time &lt;= #{statisticalTimeEnd}
        ) C,
        (
        SELECT
        count(1) as out_storage_count
        FROM
        acs_package ap
        where
        warehouse_code = #{warehouseCode}
        and outbound_time >= #{statisticalTimeBegin}
        and outbound_time &lt;= #{statisticalTimeEnd}
        ) D
explain结果

在这里插入图片描述
explain用法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值