oracle hint用法测试

oracle hint测试

创建测试表

CREATE TABLE CUST
   (CUST_ID NUMBER,
	LAST_NAME VARCHAR2(30),
	FIRST_NAME VARCHAR2(30)
   ) ;

插入1000行数据

DECLARE v_cust_id NUMBER;
v_last_name VARCHAR2(30);
v_first_name VARCHAR(30);
BEGIN FOR i IN 1..1000 LOOP v_cust_id := i;
v_last_name := 'Last' || TO_CHAR(i, 'FM000');
v_first_name := 'First' || TO_CHAR(i, 'FM000');
INSERT into
    cust (cust_id, last_name, first_name)
VALUES
    (v_cust_id, v_last_name, v_first_name);
END LOOP;
COMMIT;
END;

分别创建索引

create index index_custid on cust(cust_id);
create index index_LAST_NAME on cust(LAST_NAME);

进行统计信息收集

EXEC DBMS_STATS.GATHER_DATABASE_STATS  全库统计 
exec dbms_stats.gather_table_stats(ownname = > 'SYS', tabname = > 'CUST'); 单表统计
select LAST_ANALYZED,table_name from dba_tables where table_name = 'CUST'; 查询统计信息

开启执行计划

// An highlighted block
set autotrace on

测试

select * from cust WHERE LAST_NAME ='Last1000' AND  CUST_ID='1000';

结果

  CUST_ID LAST_NAME			  FIRST_NAME
---------- ------------------------------ ------------------------------
      1000 Last1000			  First109


Execution Plan
----------------------------------------------------------
Plan hash value: 1939667504

----------------------------------------------------------------------------------------------------
| Id  | Operation			    | Name	   | Rows  | Bytes | Cost (%CPU)| Time	   |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT		    |		   |	 1 |	21 |	 2   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS BY INDEX ROWID BATCHED| CUST	   |	 1 |	21 |	 2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN		    | INDEX_CUSTID |	 1 |	   |	 1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("LAST_NAME"='Last1000')
   2 - access("CUST_ID"=1000)


Statistics
----------------------------------------------------------
	  1  recursive calls
	  0  db block gets
	  5  consistent gets
	  0  physical reads
	  0  redo size
	726  bytes sent via SQL*Net to client
	428  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
	  1  rows processed

结果该条sql 走了INDEX_CUSTID 索引
使用hint走index_LAST_NAME 索引

select /*+INDEX(cust index_LAST_NAME) */ * from cust WHERE LAST_NAME ='Last1000' AND  CUST_ID='1000';
   CUST_ID LAST_NAME			  FIRST_NAME
---------- ------------------------------ ------------------------------
      1000 Last1000			  First109


Execution Plan
----------------------------------------------------------
Plan hash value: 3778465092

-------------------------------------------------------------------------------------------------------
| Id  | Operation			    | Name	      | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT		    |		      |     1 |    21 |     2	(0)| 00:00:01 |
|*  1 |  TABLE ACCESS BY INDEX ROWID BATCHED| CUST	      |     1 |    21 |     2	(0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN		    | INDEX_LAST_NAME |     1 |       |     1	(0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("CUST_ID"=1000)
   2 - access("LAST_NAME"='Last1000')


Statistics
----------------------------------------------------------
	  0  recursive calls
	  0  db block gets
	  5  consistent gets
	  0  physical reads
	  0  redo size
	726  bytes sent via SQL*Net to client
	462  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
	  1  rows processed

强制走全表扫测试

select /*+ FULL(CUST)*/ * from cust WHERE LAST_NAME ='Last1000' AND  CUST_ID='1000';
  CUST_ID LAST_NAME			  FIRST_NAME
---------- ------------------------------ ------------------------------
      1000 Last1000			  First109


Execution Plan
----------------------------------------------------------
Plan hash value: 260468903

--------------------------------------------------------------------------
| Id  | Operation	  | Name | Rows  | Bytes | Cost (%CPU)| Time	 |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |	 |     1 |    21 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| CUST |     1 |    21 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("LAST_NAME"='Last1000' AND "CUST_ID"=1000)


Statistics
----------------------------------------------------------
	  1  recursive calls
	  0  db block gets
	  6  consistent gets
	  0  physical reads
	  0  redo size
	726  bytes sent via SQL*Net to client
	445  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
	  1  rows processed

相关推荐

  1. oracle hint用法测试

    2024-05-10 17:32:03       37 阅读
  2. Python高级用法:使用unittest进行单元测试

    2024-05-10 17:32:03       53 阅读
  3. python+selenium做ui自动化测试用法必会

    2024-05-10 17:32:03       22 阅读
  4. 用户管理测试用例

    2024-05-10 17:32:03       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-10 17:32:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-10 17:32:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-10 17:32:03       87 阅读
  4. Python语言-面向对象

    2024-05-10 17:32:03       96 阅读

热门阅读

  1. LeetCode|700. Search in Binary Search Tree

    2024-05-10 17:32:03       36 阅读
  2. 去中心化交易所系统开发搭建的关键步骤解析

    2024-05-10 17:32:03       29 阅读
  3. day 24 第七章 回溯算法part01

    2024-05-10 17:32:03       27 阅读
  4. 【Linux】安装Python3.11报错

    2024-05-10 17:32:03       36 阅读
  5. 5.Git

    5.Git

    2024-05-10 17:32:03      33 阅读
  6. Spring自动装配:解析原理与实践

    2024-05-10 17:32:03       30 阅读
  7. Python编程技巧(下篇)

    2024-05-10 17:32:03       31 阅读
  8. LInux 基础指令

    2024-05-10 17:32:03       27 阅读