Antes de criar um index, podemos testar se ele resolverá seu problema através da criação de um INDEX VIRTUAL.
SQL> create table emp
2 (id number,
3 job varchar2(50))
4 /
Tabela criada.
SQL> declare
2 contador integer;
3 begin
4 contador := 1;
5 while contador <= 1000 loop
6 insert into emp values (contador,'JOB');
7 contador := contador + 1;
8 end loop;
9 commit;
10 end;
11 /
Procedimento PL/SQL concluído com sucesso.
SQL> alter table emp add status varchar2(20);
Tabela alterada.
SQL> update emp set status = 'ok' where id between 1 and 500;
500 linhas atualizadas.
SQL> update emp set status = 'nok' where id between 501 and 1000;
500 linhas atualizadas.
SQL> commit;
Commit concluído.
SQL> set autotrace traceonly explain
SQL> select * from emp where status = 'ok';
Plano de Execução
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 500 | 26000 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 500 | 26000 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("STATUS"='ok')
Note
-----
- dynamic sampling used for this statement
SQL> create index idx_emp_status on emp(status) nosegment;
Índice criado.
SQL> select * from emp where status = 'ok';
Plano de Execução
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 500 | 26000 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 500 | 26000 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("STATUS"='ok')
Note
-----
- dynamic sampling used for this statement
SQL> alter session set "_use_nosegment_indexes" = true;
Sessão alterada.
SQL> select * from emp where status = 'ok';
Plano de Execução
----------------------------------------------------------
Plan hash value: 229917500
----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 500 | 26000 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 500 | 26000 | 1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_EMP_STATUS | 4 | | 1 (0)| 00:00:01 |
----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("STATUS"='ok')
Note
-----
- dynamic sampling used for this statement
Nenhum comentário:
Postar um comentário