对于sql中使用to_timestamp判断时间区间和不使用的效率对比及结论
关于日期函数TO_TIMESTAMP
贴上阿里云文档:https://helpcdn.aliyun.com/knowledge_detail/64813.html
拓展:
date类型是Oracle常用的日期型变量,时间间隔是秒。两个日期型相减得到是两个时间的间隔,注意单位是“天”。
timestamp是DATE类型的扩展,可以精确到小数秒(fractional_seconds_precision),可以是 0to9,缺省是6。两个timestamp相减的话,不能直接的得到天数,而是得到,多少天,多少小时,多少秒等。
使用TO_TIMESTAMP函数:
select
distinct hiui.salesman_id as salesmanId,
hui.user_name as salesmanName,
hui.user_phone as salesmanPhone,
hiui.class_id as classId,
hiui.department_id as departmentId,
get_affiliaction_name(hiui.class_id, hiui.department_id) as affiliactionName
from hz_invited_user_info as hiui
inner join hz_user_info as hui on hui.user_id = hiui.salesman_id
where
to_timestamp('2018-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss') <= hiui.create_time
and to_timestamp('2019-05-28 00:00:00','yyyy-mm-dd hh24:mi:ss') >= hiui.create_time
时间: 0.949s 时间: 0.973s 时间: 0.783s
不使用:
select
distinct hiui.salesman_id as salesmanId,
hui.user_name as salesmanName,
hui.user_phone as salesmanPhone,
hiui.class_id as classId,
hiui.department_id as departmentId,
get_affiliaction_name(hiui.class_id, hiui.department_id) as affiliactionName
from hz_invited_user_info as hiui
inner join hz_user_info as hui on hui.user_id = hiui.salesman_id
where
'2018-01-01 00:00:00' <= hiui.create_time
and '2019-05-26 00:00:00' >= hiui.create_time
时间: 0.79s 时间: 0.743s 时间: 0.747s
两者分别执行了3次,发现不使用效率更高,因为在sql执行的时候,会自动把时间字符串转换成时间戳。但是使用效率也区别不是很明显,可能是小数据的原因。