hints翻譯成中文就是提示,暗示的意思,它在資料庫中作用就是更改SQL語句的執行方式,你可以使用hints強制sql按照你所設定的方式執行sql,一般用來做效能診斷和調優,不建議在開發中使用。
1.寫一條SQL,使它通過全表掃描方式的效率優於索引訪問,分別給出各自的執行計畫。
LEO1@LEO1> create table leo1 as select * from dba_objects; 建立leo1表
Table created.
LEO1@LEO1> create index idx_leo1 on leo1(object_id); 在這個object_id列上建立索引
Index created.
LEO1@LEO1> execute dbms_stats.gather_table_stats('LEO1','LEO1',cascade=>true); 分析表和索引
PL/SQL procedure successfully completed.
LEO1@LEO1> select count(*) from leo1; 表上有71958行記錄
COUNT(*)
---------------
71958
LEO1@LEO1> select /*+ full(leo1) */ * from leo1 where object_id>100;
71859 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2716644435
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 71862 | 6807K| 287 (1)| 00:00:04 |
|* 1 | TABLE ACCESS FULL| LEO1 | 71862 | 6807K| 287 (1)| 00:00:04 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID">100)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
5762 consistent gets 5762次一致性讀
0 physical reads
0 redo size
3715777 bytes sent via SQL*Net to client
53214 bytes received via SQL*Net from client
4792 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
71859 rows processed
LEO1@LEO1> select /*+ index(leo1 idx_leo1) */ * from leo1 where object_id>100;
71859 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1434365503
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 71862 | 6807K| 1232 (1)| 00:00:15 |
| 1 | TABLE ACCESS BY INDEX ROWID| LEO1 | 71862 | 6807K| 1232 (1)| 00:00:15 |
|* 2 | INDEX RANGE SCAN | IDX_LEO1 | 71862 | | 160 (0)| 00:00:02 |
----------------------------------------------------------------------------------------