About me:Oracle ACE pro,optimistic,passionate and harmonious. Focus on ORACLE,MySQL and other database programming,peformance tuning,db design, j2ee,Linux/AIX,Architecture tech,etc
全部博文(169)
分类: Oracle
2022-04-12 10:28:15
DROP TABLE t; CREATE TABLE t AS SELECT LEVEL ID,rpad('x',10,'x') padding,CAST('JACK' AS VARCHAR2(100)) NAME FROM dual CONNECT BY LEVEL<100000; --构造3行较少的值 INSERT INTO t VALUES(100000,'y','DINGJUN1'); INSERT INTO t VALUES(100001,'y','DINGJUN2'); INSERT INTO t VALUES(100002,'y','DINGJUN3'); COMMIT; ALTER TABLE t MODIFY NAME NOT NULL; --创建索引 CREATE INDEX idx_t ON t(NAME); BEGIN dbms_stats.gather_table_stats(ownname => USER,tabname => 'T',estimate_percent => 100,method_opt => 'for columns name size 10',cascade => TRUE); END; / |
dingjun123@ORADB> SELECT * FROM t 2 WHERE t.name<>'JACK'; 3 rows selected. Elapsed: 00:00:00.01 Execution Plan ---------------------------------------------------------- Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4 | 88 | 107 (2)| 00:00:02 | |* 1 | TABLE ACCESS FULL| T | 4 | 88 | 107 (2)| 00:00:02 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("T"."NAME"<>'JACK') |
dingjun123@ORADB> SELECT * FROM t 2 WHERE t.NAME>'JACK' OR t.NAME <'JACK'; 3 rows selected. Elapsed: 00:00:00.06 Execution Plan ---------------------------------------------------------- Plan hash value: 2945726203 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4 | 88 | 6 (0)| 00:00:01 | | 1 | CONCATENATION | | | | | | | 2 | TABLE ACCESS BY INDEX ROWID| T | 1 | 22 | 3 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | IDX_T | 1 | | 2 (0)| 00:00:01 | | 4 | TABLE ACCESS BY INDEX ROWID| T | 3 | 66 | 3 (0)| 00:00:01 | |* 5 | INDEX RANGE SCAN | IDX_T | 4 | | 2 (0)| 00:00:01 | -------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("T"."NAME">'JACK') 5 - access("T"."NAME"<'JACK') filter(LNNVL("T"."NAME">'JACK')) |
DROP TABLE t1; CREATE TABLE t1 AS SELECT 1 ID,'JACK' NAME FROM dual; ALTER TABLE t1 MODIFY NAME NOT NULL; BEGIN dbms_stats.gather_table_stats(ownname => USER,tabname => 'T1',estimate_percent => 100); END; / |
dingjun123@ORADB> SELECT * FROM t 2 WHERE t.NAME NOT IN(SELECT t1.NAME FROM t1); Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 4253491563 ----------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 100K| 2636K| 110 (2)| 00:00:02 | |* 1 | HASH JOIN RIGHT ANTI| | 100K| 2636K| 110 (2)| 00:00:02 | | 2 | TABLE ACCESS FULL | T1 | 1 | 5 | 3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL | T | 100K| 2148K| 106 (1)| 00:00:02 | ----------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T"."NAME"="T1"."NAME") dingjun123@ORADB> SELECT/*+index(t idx_t)*/ * FROM t 2 WHERE t.NAME NOT IN(SELECT t1.NAME FROM t1); Elapsed: 00:00:00.03 Execution Plan ---------------------------------------------------------- Plan hash value: 1707532828 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 100K| 2636K| 591 (1)| 00:00:08 | | 1 | MERGE JOIN ANTI | | 100K| 2636K| 591 (1)| 00:00:08 | | 2 | TABLE ACCESS BY INDEX ROWID| T | 100K| 2148K| 587 (1)| 00:00:08 | | 3 | INDEX FULL SCAN | IDX_T | 100K| | 225 (1)| 00:00:03 | |* 4 | SORT UNIQUE | | 1 | 5 | 4 (25)| 00:00:01 | | 5 | TABLE ACCESS FULL | T1 | 1 | 5 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 4 - access("T"."NAME"="T1"."NAME") filter("T"."NAME"="T1"."NAME") |
dingjun123@ORADB> SELECT * FROM t WHERE t.NAME LIKE '%DINGJUN%'; Elapsed: 00:00:00.04 Execution Plan ---------------------------------------------------------- Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 5000 | 107K| 107 (2)| 00:00:02 | |* 1 | TABLE ACCESS FULL| T | 5000 | 107K| 107 (2)| 00:00:02 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("T"."NAME" LIKE '%DINGJUN%') --改为后通配,走索引 dingjun123@ORADB> SELECT * FROM t WHERE t.NAME LIKE 'DINGJUN%'; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 1594971208 ------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3 | 66 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T | 3 | 66 | 3 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T | 3 | | 2 (0)| 00:00:01 | ------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("T"."NAME" LIKE 'DINGJUN%') filter("T"."NAME" LIKE 'DINGJUN%') |
dingjun123@ORADB> CREATE INDEX idx1_t ON t (instr(NAME,'DINGJUN')); Index created. Elapsed: 00:00:00.16dingjun123@ORADB> SELECT * FROM t WHERE instr(t.NAME,'DINGJUN')>0; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 2071967826 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 5000 | 102K| 7 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T | 5000 | 102K| 7 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX1_T | 900 | | 3 (0)| 00:00:01 | -------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access(INSTR("NAME",'DINGJUN')>0) |
dingjun123@ORADB> SELECT * FROM t WHERE REVERSE(t.NAME) LIKE '1NUJGNID%'; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 3787301248 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 5000 | 102K| 8 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T | 5000 | 102K| 8 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX2_T | 900 | | 4 (0)| 00:00:01 | -------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access(REVERSE("NAME") LIKE '1NUJGNID%') filter(REVERSE("NAME") LIKE '1NUJGNID%') |
dingjun123@ORADB> CREATE INDEX idx3_t ON t(ID); Index created. Elapsed: 00:00:00.13 dingjun123@ORADB> ALTER TABLE T MODIFY ID NOT NULL; Table altered. Elapsed: 00:00:00.10 dingjun123@ORADB> SELECT * FROM t WHERE ID+0=1; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1000 | 21000 | 107 (2)| 00:00:02 | |* 1 | TABLE ACCESS FULL| T | 1000 | 21000 | 107 (2)| 00:00:02 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("ID"+0=1) dingjun123@ORADB> SELECT * FROM t WHERE ID=1; Elapsed: 00:00:00.01 Execution Plan ---------------------------------------------------------- Plan hash value: 2351669764 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1000 | 21000 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T | 1000 | 21000 | 3 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX3_T | 400 | | 1 (0)| 00:00:01 | -------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("ID"=1) |
dingjun123@ORADB> DROP TABLE t1; Table dropped. Elapsed: 00:00:00.03 dingjun123@ORADB> CREATE TABLE t1(x VARCHAR2(100)); Table created. Elapsed: 00:00:00.04 dingjun123@ORADB> CREATE INDEX idx_t1 ON t1(x); Index created. Elapsed: 00:00:00.04 dingjun123@ORADB> SELECT * FROM t1 WHERE x = 1; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 3617692013 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 52 | 2 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| T1 | 1 | 52 | 2 (0)| 00:00:01 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(TO_NUMBER("X")=1) dingjun123@ORADB> SELECT * FROM t1 WHERE x = '1'; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 1369807930 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 52 | 1 (0)| 00:00:01 | |* 1 | INDEX RANGE SCAN| IDX_T1 | 1 | 52 | 1 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("X"='1') |
DROP TABLE t_objects; CREATE TABLE t_objects AS SELECT * FROM dba_objects; CREATE INDEX idx_t_objects ON t_objects(last_ddl_time,created); BEGIN dbms_stats.gather_table_stats(ownname => USER,tabname => 't_objects',estimate_percent => 100,cascade => TRUE); END; / |
dingjun123@ORADB> SELECT * FROM 2 t_objects t 3 WHERE t.last_ddl_time-t.created>1 4 AND t.created>=DATE'2013-1-1'; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 3629755566 ------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1171 | 110K| 300 (1)| 00:00:04 | |* 1 | TABLE ACCESS FULL| T_OBJECTS | 1171 | 110K| 300 (1)| 00:00:04 | ------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("T"."CREATED">=TO_DATE(' 2013-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T"."LAST_DDL_TIME"-"T"."CREATED">1) |
dingjun123@ORADB> SELECT * FROM 2 t_objects t 3 WHERE t.last_ddl_time>=(t.created+1) 4 AND t.created>=DATE'2013-1-1'; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 3629755566 ------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 403 | 39091 | 301 (1)| 00:00:04 | |* 1 | TABLE ACCESS FULL| T_OBJECTS | 403 | 39091 | 301 (1)| 00:00:04 | ------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("T"."CREATED">=TO_DATE(' 2013-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T"."LAST_DDL_TIME">=INTERNAL_FUNCTION("T" ."CREATED")+1) |
dingjun123@ORADB> SELECT * FROM 2 t_objects t 3 WHERE t.last_ddl_time>=(t.created) 4 AND t.last_ddl_time>=(t.created+1) 5 AND t.created>=DATE'2013-1-1'; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 641904483 --------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2 | 194 | 11 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T_OBJECTS | 2 | 194 | 11 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T_OBJECTS | 6 | | 10 (0)| 00:00:01 | --------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("T"."LAST_DDL_TIME">=TO_DATE(' 2013-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T"."CREATED">=TO_DATE(' 2013-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T"."LAST_DDL_TIME" IS NOT NULL) filter("T"."CREATED">=TO_DATE(' 2013-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T"."LAST_DDL_TIME">="T"."CREATED" AND "T"."LAST_DDL_TIME">=INTERNAL_FUNCTION("T"."CREATED")+1) |
dingjun123@ORADB> SELECT * FROM 2 t_objects t 3 WHERE t.last_ddl_time>=DATE'2013-1-2' 4 AND t.last_ddl_time>=(t.created+1) 5 AND t.created>=DATE'2013-1-1'; Elapsed: 00:00:00.00 Execution Plan ---------------------------------------------------------- Plan hash value: 641904483 --------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 13 | 1261 | 11 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T_OBJECTS | 13 | 1261 | 11 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T_OBJECTS | 13 | | 10 (0)| 00:00:01 | --------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("T"."LAST_DDL_TIME">=TO_DATE(' 2013-01-02 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T"."CREATED">=TO_DATE(' 2013-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T"."LAST_DDL_TIME" IS NOT NULL) filter("T"."CREATED">=TO_DATE(' 2013-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "T"."LAST_DDL_TIME">=INTERNAL_FUNCTION("T"."CREATED")+1) |
SELECT * FROM t_objects t WHERE t.last_ddl_time-t.created>1; QL> set autotrace traceonly exp QL> SELECT * FROM 2 t_objects t 3 WHERE t.last_ddl_time-t.created>1; 执行计划 --------------------------------------------------------- Plan hash value: 3629755566 ------------------------------------------------------------------------------ Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------ 0 | SELECT STATEMENT | | 11706 | 1108K| 297 (2)| 00:00:04 | * 1 | TABLE ACCESS FULL| T_OBJECTS | 11706 | 1108K| 297 (2)| 00:00:04 | ------------------------------------------------------------------------------ redicate Information (identified by operation id): -------------------------------------------------- 1 - filter("T"."LAST_DDL_TIME"-"T"."CREATED">1) SQL> set autotrace off SQL> SELECT COUNT(*) FROM 2 t_objects t; COUNT(*) ---------- 74059 SQL> SELECT COUNT(*) FROM 2 t_objects t 3 WHERE t.last_ddl_time-t.created>1; COUNT(*) ---------- 216 应该走索引更佳,因为没有其他过滤条件,可以考虑建立函数索引: SQL> CREATE INDEX idx1_t_object ON t_objects(last_ddl_time-created); 索引已创建。 --注意收集直方图,因为分布不均 SQL> exec dbms_stats.gather_table_stats(ownname => USER,tabname => 't_objects',estimate_percent => 100,method_opt => 'for all indexed columns',cascade => TRUE); PL/SQL 过程已成功完成。 SQL> SELECT COUNT(*) FROM 2 t_objects t 3 WHERE t.last_ddl_time-t.created>1; 执行计划 ---------------------------------------------------------- Plan hash value: 3236535878 ----------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 9 | 4 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 9 | | | |* 2 | INDEX RANGE SCAN| IDX1_T_OBJECT | 986 | 8874 | 4 (0)| 00:00:01 | ----------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("LAST_DDL_TIME"-"CREATED">1) 统计信息 ---------------------------------------------------------- 1 recursive calls 0 db block gets 3 consistent gets 0 physical reads 0 redo size 424 bytes sent via SQL*Net to client 416 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed |