| 導購 | 订阅 | 在线投稿
分享
 
 
 

Oracle數據庫中的timestamp和date類型

來源:互聯網  2008-06-01 03:22:22  評論

問題:where timestamp>date 這種子句是走索引嗎?

下面我們針對這個問題做一個試驗:

c:>sqlplus / as sysdba

sys@EOS >create table test as select table_name,to_timestamp(last_analyzed) date_test from dba_tables;

表已創建。

sys@EOS> create index idx_test_date on test (date_test);

索引已創建。

sys@EOS> desc test

名稱 是否爲空? 類型

------------------------- -------- ----------------

TABLE_NAME NOT NULL VARCHAR2(30)

DATE_TEST TIMESTAMP(0)

sys@EOS> select date_test from test where date_test > TO_DATE('2007-11-5 00:00:00','yyyy-MM-dd HH24:mi:ss');

執行計劃

----------------------------------------------------------

Plan hash value: 944171586

-------------------------------------------------------------------------------- --

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

-------------------------------------------------------------------------------- --

| 0 | SELECT STATEMENT | | 1 | 22 | 1 (0)| 00:00:01 |

|* 1 | INDEX RANGE SCAN| IDX_TEST_DATE | 1 | 22 | 1 (0)| 00:00:01 |

-------------------------------------------------------------------------------- --

Predicate Information (identified by operation id):

---------------------------------------------------

1 - access("DATE_TEST">TIMESTAMP'2007-11-05 00:00:00')

Note

-----

- dynamic sampling used for this statement

統計信息

----------------------------------------------------------

7 recursive calls

0 db block gets

18 consistent gets

0 physical reads

0 redo size

280 bytes sent via SQL*Net to client

374 bytes received via SQL*Net from client

1 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

0 rows processed

從上文中大家可以清楚地看到,timestamp>date情況下,走索引。

另外,date類型一般很少用,建議大家在産品裏面所有的date數據類型全部改爲timestamp。

問題:where timestamp>date 這種子句是走索引嗎? 下面我們針對這個問題做一個試驗: c:>sqlplus / as sysdba sys@EOS >create table test as select table_name,to_timestamp(last_analyzed) date_test from dba_tables; 表已創建。 sys@EOS> create index idx_test_date on test (date_test); 索引已創建。 sys@EOS> desc test 名稱 是否爲空? 類型 ------------------------- -------- ---------------- TABLE_NAME NOT NULL VARCHAR2(30) DATE_TEST TIMESTAMP(0) sys@EOS> select date_test from test where date_test > TO_DATE('2007-11-5 00:00:00','yyyy-MM-dd HH24:mi:ss'); 執行計劃 ---------------------------------------------------------- Plan hash value: 944171586 -------------------------------------------------------------------------------- -- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- -- | 0 | SELECT STATEMENT | | 1 | 22 | 1 (0)| 00:00:01 | |* 1 | INDEX RANGE SCAN| IDX_TEST_DATE | 1 | 22 | 1 (0)| 00:00:01 | -------------------------------------------------------------------------------- -- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("DATE_TEST">TIMESTAMP'2007-11-05 00:00:00') Note ----- - dynamic sampling used for this statement 統計信息 ---------------------------------------------------------- 7 recursive calls 0 db block gets 18 consistent gets 0 physical reads 0 redo size 280 bytes sent via SQL*Net to client 374 bytes received via SQL*Net from client 1 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 0 rows processed 從上文中大家可以清楚地看到,timestamp>date情況下,走索引。 另外,date類型一般很少用,建議大家在産品裏面所有的date數據類型全部改爲timestamp。
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有