ORACLE的索引

功能:加快查詢的(selected語句)速度
結構:內容(指定索引字段的值)和位置(rowid相當於普通索引中的頁碼)
rowid:ORACLE表的一個定位符。在記錄插入時生成,表示記錄在物理介質上的存放位置,它包含了Oracle定位數據行需要的所有信息,包括:數據庫對象、包括這一行的數據文件、數據塊在數據文件中的位置、記錄行在塊中的位置。
rowid表示記錄的物理位置。
rowid時查找記錄物理地址的最快方法。(rowid不是以十六進制表示的,並且rowid是可能變化的,導入再導出就不可信了)

可由dbm維持索引與主表的數據同步(條件:在字段中添加索引後)
索引可以在表的一列或多列上建立,一旦建立後,Oracle自動維護和使用,對用戶全透明。
索引的建立或是刪除對數據沒有影響。
一個索引的索引列最多32列。(通常一個索引對應一列。)

索引的影響:加快select,減慢insert(同時維護索引),對delete、update既會加快也可能會減慢。設計時是否建立索引應酌情考慮。
索引實際上是一個隱藏表。
 


索引的建立(這種方法默認建立缺省索引)
create index index_name
on table_name (字段列表);

索引類型
B_TREE:缺省索引(默認建立,相當於二分查找)。應用:含有大量不同值的列如姓名,時間。
Biemap IndexL位圖索引(建立數據值的表,如男、女,每個字段一列,在男的行,是男的標1,不是的標0)。
應用:含有大量相同值的列:如姓別,年齡。
建立:
create Bitmap index index_name
on table_name (字段列表);

索引何時起作用
當索引包括的第一個字段出現在where子句中,bdms自動調用索引加速尋找。
選用查修語句時要考慮index是否會起作用。

刪除索引
Drop INDEX index名;

系統中索引的查詢:
DESC USER_INDEXES;
select INDEX_NAME,INDEX_TYPE,TABLE_NAME
from USER_INDEXES;


驗證索引效果:

驗證索引的效果:

bi*tree
@C:\oracle\product\10.2.0\db_1\RDBMS\ADMIN\utlxplan.sql
//作用:建立保存評測分析的表。打印:表已創建

@create.sql(注:運行前從文件打開.sql文件)
//運行create:創建十萬條id帶默認索引的記錄的表

打開analysis.sql和analysis2.sql,逐條執行(注意:執行解釋之前要先清空解釋表plan_table)

bitmap:
@create_bit.sql
//運行create:創建十萬條id帶bit索引的記錄的表(耗時較長)

打開analysis_bitmap_10g.sql,逐條執行(注意:執行解釋之前要先清空解釋表plan_table)

fun://對加索引的字段進行處理後不能再使用索引,所以在建立索引時加入處理函數。
@create_fun.sql
打開analysis_fun.sql,逐條執行(注意:執行解釋之前要先清空解釋表plan_table)

 create.sql


drop table person;
create table person (id number(10),name varchar2(20));
create index ind_1 on person(id);

--控制是否SQL*plus顯示存儲過程或PL/SQL塊從SQL*plus提示符下執行,
--這樣的輸出最常見的由dbms_output.put_line程產生。
set serveroutput on
set echo on

declare
n_loop number;
s_time date;
e_time date;
begin
  select sysdate into s_time from dual ;
  for n_loop in 1..100000 loop
    insert into person values(n_loop,'name'||n_loop);
    commit;
  end loop;
  select sysdate into e_time from dual ;
  dbms_output.put_line(to_char((e_time),'hh:mi:ss'));
  dbms_output.put_line(to_char((s_time),'hh:mi:ss'));
end;
/
set serveroutput off

 analysis.sql

set echo on
delete from plan_table;
commit;

explain plan for select * from person where name='1234name';

select '-----無索引參加查詢的資源消耗-----' from dual;
select operation,cost from plan_table;


delete from plan_table;
commit;

explain plan for select * from person where id=1234;

select '-----有索引參加查詢的資源消耗-----' from dual;

select operation ,cost from plan_table;

 analysis2.sql

set echo on
delete from plan_table;
commit;

explain plan for select * from person where id/3=111;

select '-----不正確使用索引字段查詢的資源消耗-----' from dual;
select operation,cost from plan_table;


delete from plan_table;
commit;

explain plan for select * from person where id=111*3;

select '------正確使用索引字段查詢的資源消耗------' from dual;

select operation ,cost from plan_table;

create_bit.sql

drop table bit_test;
create table bit_test
(
 sname varchar2(20),
 sage number(2)
);

create bitmap index kk_l on bit_test(sage);

declare
n_loop number;
begin
  for n_loop in 1..100000 loop
    insert into bit_test values('name'||n_loop,3);
    commit;
  end loop;
end;
/

analysis_bitmap_10g.sql

set echo off
delete from plan_table;
commit;

explain plan for select * from bit_test where sage=3;

select '-----不正確使用索引字段查詢的資源消耗-----' from dual;
select operation,cost from plan_table;

delete from plan_table;
commit;

explain plan for select * from bit_test where sage<>3;

select '-----不正確使用索引字段查詢的資源消耗-----' from dual;
select operation,cost from plan_table;


create_fun.sql

drop table fun_test;
create table fun_test
(
 sname varchar2(20),
 snameL varchar2(20)
);

create index fun_ia on fun_test(sname);
create index fun_ib on fun_test(upper(snameL));

declare
n_loop number;
begin
  for n_loop in 1..100000 loop
    insert into fun_test values('name'||n_loop,'name'||n_loop);
    commit;
  end loop;
end;
/

analysis_fun.sql

set echo off
delete from plan_table;
commit;

explain plan for select * from fun_test where upper(sname)='NAME1';
--未添加函數索引
select '-----不正確使用索引字段查詢的資源消耗-----' from dual;
select operation,cost from plan_table;

delete from plan_table;
commit;

explain plan for select * from fun_test where upper(snameL)='NAME1';
--添加函數索引
select '-----不正確使用索引字段查詢的資源消耗-----' from dual;
select operation,cost from plan_table;


C:\oracle\product\10.2.0\db_1\RDBMS\ADMIN\utlxplan.sql

rem 
rem $Header: utlxplan.sql 08-may-2004.12:53:19 bdagevil Exp $ xplainpl.sql 
rem 
Rem Copyright (c) 1988, 2004, Oracle. All rights reserved.  
Rem NAME
REM    UTLXPLAN.SQL
Rem  FUNCTION
Rem  NOTES
Rem  MODIFIED
Rem     bdagevil   05/08/04  - add other_xml column 
Rem     bdagevil   06/18/03  - rename hint alias to object_alias
Rem     ddas       06/03/03  - increase size of hint alias column
Rem     bdagevil   02/13/03  - add plan_id and depth column
Rem     ddas       01/17/03  - add query_block and hint_alias columns
Rem     ddas       11/04/02  - revert timestamp column to DATE (PL/SQL problem)
Rem     ddas       10/28/02  - change type of timestamp column to TIMESTAMP
Rem     ddas       10/03/02  - add estimated_time column
Rem     mzait      04/16/02  - add row vector to the plan table
Rem     mzait      10/26/01  - add keys and filter predicates to the plan table
Rem     ddas       05/05/00  - increase length of options column
Rem     ddas       04/17/00  - add CPU, I/O cost, temp_space columns
Rem     mzait      02/19/98 -  add distribution method column
Rem     ddas       05/17/96 -  change search_columns to number
Rem     achaudhr   07/23/95 -  PTI: Add columns partition_{start, stop, id}
Rem     glumpkin   08/25/94 -  new optimizer fields
Rem     jcohen     11/05/93 -  merge changes from branch 1.1.710.1 - 9/24
Rem     jcohen     09/24/93 -  #163783 add optimizer column
Rem     glumpkin   10/25/92 -  Renamed from XPLAINPL.SQL 
Rem     jcohen     05/22/92 -  #79645 - set node width to 128 (M_XDBI in gendef)
Rem     rlim       04/29/91 -  change char to varchar2 
Rem     Peeler     10/19/88 - Creation
Rem
Rem This is the format for the table that is used by the EXPLAIN PLAN
Rem statement.  The explain statement requires the presence of this 
Rem table in order to store the descriptions of the row sources.

create table PLAN_TABLE (
        statement_id       varchar2(30),
        plan_id            number,
        timestamp          date,
        remarks            varchar2(4000),
        operation          varchar2(30),
        options            varchar2(255),
        object_node        varchar2(128),
        object_owner       varchar2(30),
        object_name        varchar2(30),
        object_alias       varchar2(65),
        object_instance    numeric,
        object_type        varchar2(30),
        optimizer          varchar2(255),
        search_columns     number,
        id                 numeric,
        parent_id          numeric,
        depth              numeric,
        position           numeric,
        cost               numeric,
        cardinality        numeric,
        bytes              numeric,
        other_tag          varchar2(255),
        partition_start    varchar2(255),
        partition_stop     varchar2(255),
        partition_id       numeric,
        other              long,
        distribution       varchar2(30),
        cpu_cost           numeric,
        io_cost            numeric,
        temp_space         numeric,
        access_predicates  varchar2(4000),
        filter_predicates  varchar2(4000),
        projection         varchar2(4000),
        time               numeric,
        qblock_name        varchar2(30),
        other_xml          clob
);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章