CREATE TABLE table_access_methods
(
id1 NUMBER(5,0) NOT NULL
, id2 NUMBER(5,0) NOT NULL
, type1 NUMBER(1,0) NOT NULL
, desc1 NVARCHAR2(2000) NOT NULL
);
DECLARE
indexLoop NUMBER(5,0);
BEGIN
FOR indexLoop IN 1..50000 LOOP
IF MOD(indexLoop,2) = 0 THEN
INSERT INTO table_access_methods (id1, id2, type1, desc1) VALUES (indexLoop, DBMS_RANDOM.VALUE(1,50000), 1, 'ABCD');
ELSE
INSERT INTO table_access_methods (id1, id2, type1, desc1) VALUES (indexLoop, DBMS_RANDOM.VALUE(1,50000), 2, 'ZYZW');
END IF;
END LOOP;
END;
/
COMMIT;
The sample data creates 50,000 rows with alternating type1 values (1 and 2) and random id2 values.
This distribution provides realistic scenarios for demonstrating different access methods.SELECT * FROM table_access_methods;Execution plan:
---------------------------------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ---------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 50 |00:00:00.01 | 4 | | 1 | TABLE ACCESS FULL| TABLE_ACCESS_METHODS | 1 | 48819 | 50 |00:00:00.01 | 4 | ----------------------------------------------------------------------------------------------------
CREATE INDEX ix_table_access_methods_id2 ON table_access_methods (id2); ANALYZE INDEX ix_table_access_methods_id2 COMPUTE STATISTICS;
SELECT * FROM table_access_methods WHERE ID2 = 2;Execution plan:
---------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 4 |00:00:00.01 | 6 |
| 1 | TABLE ACCESS BY INDEX ROWID| TABLE_ACCESS_METHODS | 1 | 4 | 4 |00:00:00.01 | 6 |
|* 2 | INDEX RANGE SCAN | IX_TABLE_ACCESS_METHODS_ID2 | 1 | 195 | 4 |00:00:00.01 | 2 |
---------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID2"=2)
CREATE UNIQUE INDEX ix_table_access_methods_id1 ON table_access_methods (id1); ANALYZE INDEX ix_table_access_methods_id1 COMPUTE STATISTICS;
SELECT * FROM table_access_methods WHERE ID1 = 2;Execution plan:
---------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 1 |00:00:00.01 | 3 |
| 1 | TABLE ACCESS BY INDEX ROWID| TABLE_ACCESS_METHODS | 1 | 1 | 1 |00:00:00.01 | 3 |
|* 2 | INDEX UNIQUE SCAN | IX_TABLE_ACCESS_METHODS_ID1 | 1 | 1 | 1 |00:00:00.01 | 2 |
---------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID1"=2)
SELECT * FROM table_access_methods WHERE id1 > 2;Execution plan:
---------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 50 |00:00:00.01 | 3 |
| 1 | TABLE ACCESS BY INDEX ROWID| TABLE_ACCESS_METHODS | 1 | 48812 | 50 |00:00:00.01 | 3 |
|* 2 | INDEX RANGE SCAN | IX_TABLE_ACCESS_METHODS_ID1 | 1 | 439 | 50 |00:00:00.01 | 2 |
---------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID1">2)
SELECT ID1 FROM table_access_methods ORDER BY id1;Execution plan:
---------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ---------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 50 |00:00:00.01 | 2 | | 1 | INDEX FULL SCAN | IX_TABLE_ACCESS_METHODS_ID1 | 1 | 48819 | 50 |00:00:00.01 | 2 | ----------------------------------------------------------------------------------------------------------
CREATE INDEX ix_table_access_methods_type1 ON table_access_methods (type1, id2); ANALYZE INDEX ix_table_access_methods_type1 COMPUTE STATISTICS;
SELECT type1, id2 FROM table_access_methods;Execution plan:
---------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ---------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 50 |00:00:00.01 | 7 | | 1 | INDEX FAST FULL SCAN| IX_TABLE_ACCESS_METHODS_TYPE1 | 1 | 48819 | 50 |00:00:00.01 | 7 | ----------------------------------------------------------------------------------------------------------------
SELECT * FROM table_access_methods WHERE id2 = 100;This query would use INDEX SKIP SCAN on ix_table_access_methods_type1 (type1, id2) because id2 is the second column in the index but type1 is not specified in the WHERE clause.