从PostgreSQL中的时间戳中提取日期的最有效方法是什么?
发布时间:2020-12-13 16:08:56 所属栏目:百科 来源:网络整理
导读:查询表格 select * from foowhere created_on::date = '2014/1/1' 要么 select * from foowhere date_trunc('day',created_on) = '2014/1/1' 要么 select * from foowhere date(created_on) = '2014/1/1' 在什么条件下,不同的查询会表现得更好/更差?哪三个
查询表格
select * from foo where created_on::date = '2014/1/1' 要么 select * from foo where date_trunc('day',created_on) = '2014/1/1' 要么 select * from foo where date(created_on) = '2014/1/1' 在什么条件下,不同的查询会表现得更好/更差?哪三个选项中效率最高? 解决方法
总结评论,您的第一个和第三个解决方案是相同的.根据@Nick Barnes,转换为日期只使用日期函数.
这些选项加上选项2,需要对表的每一行运行一个函数,所以即使你有一个索引,也不能使用它. 假设created_on上有一个索引,这是你最好的选择: select * from foo where created_on >= '2014/1/1 00:00:00' and created_on < '2014/1/2 00:00:00'; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |