2008 年 08 月 07日, 星期四
IMP命令如何把对象导入用户的默认表空间,而不是对象原来的表空间
网上整理,不是原创,作者不祥
1.sqlplus system/sys
2.创建数据库用户
SQL> create user newuser identified by newpass
2 default tablespace newspace
3 temporary tablespace temp
4 /
3.授予权限给用户
SQL> grant connect,resource to newuser;
SQL> grant dba to newuser;
4.移除无限表空间权限
SQL> revoke unlimited tablespace from newuser;
5.取消用户在原表空间的配额
SQL> alter user newuser quota 0 on users;
6.授予用户在默认表空间的无限配额
SQL> alter user newuser quota unlimited on newspace;
7.SQL> exit
8.执行导入
imp newuser/newpass file=data.dmp fromuser=olduser touser=newuser grants=n
Oracle IMP
通过参数文件,使用一些好用的选项 imp parfile=.par.txt
par.txt中好用的选项举例
userid=xxx/xxx@xxx
file=.xxx.dmp
log=.xxx.log
fromuser=(xxx)
touser=(xxx)
feedback=20000
commit=y
buffer=134217728
indexes=n
analyze=n
grants=n
indexes=n表示除了必要的如pk之外的索引,其他索引不导入
2008 年 07 月 23日, 星期三
Cursor Sharing监控相关语句
自己总结和摘录的一些如何发现cursor sharing问题相关的一些SQL,很多是摘自Performance Tuning Guide,绝大部分并非原创。主要是探讨如何去发现相似的SQL或者查找SQL没有共享的原因。
查看全文查看系统中各种等待时间占用的数据库时间比例
-- 摘自Performance Tuning Guide
SELECT EVENT, WAIT_CLASS,
TIME_WAITED_MICRO,ROUND(TIME_WAITED_MICRO*100/S.DBTIME,1) PCT_DB_TIME
FROM V$SYSTEM_EVENT E, V$EVENT_NAME N,
(SELECT VALUE DBTIME FROM V$SYS_TIME_MODEL WHERE STAT_NAME = 'DB time') S
WHERE E.EVENT_ID = N.EVENT_ID
AND N.WAIT_CLASS NOT IN ('Idle', 'System I/O')
ORDER BY PCT_DB_TIME ASC;
2008 年 07 月 18日, 星期五
一个经常存取的表,一定要把它的数据和索引分布在不同的磁盘上吗?
One popular approach to manual I/O distribution suggests separating a frequently used table from its index. This is not correct. During the course of a transaction, the index is read first, and then the table is read. Because these I/Os occur sequentially, the table and index can be stored on the same disk without contention. It is not sufficient to separate a datafile simply because the datafile contains indexes or table data. The decision to segregate a file should be made only when the I/O rate for that file affects database performance.
-- 引用自 <<Oracle Database Performance Tuning Guide>>
2008 年 07 月 06日, 星期日
ORA-12518错误
在做压力测试的时候,出现这个错误,搞了好久,网上也找不到什么好的方法,后来看到这个错误之前有个out of process memory错误,所以,估计是32位系统的内存限制问题,把SGA_TARGET从1.5G改成1G,就没有出现这个问题。基本的经验就是在32位的4G内存的机器上,PGA_AGGREGATE_TARGET + SGA_TARGET不能够超过2G,否则可以支持的连接数就变少。(难道创建连接需要用到额外的系统内存空间?)
查看全文2008 年 05 月 21日, 星期三
常用的Oracle脚本参考
http://www.oracle-base.com/dba/DBACategories.php
2008-05-21:基本上都是一些简单的东西,注释也不错,比较容易看懂,在这个基础上自行总结汇总还是需要的。
2008 年 05 月 11日, 星期日
查看客户端对PGA使用的高水位值(PGA_MAX_MEM超过10M一般都有优化空间)
select s.username, s.machine, trunc(p.pga_used_mem/(1024*1024)) used_memory,trunc(pga_alloc_mem/(1024*1024)) allocated_memory,
trunc(pga_max_mem/(1024*1024)) max_memory
from v$process p, v$session s
where p.addr = s.paddr
order by pga_max_mem desc;
2008 年 05 月 06日, 星期二
数据库统计信息不更新导致的性能问题
SQL如下,系统动态生成的,主要是查看符合多个条件付款对应的所有明细记录。语句本身有问题,因为,多个in可以合并成一个in,相关的条件and起来即可,这个暂时忽略。
SELECT doc.* FROM TLK_PAYMENT_DETAIL doc
WHERE ISTMP=0 AND (((((((ITEM_contractnumber = '06603833'))
AND (doc.parent in (select parentdoc.id from TLK_TEPAYMENT_MAIN parentdoc where parentdoc.ITEM_单据类型 IN ('4','8'))))
AND (doc.parent in (select parentdoc.id from TLK_TEPAYMENT_MAIN parentdoc where parentdoc.ITEM_IsReverse = '0')))
AND (doc.parent in (select parentdoc.id from TLK_TEPAYMENT_MAIN parentdoc where parentdoc.stateint = 1048576)))
AND (doc.parent in (select parentdoc.id from TLK_TEPAYMENT_MAIN parentdoc where parentdoc.istmp <> 1)))
AND ((ITEM_expensecode = '33000100')))
2008 年 04 月 07日, 星期一
更改Oracle文件位置
控制文件,注意参数的设置格式:
startup nomount;
alter system set control_files="xxxxx", "xxxxx" scope=spfile;
数据和日志文件:
startup mount;
alter database rename file "xxxx" to "xxxx";
2008 年 03 月 05日, 星期三
删除数据库表空间
几个必要的步骤:
1 - alter tablespace xxxx offline normal;
2 - drop tablespace xxxx including contents and datafiles
3 - 由于windows版本的oracle不会自动删除文件,需要重启oracle,删除文件。
2007 年 11 月 15日, 星期四
估算undo表空间大小
select (UR*(UPS*DBS))+(DBS*24) as "Bytes"from (select value as UR
from v$parameter
where name='undo_retention'),
(select (sum(undoblks)/sum(((end_time-begin_time)*86400))) as UPS
from v$undostat),
(select value as DBS
from v$parameter
where name='db_block_size');
2007 年 10 月 26日, 星期五
FILE_ID + BLOCK_ID -> TABLE_NAME
-- 通过文件id和block_id确定所属的表
select * from dba_extents
where owner = SCHEMA_NAME
and file_id = 6
and block_id < 605600
and block_id + blocks > 605600;
2007 年 10 月 12日, 星期五
Ora-01632解决办法
alter index xxxxxx STORAGE(MAXEXTENTS UNLIMITED);2007 年 08 月 17日, 星期五
如何改变各种Segment的表空间
1 - alte table TABLE_NAME move tablespace TABLESPACE_NAME
2 - alter index INDEX_NAME rebuild tablespace TABLESPACE_NAME
3 - alter table TABLE_NAME move tablespace TABLESPACE_NAME lob(COLUMN_NAME) store as (tablespace TABLESPACE_NAME)
2007 年 06 月 18日, 星期一
Oracle中关于函数的使用
在一个select语句中使用函数,对性能危害很大,尽可能少用。在where从句中使用,可能需要用到函数索引提高新能。总之,尽量少用。2006 年 12 月 07日, 星期四
Swith Comsumer Group
DBMS.SESSION.SWITCH_CURRENT_CONSUMER_GROUP
2006 年 12 月 05日, 星期二
Create Refresh Group
BEGIN
DBMS_REFRESH.MAKE(
name => '"GOLDENBEAR"."XXXX"',
list => '',
next_date => SYSDATE,
interval => '/*10:Mins*/ sysdate + 10/(60*24)',
implicit_destroy => FALSE,
lax => FALSE,
job => 0,
rollback_seg => NULL,
push_deferred_rpc => FALSE,
refresh_after_errors => TRUE,
purge_option => NULL,
parallelism => NULL,
heap_size => NULL);
END;
/
BEGIN
DBMS_REFRESH.ADD(
name => '"GOLDENBEAR"."XXXX"',
list => '"GOLDENBEAR"."DYNA_DKARE"',
lax => TRUE);
END;
/
BEGIN
DBMS_REFRESH.ADD(
name => '"MVIEWADMIN"."GOLDENBEAR_REFRESH_GROUP"',
list => '"GOLDENBEAR"."T_DYNAFORM_ITEM"',
lax => TRUE);
END;
/
Create DB Link
create public database link "xxxx"connect to "<user>"
identified by "<pwd>"
using 'servicename';
2006 年 11 月 01日, 星期三
打印CLOB的方法
-- 由于每行只能打印255字符的限制,这里每100字符打印一次
-- 所以格式不太好看
CREATE OR REPLACE
procedure PRINT_CLOB (p_clob_in in CLOB) is
v_buffer varchar2(300);
v_buffer_size integer := 100;
v_offset integer := 1;
begin
--
IF p_clob_in is null THEN
return;
END IF;
--
FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(p_clob_in)/v_buffer_size) LOOP
v_buffer :=DBMS_LOB.SUBSTR(p_clob_in, v_buffer_size, v_offset);
dbms_output.put_line(v_buffer);
v_offset := v_offset + v_buffer_size;
END LOOP;
end;
CLOB转换成BLOB的方法
create or replace function clob_to_blob (p_clob_in in clob)return blob
is
v_blob blob;
v_offset integer;
v_buffer_varchar varchar2(32000);
v_buffer_raw raw(32000);
v_buffer_size binary_integer := 32000;
begin
--
if p_clob_in is null then
return null;
end if;
--
DBMS_LOB.CREATETEMPORARY(v_blob, TRUE);
v_offset := 1;
FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(p_clob_in) / v_buffer_size)
loop
dbms_lob.read(p_clob_in, v_buffer_size, v_offset, v_buffer_varchar);
v_buffer_raw := utl_raw.cast_to_raw(v_buffer_varchar);
dbms_lob.writeappend(v_blob, utl_raw.length(v_buffer_raw), v_buffer_raw);
v_offset := v_offset + v_buffer_size;
end loop;
return v_blob;
end clob_to_blob;
BLOB转换成CLOB的方法
create or replace FUNCTION blob_to_clob (p_blob_in IN BLOB)RETURN CLOB
AS
v_clob CLOB;
v_buffer VARCHAR2(32000);
v_buffer_size PLS_INTEGER := 32000;
v_offset PLS_INTEGER := 1;
BEGIN
--
IF p_blob_in is null THEN
RETURN null;
END IF;
--
DBMS_LOB.CREATETEMPORARY(v_clob, TRUE);
-- read the lob using dbms_lob.substr function and
-- utl_raw.cast_to_varchar2 function
FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(p_blob_in) / v_buffer_size)
LOOP
v_buffer := UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(p_blob_in, v_buffer_size, v_offset));
DBMS_LOB.WRITEAPPEND(v_clob, LENGTH(v_buffer), v_buffer);
v_offset := v_offset + v_buffer_size;
END LOOP;
RETURN v_clob;
END blob_to_clob;
2006 年 09 月 04日, 星期一
关于如何计算一个表或索引的剩余空间
以前一直只知道通过DBA_FREE_SPACE计算表空间的剩余空间,不知道某个SEGMENT的剩余表空间。今天看了看Adminisrator's Guide,了解到可以使用DBMS_SPACE.
2008-05-21:眼大看过界,通过dba_tables/dba_indexes可以计算对象的剩余空间。需要先anayze一下,更新统计信息。dbms_space不需要先更新统计信息。
SET CONSTRAINT[S] 语句
SET CONSTRAINT xxxx IMMEDIATE/DEFERRED
SET CONSTRAINTS xxx, yyy, zzz / ALL IMMEDIATE/DEFERRED
切换SCHEMA的语句
ALTER SESSION SET DEFAULT_SCHEMA = xxxxxx2006 年 08 月 31日, 星期四
To Report Exception Rows When Enabling Constraint
alter table tbl_xxx enable primary key pk_xxx exceptions into tbl_yyyy2006 年 08 月 29日, 星期二
手工触发数据库实例向监听器注册自己
在10g中,数据库服务是自动向监听器注册自己的,在一般情况下,数据库实例重启后,PMON60秒内,检测是否应该注册自己。为了加快这个过程,可以使用以下语句:
alter system register
2006 年 07 月 12日, 星期三
2006 年 07 月 06日, 星期四
2006 年 01 月 23日, 星期一
Kill Session
-- session string format 'sid, serial#'
alter system kill session '506,4622'
2006 年 01 月 18日, 星期三
PGA相关统计脚本
-- PGA performance consideration
-- Ideally, the size of a work area is big enough that it
-- can accommodate the input data and auxiliary memory structures
-- allocated by its associated SQL operator. This is known as
-- the optimal size of a work area.
-- When the size of the work area is smaller than optimal,
-- the response time increases, because an extra pass is
-- performed over part of the input data. This is known as
-- the one-pass size of the work area.
-- Under the one-pass threshold, when the size of a work area
-- is far too small compared to the input data size, multiple
-- passes over the input data are needed. This could dramatically
-- increase the response time of the operator. This is known as
-- the multi-pass size of the work area.
-- hit ratio regarding PGA
-- (BP * 100) / (BP + EBP)
-- BP: Bytes processed
-- EBP: Extract bytes processed (through one pass or multiple passes)
select value from v$pgastat where name = 'bytes processed'
select value from v$pgastat where name = 'extra bytes read/written'
select value from v$pgastat where name = 'cache hit percentage'
-- check for % of times that work areas were executed in optimal,
-- one-pass, or multi-pass mode since start-up.
select optimal_count, round(optimal_count*100/total, 2) optimal_perc,
onepass_count, round(onepass_count*100/total, 2) onepass_perc,
multipass_count, round(multipass_count*100/total, 2) multipass_perc
from
(select decode(sum(total_executions), 0, 1, sum(total_executions)) total,
sum(optimal_executions) optimal_count,
sum(onepass_executions) onepass_count,
sum(multipasses_executions) multipass_count
from v$sql_workarea_histogram
where low_optimal_size > 64*1024);
-- to see the active work area
select to_number(decode(sid, 65535, null, sid)) sid,
operation_type operation,
trunc(expected_size/1024) esize,
trunc(actual_mem_used/1024) mem,
trunc(max_mem_used/1024) "max mem",
number_passes pass,
trunc(tempseg_size/1024) tsize
from v$sql_workarea_active
order by 1,2;
Redo log buffer相关统计脚本
-- resonable size of log buffer should be:
-- max(0.5m, (128k * number of cpus))
-- check for statistics related to redo log buffer
select name, value
from v$sysstat
where name = 'redo buffer allocation retries';
Shared pool相关统计脚本
-- check the library cache statistics by name space (type)
select namespace, pins, pinhits, reloads, invalidations
from v$librarycache
order by namespace
-- to caculate the overall hit ratio of libary cache
select sum(pinhits) / sum(pins)
from v$librarycache
-- check for free memory in shared pool
select * from v$sgastat
where name = 'free memory'
and pool = 'shared pool';
-- check for library cache memory allocations
select * from v$library_cache_memory
-- check for data dictionary hit ratio
select parameter
, sum(gets)
, sum(getmisses)
, 100*sum(gets - getmisses) / sum(gets) pct_succ_gets
, sum(modifications) updates
from v$rowcache
where gets > 0
group by parameter;
-- check for overall data dictionary hit ratio
select (sum(gets - getmisses - fixed)) / sum(gets) "row cache"
from v$rowcache;
-- check for reserved shared pool
select * from v$shared_pool_reserved
2006 年 01 月 10日, 星期二
Buffer cache相关统计
-- Formula to calculate the hit ratio
-- hit ratio = 1 - (('physical reads cache') / ('consistent gets from cache' + 'db block gets from cache')
-- Method 1, calculated from v$sysstat, for overall buffer caches/pools
select name, value
from v$sysstat
where name in ('db block gets from cache', 'consistent gets from cache',
'physical reads cache');
-- method 2, calculate from v$buffer_pool_statistics, for separated buffer cache/pool
select name, physical_reads, db_block_gets, consistent_gets,
1 - (physical_reads / (db_block_gets + consistent_gets)) "hit ratio"
from v$buffer_pool_statistics;
-- check how many pools are configure in the database
select name, block_size, sum(buffers)
from v$buffer_pool
group by name, block_size
having sum(buffers) > 0;
-- check he to see what is the object id related to each block
select * from v$bh
-- check how many blocks of a segment residing in data cache
select o.object_name, count(*) number_of_blocks
from dba_objects o, v$bh bh
where o.data_object_id = bh.objd
and o.owner != 'sys'
group by o.object_name
order by count(*);