is
v_crt_ind_sql varchar2(4000) := '';
v_drop_ind_sql varchar2(4000) := '';
v_ind_name varchar2(100) := '';
v_tgt_ind_name varchar2(100) := '';
v_col_list varchar2(1000) := '';
v_col_name varchar2(100) := '';
i_ind_col_cnt number(2,0) := 0;
i_tbl_cnt number(2,0) := 0;
i_ind_cnt number(2,0) := 0;
v_step_no varchar2(3) :='';
begin
--step 1 检查索引定义是否存在
v_step_no :='1';
select count(1) into i_ind_col_cnt from all_ind_columns
where table_name = p_src_tbl_name;
if(i_ind_col_cnt = 0 ) then
sp_log('sp_crt_stg_index',v_step_no,'3',p_tx_date,p_src_tbl_name||'不存在索引定义');
return;
end if;
--step 2 检查目标表是否存在
v_step_no :='2';
select count(1) into i_tbl_cnt from tabs
where table_name = p_tgt_tbl_name;
if(i_tbl_cnt = 0 ) then
sp_log('sp_crt_stg_index',v_step_no,'3',p_tx_date,p_src_tbl_name||'不存在');
return;
end if;
--step 3 创建索引
v_step_no :='3';
for cur_ind in (select index_name from all_indexes where table_name = p_src_tbl_name) loop
v_ind_name := cur_indindex_name;
v_col_name := '';
v_col_list := '(';
for cur_ind_col in (select column_name from all_ind_columns where index_name = v_ind_name order by column_position) loop
v_col_name := cur_ind_colcolumn_name;
v_col_list := v_col_list||v_col_name||',';
end loop;
v_col_list := v_col_list||')';
v_col_list := replace(v_col_list,',)',')');
v_tgt_ind_name :=replace(v_ind_name,p_src_tbl_name,p_tgt_tbl_name);
v_tgt_ind_name := v_tgt_ind_name||substr(p_tx_date,7,21);
select count(1) into i_ind_cnt from all_indexes where index_name = v_tgt_ind_name;
if(i_ind_cnt > 0) then
v_drop_ind_sql := 'drop index '||v_tgt_ind_name;
execute immediate v_drop_ind_sql;
end if;
sp_log('sp_crt_stg_index',v_step_no,'1',p_tx_date,v_tgt_ind_name||'创建开始');
v_crt_ind_sql := 'create index '||v_tgt_ind_name||' on '||p_tgt_tbl_name||v_col_list||' nologging';
if(v_crt_ind_sql is not null) then
execute immediate v_crt_ind_sql;
end if;
sp_log('sp_crt_stg_index',v_step_no,'1',p_tx_date,v_tgt_ind_name||'创建结束');
end loop;
exception
when others then
sp_log('sp_crt_stg_index',v_step_no,'3',p_tx_date,v_tgt_ind_name||'创建异常:'||SQLERRM);
end;
#########################
---- 日期类型转换
to_char(sysdate,'YYYY-MM-DD HH24:MI:SS');
----Oracle检查分区
select count(1)
from ALL_TAB_PARTITIONS
where table_name = p_tgt_tbl_name
and PARTITION_NAME = v_prt_name;
----码表
Select From ict_s_dic Where opttype = 'ID_TYPE';
decode 内部字段拼接
decode(Date_Nextvisit,'','下次跟进时间:['||Date_Nextvisit||']') RMK1,
----拼接字段,删除表
SELECT 'DROP TABLE '||TABLE_NAME||';' FROM TABS WHERE TABLE_NAME LIKE 'M_ICT%';
Select From tabs ;
----交换分区,分区交换后数据是否交换?
'ALTER TABLE '|| p_tgt_tbl_name ||' EXCHANGE PARTITION '||v_prt_name||' WITH TABLE '||p_src_tbl_name;
----查询某表是否存在
select count(1) from tabs
where table_name = p_src_tbl_name;
----重建索引
ALTER index ind_id_idx rebuild
----创建索引nologging
create index I_ICT_CUST_INFO__0 on ICT_CUST_INFO_20141222 (CUST_NO)
nologging;
----查询分区内数据
select count(1) from stg_ict_trade_info partition(ICT_PRT_2014005);
Select From User_Ind_Partitions;
Select From User_Part_Indexes;
-----查询索引
select index_name from ALL_INDEXES WHERE TABLE_NAME=p_src_tbl_name;
-----查询分区
select from ALL_TAB_PARTITIONS
where table_name = p_tgt_tbl_name and PARTITION_NAME = v_prt_name;
-----查询表名
select from tabs where table_name = p_tgt_tbl_name;
-----清空表分区数据
'ALTER TABLE '||p_tgt_tbl_name||' TRUNCATE PARTITION ' || v_prt_name;
-----增加表分区
'ALTER TABLE '||p_tgt_tbl_name||' ADD PARTITION ' || v_prt_name||' VALUES LESS THAN (''' ||v_monthend||''') TABLESPACE ICLIENT_O_DATA01 ';
-----重建索引
'ALTER INDEX '||cur_indindex_name|| ' REBUILD PARALLEL 128 COMPUTE STATISTICS NOLOGGING';
-----交换分区
'ALTER TABLE '|| p_tgt_tbl_name ||' EXCHANGE PARTITION '||v_prt_name||' WITH TABLE '||p_src_tbl_name||' INCLUDING INDEXES';
-----oralce创建同义词------------
create or replace public SYNONYM ICT_ORG_BPH for iclientodataICT_ORG_BPH;
-----oracle 赋权限
grant select, insert, update, delete on ICT_ORG_BPH to ICLIENTOOPR; --赋权限
---------------DBA查看表空间------------
select atablespace_name,
abytes / 1024 / 1024 / 1024 "Sum G",
(abytes - bbytes) / 1024 / 1024 / 1024 "used G",
bbytes / 1024 / 1024 / 1024 "free G",
round(((abytes - bbytes) / abytes) 100, 2) "percent_used"
from (select tablespace_name, sum(bytes) bytes
from dba_data_files
group by tablespace_name) a,
(select tablespace_name, sum(bytes) bytes, max(bytes) largest
from dba_free_space
group by tablespace_name) b
where atablespace_name = btablespace_name
order by ((abytes - bbytes) / abytes) desc
-------------------------------------------------------------------------------------
--oracle 生成删表语句,
SELECT 'DROP TABLE '||TABLE_NAME||';' FROM TABS WHERE TABLE_NAME LIKE 'M_ICT%'
--查询oracle 中ICT开头的
SELECT FROM tabs WHERE TABLE_NAME LIKE 'ICT%'
----oracle 表分析语句
'ANALYZE TABLE ' || v_ana_tbl_name || ' estimate system statistics';
例如:ANALYZE TABLE ICT_SUM_AST_DBT_CUST ESTIMATE SYSTEM STATISTICS;
-----------查看锁定对象及会话
SELECT OBJECT_NAME,MACHINE,SSID,SSERIAL#
FROM GV$LOCKED_OBJECT I,DBA_OBJECTS O,GV$SESSION S
WHERE IOBJECT_ID=OOBJECT_ID AND ISESSION_ID=SSID;
----------oralce解锁
ALTER SYSTEM KILL SESSION '280,219';
授权脚本生成方法:
select 'grant select on table dmccrm'||tbl_name||' to public,ex_sdbods; ' from t_ict_tbl_type where tbl_type in('ALL','GP') and ETL_DIR IN( 'GP->ORACLE','无需同步');
-----------oracle批量生成同义词
SELECT 'create or replace public synonym '||table_name ||' for iclientodata'||table_name FROM tabs WHERE table_name like 'ICT_%'
-----------oracle批量生成修改表字段的长度
select 'ALTER TABLE '||TABLE_NAME||' MODIFY '||COLUMN_NAME||' NUMBER(30,8);' from cols t
where tDATA_TYPE = 'NUMBER'
AND TDATA_SCALE >0 AND SUBSTR(TABLE_NAME,-8,8)<>'20140531'
---------oracle 批量创建同义词
select 'create or replace public synonym '||table_name||' for iclientodata'||table_name||';' from user_tables WHERE table_name LIKE 'ICT_%' AND table_name NOT LIKE '%20140531';
-----------oracle 交换分区语句
ALTER TABLE ICT_CUST_LEVEL_HIS ADD PARTITION ICT_PRT_20140531 VALUES LESS THAN ('2014-06-01')
--------MERGE使用方法
MERGE INTO ICT_CUST_INFO_ALL a
USING
(
SELECT tCUST_MNG_UM_NO ,tCUST_NO
FROM ict_cust_mnger_rel t
WHERE tCUST_NO IN
('600021562650', '600037441214', '600036874754', '600038507516',
'600020226746', '600038089420', '600041030403', '600038952992',
'600039468303')
)b
ON( acust_no= bcust_no)
WHEN MATCHED THEN
UPDATE SET aMAX_ASSET_INTRO_NO=bCUST_MNG_UM_NO;
-------------oracle 树形查询,查询机构编号为‘9902’的和其下级子机构
select org_id,org_name,org_level from (
SELECT rownum rn, ioiorg_id , ioiorg_name,ioiorg_level
FROM ict_org_info ioi
START WITH ioiorg_id ='9902'
CONNECT BY PRIOR ioiorg_id = ioiparent_org_id_b
order by ioiorg_level desc
) where rn=1
-------------oracle 树形查询,查询机构编号为‘9902’的和上级机构
select org_id,org_name,org_level from (
SELECT rownum rn, ioiorg_id , ioiorg_name,ioiorg_level
FROM ict_org_info ioi
START WITH ioiorg_id ='9902'
CONNECT BY ioiorg_id = PRIOR ioiparent_org_id_b
order by ioiorg_level desc
) where rn=1
-------------oracle 树形查询,查询机构编号为‘9902’的上级机构
select org_id,org_name,org_level from (
SELECT rownum rn, ioiorg_id , ioiorg_name,ioiorg_level
FROM ict_org_info ioi
START WITH ioiorg_id ='9902'
CONNECT BY ioiorg_id = PRIOR ioiparent_org_id_b
order by ioiorg_level desc
) where rn=1
----------查看表名与表空间
Select From user_tables Where table_name = 'ICT_RMT_APPO';
Select From User_Tablespaces;
----------查询SQL 预估时间
SELECT SESID,
OPNAME,
TRUNC(SOFAR / TOTALWORK 100, 2) || '%' AS PCT_WORK,
ELAPSED_SECONDS ELAPSED,
ROUND(ELAPSED_SECONDS (TOTALWORK - SOFAR) / SOFAR) REMAIN_TIME,
SQL_TEXT
FROM V$SESSION_LONGOPS SL, V$SQLAREA SA, V$SESSION SE
WHERE SLSQL_HASH_VALUE = SAHASH_VALUE
AND SLSID = SESID
AND SOFAR != TOTALWORK
ORDER BY START_TIME;
------TYPE opty_cur IS REF CURSOR;
整体的意思是“创建一个类型变量cur,它引用游标”,除了cur外,其余全是关键字。
TYPE cur:定义类型变量 ,is ref cursor:相当于数据类型,不过是引用游标的数据类型。
这种变量通常用于存储过程和函数返回结果集时使用,
因为PL/SQL不允许存储过程或函数直接返回结果集,
但可以返回类型变量,于是引用游标的类型变量作为输出参数或返回值就应运而生了。
----查杀进程
SELECT dobOBJECT_NAME Table_Name,
loLOCKED_MODE,
loSESSION_ID,
vssSERIAL#,
vpsspid,
vssaction Action,
vssosuser OSUSER,
vssprocess AP_PID,
VPSSPID DB_PID,
'alter system kill session ' || '''' || loSESSION_ID || ',' ||
vssSERIAL# || ''';' kill_command
from v$locked_object lo, dba_objects dob, v$session vss, V$PROCESS VPS
where loOBJECT_ID = dobOBJECT_ID
and loSESSION_ID = vssSID
AND VSSpaddr = VPSaddr
order by 2, 3, DOBobject_name;
select if_rows,if_rows_len,del_id_rows_len from index_stats;
1 应该是可行的, 具体 会不会节省时间 试一下就可以了。
2 大概每个月存储四五十万的数据,里面只保存最新四个月的数据
每次create这7个索引用时都特别长,大概需要三四个小时;
200万的数据,重建索引花费的时间太长了;很奇怪。
3 估计之前的 先drop掉索引,然后插入数据完毕后create索引 也是为了避免 插入数据时,索引对插入效率的影响。1、重新收集统计信息
analyze table 表名 compute statistics for table for all indexes for all indexed columns
analyze index 索引名 compute statistics
2、把表现不同的sql及其执行计划发上来看看
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)