postgresql - 时间戳列上的 date_trunc 不返回任何内容

标签 postgresql postgresql-9.1

在将截断字段与 date_trunc() 进行比较后从数据库检索记录时,我遇到了一个奇怪的问题.

此查询不返回任何数据:

select id from my_db_log 
 where date_trunc('day',creation_date) >= to_date('2014-03-05'::text,'yyyy-mm-dd');

但是如果我添加列 creation_dateid然后它返回数据(即 select id, creation_date... )。

我还有另一栏last_update_date具有相同的类型,当我使用该类型时,仍然会执行相同的行为。

select id from my_db_log
 where date_trunc('day',last_update_date) >= to_date('2014-03-05'::text,'yyyy-mm-dd');

与上一篇类似。如果我这样做,它也会返回记录 id, last_update_date在我的select .

现在为了进一步挖掘,我添加了 creation_datelast_updated_date在我的where条款,这次它要求将它们都放在我的 select 中子句有记录(即 select id, creation_date, last_update_date )。

有人遇到过同样的问题吗?这个类似的东西适用于我的其他具有这种类型列的表!

如果有帮助,这是我的表架构:

id serial NOT NULL,
creation_date timestamp without time zone NOT NULL DEFAULT now(),
last_update_date timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT db_log_pkey PRIMARY KEY (id),

我之前问过一个不同的问题,但没有得到任何答案。这个问题可能与那个问题有关。如果您对此感兴趣,请参阅 the link .

编辑:: EXPLAIN (FORMAT XML)select *返回:

<explain xmlns="http://www.postgresql.org/2009/explain">
  <Query>
    <Plan>
      <Node-Type>Result</Node-Type>
      <Startup-Cost>0.00</Startup-Cost>
      <Total-Cost>0.00</Total-Cost>
      <Plan-Rows>1000</Plan-Rows>
      <Plan-Width>658</Plan-Width>
      <Plans>
        <Plan>
          <Node-Type>Result</Node-Type>
          <Parent-Relationship>Outer</Parent-Relationship>
          <Alias>my_db_log</Alias>
          <Startup-Cost>0.00</Startup-Cost>
          <Total-Cost>0.00</Total-Cost>
          <Plan-Rows>1000</Plan-Rows>
          <Plan-Width>658</Plan-Width>
          <Node/s>datanode1</Node/s>
          <Coordinator-quals>(date_trunc('day'::text, creation_date) &gt;= to_date('2014-03-05'::text, 'yyyy-mm-dd'::text))</Coordinator-quals>
        </Plan>
      </Plans>
    </Plan>
  </Query>
</explain>

最佳答案

“不可能”的现象

返回的行数完全独立于 SELECT 中的项目条款。 (但是请参阅 @Craig 关于 SRF 的评论。)您的数据库中一定有损坏的东西。
也许覆盖索引损坏了?当您添加附加列时,您会强制 Postgres 访问表本身。尝试重新索引:

REINDEX TABLE my_db_log;

REINDEX 上的手册。或者:

VACUUM FULL ANALYZE my_db_log;

更好的查询

无论哪种方式,请改用:

select id from my_db_log 
where creation_date >= '2014-03-05'::date

或者:

select id from my_db_log 
where creation_date >= '2014-03-05 00:00'::timestamp

'2014-03-05'采用 ISO 8601 格式。您可以将此字符串文字转换为 date 。不需要to_date() ,适用于任何语言环境。 date被强制为 timestamp [without time zone]creation_date相比时自动(是 timestamp [without time zone] )。有关 Postgres 中时间戳的更多详细信息,请参见此处:
Ignoring timezones altogether in Rails and PostgreSQL

此外,输入 date_trunc() 也不会带来任何好处。这里。相反,您的查询会变慢,并且无法使用列上的任何普通索引(可能会导致速度慢很多)

关于postgresql - 时间戳列上的 date_trunc 不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22281571/

相关文章:

sql - 检查 Postgres 数组中是否存在 NULL

sql - PostgreSQL:将 SQL 查询简化为更短的查询

linux - 通过 pgAdmin III 的 PostgreSQL - 服务器不监听

django - 如何在 Dockerized Django 中运行迁移?

sql - 如何在 postgresql 中查找具有特定列的表

java - Postgres 和 GWT 的主要提供商

javascript - PostgreSQL void 函数的 pg-promise 方法

java - 使用 JDBC 在 PostgreSQL 中插入点(几何)值

java - StreamedContent : Download is complete, 但文件为空

postgresql - SQL : How to merge two tables into one