Chinaunix首页 | 论坛 | 博客
  • 博客访问: 91337164
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: DB2/Informix

2008-03-24 21:22:26

讨论这个之前;需要跟大家提到一个概念-“主键”;其实主键是一种特殊的唯一索引;当建立一个主键是;在系统中其实建立了一个不能为空的唯一索引。他和唯一索引的区别也就是不能为空这么一点了。因此他能做到唯一识别表中的一条记录的作用。索引都有索引名在create index  idx-name中定义;主键在定义是没有指定名称;但实际上系统会给自动命名一个unnn_nnn【n为数字】的一个名字。可以通过如下语句查得:【如查basetab_pps主键名字】

select constrname  from sysconstraints
where tabid in (select tabid from systables where tabname='basetab_pps');

下面可以谈谈如何恢复损坏得索引了:

1、  如果是普通得索引。这样就相对得简单了;删除并重建他就可以了。

drop  index  idx-name;
create index   idx-name  on  tabname(colname1,colname2…);

2、  如果是主键损坏;同样可以删除并重建他。

A、    删除主键
select constrname  from sysconstraints 
where tabid in (select tabid from systables where tabname='tabname');
alter  table  tabname  drop  constraint  cons_name;

B、 重建主键
alter table   basetab_pps  add  constraint primary key (colname1,colname2…);


3、如果重建都有问题;那么最后一招;只有将表重建了。导出数据和建表语句;删除表、重建表、重建索引。。。


关于索引的问题最后讨论一下;统计更新【upadte statistics】的作用

举个简单的例子:

试验一:
在dbaccess 中执行如下操作:
drop table t1 
create table t1 (c1  int,c2 char(10));
create  index i_t1 on t1 (c1);
insert into t1 values (1,1);
insert into t1 values (2,1);
insert into t1 values (3,1);

set explain on   --
select * from t1 where c1 = 2;

在运行目录下的sqexplain.out文件中看到:

QUERY:

------

select * from t1 where c1 = 2
Estimated Cost: 2
Estimated # of Rows Returned: 2
1) smpmml.t1: SEQUENTIAL SCAN
    Filters: smpmml.t1.c1 = 2

试验二:
在dbaccess 中执行如下操作:
drop table t1 
create table t1 (c1  int,c2 char(10));
create  index i_t1 on t1 (c1);
insert into t1 values (1,1);
insert into t1 values (2,1);
insert into t1 values (3,1);

update statistics for table t1;
set explain on 
select * from t1 where c1 = 2;

在运行目录下的sqexplain.out文件中看到:

QUERY:
------
select * from t1 where c1 = 2
Estimated Cost: 1
Estimated # of Rows Returned: 1

1) smpmml.t1: INDEX PATH
    (1) Index Keys: c1
       Lower Index Filter: smpmml.t1.c1 = 2

试验三:
在dbaccess 中执行如下操作:
drop table t1 
create table t1 (c1  int,c2 char(10));
create unique index i_t1 on t1 (c1);
insert into t1 values (1,1);
insert into t1 values (2,1);
insert into t1 values (3,1);

set explain on 
select * from t1 where c1 = 2;

在运行目录下的sqexplain.out文件中看到:
QUERY:
------
select * from t1 where c1 = 2
Estimated Cost: 2
Estimated # of Rows Returned: 1

1) smpmml.t1: INDEX PATH
    (1) Index Keys: c1
       Lower Index Filter: smpmml.t1.c1 = 2

分析一下:
试验一:建立了一般的索引;对按索引字段进行搜索;但是没有用到刚建立起来的索引。按全表扫描SEQUENTIAL SCAN进行查找。
试验二:建立了一般的索引;并且进行了统计更新后;对按索引字段进行搜索;使用到建立起来的索引。INDEX PATH
试验三:建立了唯一索引;对按索引字段进行搜索;使用到了刚建立起来的索引。INDEX PATH;显然如果建立了主键【特殊的唯一索引】;现象将是一样的。
    我们可以这样说;如果建立了主键或唯一索引;立马就能生效;这也是我们在WIN的安装、升级或维护过程中建立了一些表没有做统计更新同样能用到索引的原因。而如果建立的不是唯一索引;就需要执行统计更新才能用到索引。

    实际上统计更新除了能决定是否使用到索引外;还有使用这些统计信息如何使用索引及其他方式进行查询的目的。具体如何使用这些信息来决定查询路径就比较底层了。只知道通过执行update statistics命令,就可以使系统表systables、sysdistrib、syscolumns、sysindexes等表内的记录数、表空间的页数、记录长度、字段不同值个数、字段值的分布、索引的层数等信息得到更新。而服务器在进行语法分析后能通过查询优化器根据这些统计信息找到最有效的执行SQL的路径。
阅读(1275) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~