1.问题:
oracle该schema下的对象很多,3万个以上,直接执行删除用户命令要很长时间,半小时未删除完成
2.删除前的准备工作
一般删除用户都是为了重新导入该用户数据(不删除表空间),涉及到删除该schema的重建,故删除用户前收集下该schema信息
1)查看用户的默认表空间及临时表空间
set lines 300
col username for a30
select username ,default_tablespace,TEMPORARY_TABLESPACE from dba_users where username='TEST';
2)查看该用户的权限和角色
select privilege from dba_sys_privs where grantee='SYSADM'
union
select privilege from dba_sys_privs where grantee in (select granted_role from dba_role_privs where grantee='TEST' );
3)获取获得授予用户权限的脚本
select 'grant '||privilege||' to SYSADM;' from (select privilege from dba_sys_privs where grantee='SYSADM'
union
select privilege from dba_sys_privs where grantee in (select granted_role from dba_role_privs where grantee='SYSADM' ));
3.快速删除用户办法
1)停止连接该数据库的应用服务
(大部分的应用都用重连机制,不关闭应用服务的话应用会反复的连接数据库,即使使用数据库kill session的命令也无法结束,会话会一直存在)
2)执行脚本获得删除该schema下对象的脚本
test为要删除的schema
connect test/test
spool /home/oracle/del_test.sql;
prompt --Drop constraint
select 'alter table '||table_name||' drop constraint '||constraint_name||' ;' from user_constraints where constraint_type='R';
prompt --truncate table
select 'truncate table '||table_name ||';' from user_tables;
prompt --Drop tables
select 'drop table '||table_name ||' purge;' from user_tables;
prompt --Drop indexes
select 'drop index '||index_name ||';' from user_indexes;
prompt --Drop view
select 'drop view ' ||view_name||';' from user_views;
prompt --Drop sequence
select 'drop sequence ' ||sequence_name||';' from user_sequences;
prompt --Drop function
select 'drop function ' ||object_name||';' from user_objects where object_type='FUNCTION';
prompt --Drop procedure
select 'drop procedure '||object_name||';' from user_objects where object_type='PROCEDURE';
prompt --Drop package
prompt --Drop package body
select 'drop package '|| object_name||';' from user_objects where object_type='PACKAGE';
prompt --Drop database link
select 'drop database link '|| object_name||';' from user_objects where object_type='DATABASE LINK';
spool off;
3)sqlplus连接到该schema下,执行如上获得的脚本
执行前查看下该schema下的对象,执行后再次查看下该schema下的对象
SQL> select object_type,count(*) from user_objects group by object_type;
4)kill掉连接数据库的session
select 'alter system kill session '''||sid||','||serial#||''' immediate;' from v$session where username='TEST';
5)删除该schema
drop user test cascade;