数据库

本类阅读TOP10

·SQL语句导入导出大全
·SQL Server日期计算
·SQL语句导入导出大全
·SQL to Excel 的应用
·Oracle中password file的作用及说明
·MS SQLServer OLEDB分布式事务无法启动的一般解决方案
·sqlserver2000数据库置疑的解决方法
·一个比较实用的大数据量分页存储过程
·如何在正运行 SQL Server 7.0 的服务器之间传输登录和密码
·SQL中两台服务器间使用连接服务器

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
oracle 使用杂记1

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

一直学习oracle 希望能和大家共同探讨问题 如有不对之处还请指出

index 种对null的使用
================================================================

有些情况可以用 " N/A " 代替 NULL

================================================================

8i 以上使用基于函数的index 可以用上 null

create table t (n number);
create index ind_n on t(n,1);   //  用 t(n,'a') 更省空间

select v from t where n is null;

V
--------------------
lg

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=614 Bytes=6140)
   1    0   TABLE ACCESS (BY INDEX ROWID) OF 'T' (Cost=3 Card=614 Bytes=6140)
   2    1     INDEX (RANGE SCAN) OF 'IND_N' (NON-UNIQUE) (Cost=3 Card=614)

要记住用 CBO
I doesn't need query rewrite to make that leap, it is a "safe" operation.

==============================================================

null 可以在 bitmap index 中使用

==============================================================

或者象下面这样使用多列组合的index 方便使用index

create table t ( f_seq int, t_seq int, x char(1) );
create index t_idx on t(f_seq,t_seq);

select f_seq, t_seq from t where f_seq > 0 and t_seq is null;

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE
   1    0   INDEX (RANGE SCAN) OF 'T_IDX' (NON-UNIQUE)

select f_seq, t_seq, x from t  where f_seq > 0 and t_seq is null;

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE
   1    0   TABLE ACCESS (BY INDEX ROWID) OF 'T'
   2    1     INDEX (RANGE SCAN) OF 'T_IDX' (NON-UNIQUE)

===============================================================

 

 

                                           表所占空间的大小   

select segment_name, round(blocks*8/1024, 0) table_size from user_segments where segment_type='TABLE';
---- 你可以知道你的表的实际size (单位: M)

-----------------------------------------------------------------------------------------

数据字典表DBA_TABLES、ALL_TABLES、USER_TABLES

select table_name,initial_extent,next_extent,min_extents,max_extents,pct_increase
from user_tables;

-----------------------------------------------------------------------------------------

分析一对象实际使用的块
     analyze table lg.t compute statistics;

分析完后就可以看一对象实际使用的块
    select blocks,num_rows,empty_blocks,avg_space,avg_row_len
                   from dba_tables where owner='LG' and table_name='T';
select table_name, round(avg_row_len*num_rows/1024/1024, 0) data_size from user_tables;
---- 你可以知道表中大约的data size (单位: M)
-----------------------------------------------------------------------------------------

select count(distinct substr(rowid,1,15)) "how many use of block" from a;

这是看一个表真正使用了多少数据块 dba_tables 的 blocks 显示了 HWM 下不包含行的块的数目


-----------------------------------------------------------------------------------------

declare
   l_total_blocks        number;
   l_total_bytes         number;
   l_unused_blocks       number;
   l_unused_bytes        number;
   l_LastUsedExtFileId         number;
   l_LastUsedExtBlockId        number;
   l_last_used_block           number;
   l varchar2(128);
   t varchar2(128);
begin
   l:=upper('&name');
   select object_type into t from user_objects where object_name=l;
   dbms_space.unused_space(
      segment_owner     =>USER,
   segment_name      =>l,
   segment_type      =>t,
   partition_name    => null,
            total_blocks      => l_total_blocks,
            total_bytes       => l_total_bytes,
            unused_blocks     => l_unused_blocks,
            unused_bytes      => l_unused_bytes,
         last_used_extent_file_id => l_LastUsedExtFileId,
            last_used_extent_block_id => l_LastUsedExtBlockId,
            last_used_block => l_last_used_block  );
    dbms_output.put_line(RPAD(('total_blocks: '||l_total_blocks),40,' ')||'total_bytes: '||l_total_bytes);
    dbms_output.put_line(RPAD(('unused_blocks: '||l_unused_blocks),40,' ')||'unused_bytes: '||l_unused_bytes);
    dbms_output.put_line(RPAD(('last_used_extent_file_id: '||l_LastUsedExtFileId),40,' ')|| 'last_used_extent_block_id: '||l_LastUsedExtBlockId);
    dbms_output.put_line('last_used_block: '||l_last_used_block);
end;
/

 

                                           sequence

<<  Oracle9i Database Administrator's Guide  >>  20

if your application can never lose sequence numbers, then you cannot use Oracle sequences
and you may choose to store sequence numbers in database tables.

CREATE SEQUENCE  // 需要的系统权限

create sequence lg_sequence
      start with 1
      increment by 1
      order        //保证每个序列值都比先前的大, ********在并行服务中有用
      nocycle;     //防止循环又回到初始值
      NOCACHE NOORDER;
     
默认cache 为 20 直接 shutdown abort 后在内存中缓存的序列就会消失
        startup后从上次shutdown以前的 sys.seq$ 的HIGHWATER 的值开始

      最大值1.0E+27  1后面27个零
      lg_sequence.nextval
      lg_sequence.currval
     
     
alter sequence lg_sequence         //  alter sequence squ_1 increment by trunc(9999999/2);
     increment by 997;    //如果序列之前是2,这样一改就是999
          //是逐渐在原有的基础上涨的

oracle   不支持复制 sequence

-------------------------------    exp sequence    --------------------------------------

sequences are objects -- just like a table, procedure, view, package, etc.
       要exp sequence 那就 export a database or schema, that will get the sequences.

或者    select 'create sequence ' || sequence_name || ' start with ' || last_number+1 ||
        ';' from user_sequences where.....;

 

由于是杂记 想到什么就写的什么 可能有点乱 请大家多包涵




相关文章

相关软件