[英]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.