2019独角兽企业重金招聘Python工程师标准>>>
索引丢失或则索引失效之后,在不进行数据库恢复的情况如何解决?下面会模拟索引丢失的情况。
1 新建一个表空间tmp0408
create tablespace tmp0408 datafile '/u01/oracle/oradata/orcl/tmp0408.dbf' size 10m;
SQL> create tablespace tmp0408 datafile '/u01/oracle/oradata/orcl/tmp0408.dbf' size 10M;
Tablespace created.
SQL> select file#,status,name from v$datafile;
FILE# STATUS NAME
---------- ------- --------------------------------------------------
1 SYSTEM /u01/oracle/oradata/orcl/system01.dbf
2 ONLINE /u01/oracle/oradata/orcl/sysaux01.dbf
3 ONLINE /u01/oracle/oradata/orcl/undotbs01.dbf
4 ONLINE /u01/oracle/oradata/orcl/users01.dbf
5 ONLINE /u01/oracle/oradata/orcl/user02.dbf
6 ONLINE /u01/oracle/oradata/orcl/tmpspace0327
7 ONLINE /u01/oracle/oradata/orcl/tmp0408.dbf
2 在刚建的表空间tmp0408 中创建表emp 字段ename的索引index_emp_ename_i
SQL> create index INDEX_EMP_ENAME_I on emp(ename) tablespace tmp0408;
Index created.
SQL> select index_name,table_owner,table_name,tablespace_name from user_indexes where table_name = 'EMP';
INDEX_NAME TABLE_OWNE TABLE_NAME TABLESPACE_NAME
-------------------- ---------- -------------------- --------------------
INDEX_EMP_ENAME_I SCOTT EMP TMP0408
PK_EMP SCOTT EMP USERS
3 删除表空间的文件tmp0408.dbf
SQL> drop index index_emp_ename_i;
#删除索引之后在sys用户下切换几次日志
SQL> alter system switch logfile;
System altered.
SQL> alter system switch logfile;
# 切换用户已经报错了,数据发现表空间tmp0408的数据文件tmp0408文件已经不在了,报错
SQL> conn scott/tiger
ERROR:
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
Linux-x86_64 Error: 2: No such file or directory
Process ID: 0
Session ID: 0 Serial number: 0
# 再切换会sys用户已经是空实例了
SQL> conn / as sysdba
Connected to an idle instance.
SQL> startup nomount;
ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
ORACLE instance started.
Total System Global Area 584568832 bytes
Fixed Size 2230552 bytes
Variable Size 440403688 bytes
Database Buffers 134217728 bytes
Redo Buffers 7716864 bytes
SQL> alter database mount;
Database altered.
# 启动到mount状态下,把丢失的表空间下线再open数据库
SQL> alter database datafile '/u01/oracle/oradata/orcl/tmp0408.dbf' offline drop;
Database altered.
SQL> alter database open;
Database altered.
这个时候就发现scott.emp 里面无法插入数据了,因为索引对于的表空间没有了,索引失效
SQL> insert into emp (empno,ename)values(123,'aaa');
insert into emp (empno,ename)values(123,'aaa')
*
ERROR at line 1:
ORA-00376: file 7 cannot be read at this time
ORA-01110: data file 7: '/u01/oracle/oradata/orcl/tmp0408.dbf'
解决办法:删掉索引,重建索引。
注意:要先找到索引叫什么名字,当时简历索引的sql语句是哪个才可以删除索引。
SQL> select index_name,table_name,tablespace_name from user_indexes;
INDEX_NAME TABLE_NAME TABLESPACE_NAME
-------------------- -------------------- --------------------
INDEX_EMP_ENAME_I EMP TMP0408
PK_EMP EMP USERS
PK_DEPT DEPT USERS
索引名字:INDEX_EMP_ENAME_I
SQL> select dbms_metadata.get_ddl('INDEX','INDEX_EMP_ENAME_I') from dual;
DBMS_METADATA.GET_DDL('INDEX','INDEX_EMP_ENAME_I')
--------------------------------------------------------------------------------
CREATE INDEX "SCOTT"."INDEX_EMP_ENAME_I" ON "SCOTT"."EMP" ("ENAME")
PCTFRE
创建索引的sql:CREATE INDEX "SCOTT"."INDEX_EMP_ENAME_I" ON "SCOTT"."EMP" ("ENAME")
4 删除原来的索引并重建索引
SQL> drop index INDEX_EMP_ENAME_I;
Index dropped.
SQL> CREATE INDEX "SCOTT"."INDEX_EMP_ENAME_I" ON "SCOTT"."EMP" ("ENAME") tablespace users;
Index created.
SQL> select index_name,table_name,tablespace_name from user_indexes;
INDEX_NAME TABLE_NAME TABLESPACE_NAME
-------------------- -------------------- --------------------
INDEX_EMP_ENAME_I EMP USERS
PK_EMP EMP USERS
PK_DEPT DEPT USERS
这个时候索引已经恢复了,表emp里面又可以重新插入数据了
SQL> insert into emp (empno,ename)values(123,'aaa');
1 row created.
SQL> commit;
Commit complete.
SQL> select empno,ename from emp where empno = 123;
EMPNO ENAME
---------- ----------
123 aaa
注意:索引是恢复了,只是索引所在的表空间换了一下。而且之前已经是把丢失文件的表空间tmp0408 下线的,下一次重启又会无法启动了,所以建议 把哪个丢失文件的表空间删除。
SQL> select tablespace_name,status from user_tablespaces;
TABLESPACE_NAME STATUS
-------------------- ---------
SYSTEM ONLINE
SYSAUX ONLINE
UNDOTBS1 ONLINE
TEMP ONLINE
USERS ONLINE
USER02 ONLINE
TMP_SPACE_0327 ONLINE
TMP0408 ONLINE
SQL> select file#,name,status from v$datafile;
FILE# NAME STATUS
---------- ---------------------------------------- ----------
########## /u01/oracle/oradata/orcl/system01.dbf SYSTEM
########## /u01/oracle/oradata/orcl/sysaux01.dbf ONLINE
########## /u01/oracle/oradata/orcl/undotbs01.dbf ONLINE
########## /u01/oracle/oradata/orcl/users01.dbf ONLINE
########## /u01/oracle/oradata/orcl/user02.dbf ONLINE
########## /u01/oracle/oradata/orcl/tmpspace0327 ONLINE
########## /u01/oracle/oradata/orcl/tmp0408.dbf RECOVER
# 可以看到tmp0408.dbf 状态有问题。
SQL> drop tablespace TMP0408;
Tablespace dropped.
# 表空间直接删除
SQL> select file#,name,status from v$datafile;
FILE# NAME STATUS
---------- ---------------------------------------- ----------
########## /u01/oracle/oradata/orcl/system01.dbf SYSTEM
########## /u01/oracle/oradata/orcl/sysaux01.dbf ONLINE
########## /u01/oracle/oradata/orcl/undotbs01.dbf ONLINE
########## /u01/oracle/oradata/orcl/users01.dbf ONLINE
########## /u01/oracle/oradata/orcl/user02.dbf ONLINE
########## /u01/oracle/oradata/orcl/tmpspace0327 ONLINE
6 rows selected.
SQL> select tablespace_name,status from user_tablespaces;
TABLESPACE_NAME STATUS
-------------------- ----------
SYSTEM ONLINE
SYSAUX ONLINE
UNDOTBS1 ONLINE
TEMP ONLINE
USERS ONLINE
USER02 ONLINE
TMP_SPACE_0327 ONLINE
7 rows selected.
# 已经删除丢失的表空间
ORACLE instance started.
Total System Global Area 584568832 bytes
Fixed Size 2230552 bytes
Variable Size 440403688 bytes
Database Buffers 134217728 bytes
Redo Buffers 7716864 bytes
Database mounted.
Database opened.