日期:2014-05-16 浏览次数:20346 次
我们知道索引已经对数据进行了排序,所以这时走索引效率是最高的,下面看一个例子
--在一个有将近两百万数据的表中创建日期索引,需要同时取出日期的最大值和最小值:
SQL> select COUNT(*) from USER_OBJECTS_TMP t; COUNT(*) ---------- 1933312 CREATE INDEX user_objects_create_dt ON user_objects_tmp(created) TABLESPACE tbs_lubinsu_idx; SQL> desc user_objects_tmp Name Type Nullable Default Comments -------------- ------------- -------- ------- -------- OBJECT_NAME VARCHAR2(128) Y SUBOBJECT_NAME VARCHAR2(30) Y OBJECT_ID NUMBER Y DATA_OBJECT_ID NUMBER Y OBJECT_TYPE VARCHAR2(19) Y CREATED DATE Y LAST_DDL_TIME DATE Y TIMESTAMP VARCHAR2(19) Y STATUS VARCHAR2(7) Y TEMPORARY VARCHAR2(1) Y GENERATED VARCHAR2(1) Y SECONDARY VARCHAR2(1) Y ;
SQL> set timing on SQL> set autotrace traceonly SQL> set linesize 200 SQL> SELECT MIN(created), MAX(created) AS MIN FROM user_objects_tmp; Elapsed: 00:00:00.54 Execution Plan ---------------------- Plan hash value: 3066201300 --------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 9 | 1718 (4)| 00:00:21 | | 1 | SORT AGGREGATE | | 1 | 9 | | | | 2 | TABLE ACCESS FULL| USER_OBJECTS_TMP | 1600K| 13M| 1718 (4)| 00:00:21 | --------------------------------------------------- Note ----- - dynamic sampling used for this statement Statistics ---------------------- 5 recursive calls 0 db block gets 8639 consistent gets 659 physical reads 0 redo size 481 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
SQL> SELECT MIN(created), MAX(created) AS MIN FROM user_objects_tmp a WHERE a.created IS NOT NULL; Elapsed: 00:00:00.62 Execution Plan ---------------------- Plan hash value: 3784617757 ------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 9 | 1661 (1)| 00:00:20 | | 1 | SORT AGGREGATE | | 1 | 9 | | | |* 2 | INDEX FAST FULL SCAN| USER_OBJECTS_CREATE_DT | 1600K| 13M| 1661 (1)| 00:00:20 | ------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("A"."CREATED" IS NO