查询条件左边写入函数,导致无法命中索引

本文分享了一个关于SQL查询优化的真实案例,针对系统查询速度慢的问题,通过调整查询条件和时间范围的表述方式,成功提升了查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:

简单罗列下之前做过的优化,人懒。

所以没能把所有的整理统一放出来。

情况:

系统查询过慢,没能命中索引。

<!--对账单数量 -->
<select id="selectCount" resultType="java.lang.Integer" parameterType="map">
    select count(*) from monthly_statements where store_id = #{storeId}
    <if test="billNo != null">
        and bill_no = #{billNo}
    </if>
    <if test="beginTime != null and endTime != null">
        and date_format(created_at,"%Y-%m-%d") between #{beginTime,jdbcType=VARCHAR}
        and #{endTime,jdbcType=VARCHAR}
    </if>
    <if test="isDeadline == 0">   <!-- 未到账期时间 -->
        and expiration_time  &gt;  now()
    </if>
    <if test="isDeadline == 1">   <!-- 已到账期时间 -->
        and expiration_time  &lt;=  now()
    </if>
</select>

 

案例:

  • 优化前
select * from cc where date_format(dd,'%Y-%m-%d')="2016-02-22";

  • 优化后
select * from orders where `created_at` > '2016-02-22' and `created_at` < date_add('2016-02-22',interval 1 day);

转载于:https://www.cnblogs.com/snifferhu/p/5730933.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值