返回10%的数据一定就能走索引?

<pre name="code" class="sql">select count(*) from dwf.f_agt_business_contract_h
--1934179

select count(*) from dwf.f_agt_business_contract_h  t where t.end_dt=date'2999-12-31'
--256314

select (256314/1934179)*100 from dual;

很多人会说这个返回数据比列13.25%,应该走索引啊!

查看f_agt_business_contract_h的块数:

SQL> select owner,table_name,blocks from dba_tables where table_name=upper('f_agt_business_contract_h');

OWNER			       TABLE_NAME			  BLOCKS
------------------------------ ------------------------------ ----------
DWF			       F_AGT_BUSINESS_CONTRACT_H	  202806

BLOCKS*	NUMBER	
Number of used data blocks in the table
EMPTY_BLOCKS*	NUMBER	
Number of empty (never used) data blocks in the table

用show_tablespace查看;
Total Blocks............................203392
Total Bytes.............................1666187264
Unused Blocks...........................0
Unused Bytes............................0
Last Used Ext FileId....................51
Last Used Ext BlockId...................2437120
Last Used Block.........................8192

SQL> select count(distinct dbms_rowid.rowid_block_number(rowid))from  F_AGT_BUSINESS_CONTRACT_H	;

COUNT(DISTINCTDBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID))
---------------------------------------------------
					     156581


可以看到F_AGT_BUSINESS_CONTRACT_H 表一共有1934179行,存储在156581个block


查看select count(distinct dbms_rowid.rowid_block_number(rowid))from  F_AGT_BUSINESS_CONTRACT_H where end_dt=date'2999-12-31';


SQL> select count(distinct dbms_rowid.rowid_block_number(rowid))from  F_AGT_BUSINESS_CONTRACT_H where end_dt=date'2999-12-31';

COUNT(DISTINCTDBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID))
---------------------------------------------------
					      43398

加入访问end_dt=date'2999-12-31'走索引,那么会返回256314条记录,产生256314个rowid,去43398个数据块了找,都是单块读

光这里就需要43398个单块读,加上访问索引的成本。

而全表扫描: 只需要202806/128=1584个I/O由于一次单块读和多块读成本差不多,明显走全表扫描效率高。
 
 
 

你可能感兴趣的:(返回10%的数据一定就能走索引?)