oracle 日期时间字段索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2852655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 02:34:40  来源:igfitidea点击:

oracle datetime field indexing

oracleindexing

提问by Milee

How do we perform indexing on a datetime field in oracle. We should be able to search for a specific year

我们如何对 oracle 中的日期时间字段执行索引。我们应该能够搜索特定的年份

Thanks

谢谢

采纳答案by OMG Ponies

To create an index in Oracle, use:

要在 Oracle 中创建索引,请使用:

CREATE INDEX your_index_name ON your_table_name(your_column_name)

For more info about Oracle index creation, read this link.

有关 Oracle 索引创建的更多信息,请阅读此链接

Correction & Clarification
If you use a function to isolate a component of a date (IE: EXTRACT, or TRUNC), an index on the column will not help. But an index will help if you provide a date range:

更正和澄清
如果您使用函数来隔离日期的组成部分(即:EXTRACTTRUNC),则列上的索引将无济于事。但是,如果您提供日期范围,索引会有所帮助:

WHERE your_date_column BETWEEN TO_DATE('2010-01-01', 'YYYY-MM-DD')
                                            AND TO_DATE('2010-12-31', 'YYYY-MM-DD')

You can however create function based indexes in Oracle:

但是,您可以在 Oracle 中创建基于函数的索引:

CREATE INDEX your_index_name 
    ON your_table_name(EXTRACT(YEAR FROM your_column_name))

...which DBAs loath with a passion.

... DBA 非常讨厌。

回答by Vincent Malgrat

You can index a DATE column (which stores date and time in Oracle) directly:

您可以直接索引 DATE 列(在 Oracle 中存储日期和时间):

CREATE INDEX ix ON table (column)

Oracle will be able to use this index directly if you build your query so as to perform a RANGE SCAN. For example, if you want to retrieve rows from 2010:

如果您构建查询以执行 RANGE SCAN,Oracle 将能够直接使用此索引。例如,如果要检索 2010 年的行:

SELECT ...
  FROM table
 WHERE column >= DATE '2010-01-01'
   AND column < DATE '2011-01-01'

This index can also be used to answer queries for a specific month, day or any other range.

此索引还可用于回答特定月份、日期或任何其他范围的查询。

回答by Nolesh

Just create index like shown above. DO NOT USETRUNCFUNCTION, because it ignores any indexes. For instance, my datecreatefield has next format 03.12.2009 16:55:52So I used to use

只需创建如上所示的索引。不要使用TRUNCFUNCTION,因为它会忽略任何索引。例如,我的datecreate字段有下一个格式03.12.2009 16:55:52所以我曾经使用

trunc(datecreate, 'dd')=to_date(to_char(sysdate,'dd.mm.yyyy'),'dd.mm.yyyy')

and it worked very slowly(about 5 sec)!!! Now I use next expression:

它的工作非常缓慢(大约 5 秒)!!!现在我使用下一个表达式:

datecreate>=to_date(to_char(sysdate,'dd.mm.yyyy'),'dd.mm.yyyy') and sw.datecreate<to_date(to_char(sysdate+1,'dd.mm.yyyy'),'dd.mm.yyyy')

and my query executes in 0,01 sec

我的查询在 0,01 sec

回答by Robert Giesecke

Add an index which is not bound to a column, but an expression that extract the year from that column:

添加一个未绑定到列的索引,而是一个从该列中提取年份的表达式:

create index sample_index on YourTable (extract(year from YourDateColumn)) tablesapce YourIndexSpace;

When you query the table using that expression, Oracle will use the index.

当您使用该表达式查询表时,Oracle 将使用该索引。