sql 优化-查询条件中有DATE_FORMAT的

查询sql where 后的条件中有时间比较的如果使用了DATE_FORMAT这种函数的话就不会走索引查询就会慢,优化方式:去掉了DATE_FORMAT函数,程序传过来的字符串“2017-07-01 14:02:19”可以直接和数据库中的时间字段值进行比较

优化前:


and TRADE_NO = #{tradeNo}
and BATCH_ID = #{batchId}
and DATE_FORMAT(TRADE_TIME,'%Y-%m-%d %H:%i:%s') >= DATE_FORMAT(#{tradeStartTime},'%Y-%m-%d %H:%i:%s')
and DATE_FORMAT(TRADE_TIME,'%Y-%m-%d %H:%i:%s') <= DATE_FORMAT(#{tradeEndTime},'%Y-%m-%d %H:%i:%s')
and CHANNEL_CODE = #{channelCode}
and CHANNEL_NO = #{channelNo}
and EXT_NO=#{extNo}
and FEE_STATUS=#{feeStatus}
and DATE_FORMAT(CHARGING_TIME,'%Y-%m-%d %H:%i:%s') >= DATE_FORMAT(#{chargeStartTime},'%Y-%m-%d %H:%i:%s')
and DATE_FORMAT(CHARGING_TIME,'%Y-%m-%d %H:%i:%s') <= DATE_FORMAT(#{chargeEndTime},'%Y-%m-%d %H:%i:%s')
and FEE_RULE=#{feeRule}
and FEE_RULE_NAME = #{feeRuleName}
and DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H:%i:%s') >= DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i:%s')
and DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H:%i:%s') <= DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i:%s')
and FEE_RULE=#{feeRule}

优化后:


and TRADE_NO = #{tradeNo}
and BATCH_ID = #{batchId}
and TRADE_TIME >= #{tradeStartTime}
and TRADE_TIME <= #{tradeEndTime}
and CHANNEL_CODE = #{channelCode}
and CHANNEL_NO = #{channelNo}
and EXT_NO=#{extNo}
and FEE_STATUS=#{feeStatus}
and CHARGING_TIME >= #{chargeStartTime}
and CHARGING_TIME <= #{chargeEndTime}
and FEE_RULE=#{feeRule}
and FEE_RULE_NAME = #{feeRuleName}
and CREATE_TIME >= #{startTime}
and CREATE_TIME <= #{endTime}
and FEE_RULE=#{feeRule}

转载于:https://www.cnblogs.com/sailormoon/p/7239472.html

你可能感兴趣的:(sql 优化-查询条件中有DATE_FORMAT的)