简体   繁体   English

从PostgreSQL中的时间戳中提取日期的最有效方法是什么?

[英]What is the most efficient way to extract a date from a timestamp in PostgreSQL?

Having queries of the forms 查询表格

select * from foo
where created_on::date = '2014/1/1'

or 要么

select * from foo
where date_trunc('day', created_on) = '2014/1/1'

or 要么

select * from foo
where date(created_on) = '2014/1/1'

Under what conditions would the different queries perform better/worse? 在什么条件下,不同的查询会表现得更好/更差? Which is the most efficient of the three options? 哪三个选项中效率最高?

To summarize the comments, your first and third solution are identical. 总结评论,您的第一个和第三个解决方案是相同的。 Casting to a date simply uses the date function according to @Nick Barnes. 根据@Nick Barnes,转换为日期只使用date函数。

Those options, plus option 2, requires a function to be run against every row of the table, so even if you have an index, it cannot be used. 这些选项加上选项2,需要对表的每一行运行一个函数,所以即使你有一个索引,也不能使用它。

Assuming there is an index on created_on , this is your best bet: 假设created_on上有一个索引,这是你最好的选择:

select * from foo
where created_on >= '2014/1/1 00:00:00'
  and created_on < '2014/1/2 00:00:00';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 postgresql 中使用 nanosec 存储时间戳的最优雅方法是什么? - What is the most elegant way to store timestamp with nanosec in postgresql? 在PostgreSQL数据库中定期同步数据的最有效方法是什么? - What's the most efficient way to periodically sync data in a PostgreSQL DB? 使用PostgreSQL从Unix时间戳中提取日期,月份,年份和月份名称 - Extract date,month,year and month name from the unix timestamp with postgresql 从 PostgreSQL 中的时间戳中提取日期 (yyyy/mm/dd) - Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL 在 Postgresql 中比较字符串日期的最有效方法? - Most Efficient Way to Compare String Dates in Postgresql? 缓存从API提取的信息的最有效方法是什么? - What is the most efficient way of caching information pulled from an API? 在给定某些其他标准的情况下,按日期检索单个最新记录的最有效方法是什么 - What is the most efficient way to retrieve the single most recent record by date given certain other criteria 从关系数据库中选择数据的最有效方法是什么? - What is the most efficient way of selecting data from relational database? 从SQL删除最新的X记录的有效方法是什么? - What is a efficient way to delete the most recent X record from SQL? 从 SQL 中的多个表中获取 select 数据的最有效方法是什么? - What is the most efficient way to select data from multiple tables in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM