使用 to_char(timestamp,'YYYY-MM-DD')) 函数进行索引

indexing using to_char(timestamp,'YYYY-MM-DD')) function

我的 SQL where 子句中包含以下内容。这是针对 Oracle 数据库的 运行。时间戳字段在数据库中定义为时间戳字段。 我的 select 查询是:

create index ind_timestamp on sms_in(to_char(timestamp,'YYYY-MM-DD'));

select * from sms_in where to_char(timestamp,'YYYY-MM-DD')in '2018-08-01';

但是当我尝试执行查询时,数据库访问权限已满。这意味着索引不起作用。

如果您一直在寻找特定日期的数据,那么我的建议如下

您在 timestamp 列上添加了一个正常索引

create index ind_timestamp on sms_in(timestamp);

你的select可能看起来像

select * 
  from sms_in 
 where timestamp between to_date('2018-08-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') 
                     and to_date('2018-08-01 23:59:59','YYYY-MM-DD HH24:MI:SS');

或者你也可以这样做

select * 
  from sms_in 
 where timestamp >= to_date('2018-08-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
  and timestamp  <= to_date('2018-08-01 23:59:59','YYYY-MM-DD HH24:MI:SS');

编辑:

这个答案基本正确,但是逻辑应该是:

select * 
 from sms_in 
 where timestamp >= date '2018-08-01' and
       timestamp < date '2018-08-02 ;