I did some tests here to prove it, and I realized that
it actually commands using Hint.
See the example, I created an index in the EMP ENAM field:
Select all
create index emp_ind_ename on emp (ename)
Now, I'll do a Select: (Repair the Ename field)
Select all
SQL> SELECT * FROM EMP WHERE ename like '%';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------------- ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=5 Card=14 Bytes=1218)
1 0 TABLE ACCESS (FULL) OF 'EMP' (TABLE) (Cost=5 Card=14 Bytes =1218)
Statistics
----------------------------------------------------------
4 recursive calls
0 db block gets
34 consistent gets
0 physical reads
0 redo size
1201 bytes sent via SQL*Net to client
435 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
14 rows processed
SQL>
In this case, as I did not put Hint it is not forcing the use of the index, made Full, the data came messy.
Now, I will repeat exactly the same query, but with Hint:
(the like is for him to force the use of the index. If I do not use the column, it Fullscan and does not use the index, and the lines come messy).
Select all
SQL> SELECT /*+ INDEX_DESC( EMP emp_ind_ename)*/ * FROM EMP WHERE ename like '%';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------------- ---------- ---------- ----------
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7369 SMITH CLERK 7902 17-DEC-80 800 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7839 KING PRESIDENT 17-NOV-81 5000 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
14 rows selected.
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=2 Card=14 Bytes=1218)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'EMP' (TABLE) (Cost=2 Card=14 Bytes=1218)
2 1 INDEX (FULL SCAN DESCENDING) OF 'EMP_IND_ENAME' (INDEX) (Cost=1 Card=1)
Statistics
----------------------------------------------------------
4 recursive calls
0 db block gets
22 consistent gets
0 physical reads
0 redo size
1275 bytes sent via SQL*Net to client
435 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
14 rows processed
SQL>
Note that now, the lines came to the contrary, as I used index_desc.
I will do the same test with index_asc:
Select all
SQL> SELECT /*+ INDEX_ASC( EMP emp_ind_ename)*/ * FROM EMP WHERE ENAME LIKE '%';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------------- ---------- ---------- ----------
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
14 rows selected.
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=2 Card=14 Bytes=1218)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'EMP' (TABLE) (Cost=2 Card=14 Bytes=1218)
2 1 INDEX (FULL SCAN) OF 'EMP_IND_ENAME' (INDEX) (Cost=1 Card=1)
Statistics
----------------------------------------------------------
4 recursive calls
0 db block gets
22 consistent gets
0 physical reads
0 redo size
1262 bytes sent via SQL*Net to client
435 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
14 rows processed
SQL>
Now, the execution plan with an Order By, without Hint:
Select all
SELECT * FROM EMP ORDER BY ENAME;
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=14 Bytes=1218)
1 0 SORT (ORDER BY) (Cost=6 Card=14 Bytes=1218)
2 1 TABLE ACCESS (FULL) OF 'EMP' (TABLE) (Cost=5 Card=14 Bytes=1218)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
15 consistent gets
0 physical reads
0 redo size
1262 bytes sent via SQL*Net to client
435 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed
Conclusion:
* With Order BY, the cost increased.
* This HINTS system even works in some cases. But I would not use any hypothesis, because if the optimizer changes the plan and decides to use a full table scan automatically, it will no longer use the index and your data will come messy. In all these years, I'vê never seen anyone using this system to order lines.
* I talked to my Mauro Mauro from Bittencourt on this subject because I found it super bizarre all this. He said he has seen this in another company, but it is an old database, which the optimizer was per rule. In this case, the execution plan would never change, so for him, it was not so dangerous to use this method.
I would like to know other people's opinion on this ...