oracle 索引介绍

来源:互联网 发布:电路磁场仿真软件 编辑:程序博客网 时间:2024/06/05 00:17

索引类似于一本书的目录,我们通过查询目录就可以找到指定数据所在的具体位置,
而不需要翻越整本书。索引在检索数据方面具有高效性,尤其是从存储了大量数据的表中查询数据时。
索引是一种树状结构,可以分为两类:
1.从逻辑设计
      单列索引,组合索引,唯一性索引,非唯一性索引,基于函数的索引...
2.从物理实现
      分区索引,非分区索引,b树索引,正向索引,反向索引,位图索引,位图联接索引...
oracle提供了这么多索引,常用到的如下:
·B*树索引
    ·索引组织表
    ·B*树聚簇索引
    ·降序索引
    ·反向键索引
·位图索引
·位图联结索引
·基于函数的索引
·应用域索引

索引管理
注意下面问题:
·装载数据后再建立索引
·索引应建立在where子句经常引用的列上
·在联接属性上建立索引
·不要再小表上建立索引
·在经常需要排序操作的列上建立索引
·删除不经常使用的索引
·指定索引块的参数,如果将来会在表上执行大量的insert操作,建立索引时设定较大的ptcfree
·指定索引所在的表空间,将表和索引放在不同的表空间上可以提高性能

创建索引

[c-sharp] view plaincopy
  1. create [unique] index index_name on  
  2. table_name(col_name[,col_name...])  
  3. tablespace table_space_name;  
  4. ·unique:指定索引列中值必须是唯一的  
  5. ·index_name:索引名  
  6. ·table_name:指定要建立索引的表  
  7. ·col_name:要建立索引的列,可以是多列,那样的索引叫多列索引  
  8. ·table_space_name:索引存储的表空间  

实例1:

[c-sharp] view plaincopy
  1. 如果我们经常对emp表进行按照empno来查询某个员工的信息,  
  2. SQL> select * from emp where empno=7788;  
  3.   
  4. EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO  
  5. ----- ---------- --------- ----- ----------- --------- --------- ------  
  6.  7788 SCOTT      ANALYST    7566 1987-4-19     3000.00               20  
  7. 我们就应该对empno这一列建一个索引。  
  8. create index indx_on_empno   
  9. on emp(empno)   
  10. tablespace users;  

实例2:

 

[c-sharp] view plaincopy
  1. 如果我们经常查询某个部门工资大于1000的员工信息,  
  2. 那么我们就可以在job和sal列上建立所以,这叫组合索引:  
  3. SQL> select * from emp  
  4.   2  where job='SALESMAN' and sal>1000;  
  5.   
  6. EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO  
  7. ----- ---------- --------- ----- ----------- --------- --------- ------  
  8.  7499 ALLEN      SALESMAN   7698 1981-2-20     1600.00    300.00     30  
  9.  7521 WARD       SALESMAN   7698 1981-2-22     1250.00    500.00     30  
  10.  7654 MARTIN     SALESMAN   7698 1981-9-28     1250.00   1400.00     30  
  11.  7844 TURNER     SALESMAN   7698 1981-9-8      1500.00      0.00     30  
  12.   
  13. create index indx_on_job_sal   
  14. on emp(job,sal)  
  15. tablespace users;  

实例3:

[c-sharp] view plaincopy
  1. 我们还可以创建基于函数的索引,不过在此前,我们必须设置一下初始化参数:  
  2. conn system/chage_on_install as sysdba;  
  3. alter system set query_rewrite_enabled=true;  
  4.   
  5. create index indx_lower_job  
  6. on emp(lower(job))  
  7. tablespace users;  

修改索引

[c-sharp] view plaincopy
  1. 就像修改表一样,我们在创建索引后可以进行修改  
  2. 实例1:分配和释放索引空间  
  3. alter index indx_on_empno  
  4. allocate extent(size 1m);  
  5.   
  6. 实例2:释放多余的索引空间  
  7. alter index indx_on_empno  
  8. deallocate unused;  
  9.   
  10. 实例3:重建索引  
  11. alter index indx_on_job_sal rebuild;  
  12.   
  13. 实例4:联机重建索引,  
  14.       使用rebuild,若其他用户正在表上执行dml操作,重建会失败,  
  15.       使用如下语句,就可以成功重建  
  16. alter index indx_on_job_sal rebuild online;  
  17.   
  18. 实例5:合并索引  
  19.       当相邻索引块存在剩余空间,通过合并索引将其合并到一个索引块上  
  20. alter index indx_on_job_sal coalesce;  
  21.   
  22. 实例6:重命名索引  
  23. alter index indx_on_job_sal rename to indx_on_jobandsal;  

删除索引

[c-sharp] view plaincopy
  1. 如果索引不再需要了,留在数据库中将会占用资源,我们可以将其删除  
  2. drop index indx_on_job_sal;  

有关索引的信息

[c-sharp] view plaincopy
  1. 我们可以从unser_indexes视图中查看有关某表的索引信息,  
  2. 下面我先来看看unser_indexes视图的信息:  
  3. SQL> desc user_indexes;  
  4. Name                    Type           Nullable Default Comments                                                                                 
  5. ----------------------- -------------- -------- ------- --------------------------------------------------------------------------------------   
  6. INDEX_NAME              VARCHAR2(30)                    Name of the index                                                                        
  7. INDEX_TYPE              VARCHAR2(27)   Y                                                                                                         
  8. TABLE_OWNER             VARCHAR2(30)                    Owner of the indexed object                                                              
  9. TABLE_NAME              VARCHAR2(30)                    Name of the indexed object                                                               
  10. TABLE_TYPE              VARCHAR2(11)   Y                Type of the indexed object                                                               
  11. UNIQUENESS              VARCHAR2(9)    Y                Uniqueness status of the index:  "UNIQUE",  "NONUNIQUE", or "BITMAP"                     
  12. COMPRESSION             VARCHAR2(8)    Y                Compression property of the index: "ENABLED",  "DISABLED", or NULL                       
  13. PREFIX_LENGTH           NUMBER         Y                Number of key columns in the prefix used for compression                                 
  14. TABLESPACE_NAME         VARCHAR2(30)   Y                Name of the tablespace containing the index                                              
  15. INI_TRANS               NUMBER         Y                Initial number of transactions                                                           
  16. MAX_TRANS               NUMBER         Y                Maximum number of transactions                                                           
  17. INITIAL_EXTENT          NUMBER         Y                Size of the initial extent in bytes                                                      
  18. NEXT_EXTENT             NUMBER         Y                Size of secondary extents in bytes                                                       
  19. MIN_EXTENTS             NUMBER         Y                Minimum number of extents allowed in the segment                                         
  20. MAX_EXTENTS             NUMBER         Y                Maximum number of extents allowed in the segment                                         
  21. PCT_INCREASE            NUMBER         Y                Percentage increase in extent size                                                       
  22. PCT_THRESHOLD           NUMBER         Y                Threshold percentage of block space allowed per index entry                              
  23. INCLUDE_COLUMN          NUMBER         Y                User column-id for last column to be included in index-only table top index              
  24. FREELISTS               NUMBER         Y                Number of process freelists allocated in this segment                                    
  25. FREELIST_GROUPS         NUMBER         Y                Number of freelist groups allocated to this segment                                      
  26. PCT_FREE                NUMBER         Y                Minimum percentage of free space in a block                                              
  27. LOGGING                 VARCHAR2(3)    Y                Logging attribute                                                                        
  28. BLEVEL                  NUMBER         Y                B-Tree level                                                                             
  29. LEAF_BLOCKS             NUMBER         Y                The number of leaf blocks in the index                                                   
  30. DISTINCT_KEYS           NUMBER         Y                The number of distinct keys in the index                                                 
  31. AVG_LEAF_BLOCKS_PER_KEY NUMBER         Y                The average number of leaf blocks per key                                                
  32. AVG_DATA_BLOCKS_PER_KEY NUMBER         Y                The average number of data blocks per key                                                
  33. CLUSTERING_FACTOR       NUMBER         Y                A measurement of the amount of (dis)order of the table this index is for                 
  34. STATUS                  VARCHAR2(8)    Y                Whether the non-partitioned index is in USABLE or not                                    
  35. NUM_ROWS                NUMBER         Y                Number of rows in the index                                                              
  36. SAMPLE_SIZE             NUMBER         Y                The sample size used in analyzing this index                                             
  37. LAST_ANALYZED           DATE           Y                The date of the most recent time this index was analyzed                                 
  38. DEGREE                  VARCHAR2(40)   Y                The number of threads per instance for scanning the partitioned index                    
  39. INSTANCES               VARCHAR2(40)   Y                The number of instances across which the partitioned index is to be scanned              
  40. PARTITIONED             VARCHAR2(3)    Y                Is this index partitioned? YES or NO                                                     
  41. TEMPORARY               VARCHAR2(1)    Y                Can the current session only see data that it place in this object itself?               
  42. GENERATED               VARCHAR2(1)    Y                Was the name of this index system generated?                                             
  43. SECONDARY               VARCHAR2(1)    Y                Is the index object created as part of icreate for domain indexes?                       
  44. BUFFER_POOL             VARCHAR2(7)    Y                The default buffer pool to be used for index blocks                                      
  45. USER_STATS              VARCHAR2(3)    Y                Were the statistics entered directly by the user?                                        
  46. DURATION                VARCHAR2(15)   Y                If index on temporary table, then duration is sys$session or sys$transaction else NULL   
  47. PCT_DIRECT_ACCESS       NUMBER         Y                If index on IOT, then this is percentage of rows with Valid guess                        
  48. ITYP_OWNER              VARCHAR2(30)   Y                If domain index, then this is the indextype owner                                        
  49. ITYP_NAME               VARCHAR2(30)   Y                If domain index, then this is the name of the associated indextype                       
  50. PARAMETERS              VARCHAR2(1000) Y                If domain index, then this is the parameter string                                       
  51. GLOBAL_STATS            VARCHAR2(3)    Y                Are the statistics calculated without merging underlying partitions?                     
  52. DOMIDX_STATUS           VARCHAR2(12)   Y                Is the indextype of the domain index valid                                               
  53. DOMIDX_OPSTATUS         VARCHAR2(6)    Y                Status of the operation on the domain index                                              
  54. FUNCIDX_STATUS          VARCHAR2(8)    Y                Is the Function-based Index DISABLED or ENABLED?                                         
  55. JOIN_INDEX              VARCHAR2(3)    Y                Is this index a join index?                                                              
  56. IOT_REDUNDANT_PKEY_ELIM VARCHAR2(3)    Y                Were redundant primary key columns eliminated from iot secondary index?                  
  57. DROPPED                 VARCHAR2(3)    Y                Whether index is dropped and is in Recycle Bin  
  58.   
  59. 我们常关心的也就那么几列:index_name,table_owner,table_name,uniqueness,status.  
  60. 实例7:我们来查询一下scott用户下的索引有哪些  
  61. SQL> select index_name,table_owner,table_name,uniqueness,status from user_indexes;  
  62.   
  63. INDEX_NAME                     TABLE_OWNER                    TABLE_NAME                     UNIQUENESS STATUS  
  64. ------------------------------ ------------------------------ ------------------------------ ---------- --------  
  65. PK_EMP                         SCOTT                          EMP                            UNIQUE     VALID  
  66. PK_DEPT                        SCOTT                          DEPT                           UNIQUE     VALID  

有关列索引的信息

[sql] view plaincopy
  1. 我们可以从user_ind_columns中获得列素颜的信息,先看看user_ind_columns视图的结果:  
  2. SQL> desc user_ind_columns;  
  3. Name            Type           Nullable Default Comments                                                          
  4. --------------- -------------- -------- ------- ---------------------------------------------------------------   
  5. INDEX_NAME      VARCHAR2(30)   Y                Index name                                                        
  6. TABLE_NAME      VARCHAR2(30)   Y                Table or cluster name                                             
  7. COLUMN_NAME     VARCHAR2(4000) Y                Column name or attribute of object column                         
  8. COLUMN_POSITION NUMBER         Y                Position of column or attribute within index                      
  9. COLUMN_LENGTH   NUMBER         Y                Maximum length of the column or attribute, in bytes               
  10. CHAR_LENGTH     NUMBER         Y                Maximum length of the column or attribute, in characters          
  11. DESCEND         VARCHAR2(4)    Y                DESC if this column is sorted descending on disk, otherwise ASC   
  12.   
  13. 实例8:查看emp和dept表中列的索引信息  
  14. SQL> select index_name,table_name,column_name  
  15.   2  from user_ind_columns  
  16.   3  where table_name in ('EMP','DEPT');  
  17.   
  18. INDEX_NAME                     TABLE_NAME                     COLUMN_NAME  
  19. ------------------------------ ------------------------------ ----------------------------------------------------  
  20. PK_DEPT                        DEPT                           DEPTNO  
  21. PK_EMP                         EMP                            EMPNO  

转载于:http://blog.csdn.net/chen_linbo/article/details/6302773


0 0