普通表转化成分区表的高效脚本
通用转换存储过程
查询未建分区的大表脚本
1 prompt <p>当前用户下,表大小超过10个GB未建分区的 2 select segment_name, 3 segment_type, 4 sum(bytes) / 1024 / 1024 / 1024 object_size 5 from user_segments 6 WHERE segment_type = ‘TABLE‘ 7 group by segment_name, segment_type 8 having sum(bytes) / 1024 / 1024 / 1024 >= 10 9 order by object_size desc;
sql
查询失效的分区表索引
1 prompt <p>查询当前用户下,失效-普通索引 2 select t.index_name, 3 t.table_name, 4 blevel, 5 t.num_rows, 6 t.leaf_blocks, 7 t.distinct_keys 8 from user_indexes t 9 where status = ‘INVALID‘; 10 11 prompt <p>查询当前用户下的失效-分区索引 12 select t1.blevel, 13 t1.leaf_blocks, 14 t1.INDEX_NAME, 15 t2.table_name, 16 t1.PARTITION_NAME, 17 t1.STATUS 18 from user_ind_partitions t1, user_indexes t2 19 where t1.index_name = t2.index_name 20 and t1.STATUS = ‘UNUSABLE‘;
查询分区表各分区大小严重不均匀情况
1 --以下脚本可以分析分区表记录不平衡的情况,同时也可以从侧面发现由于疏于管理,大量当前数据进入默认分区的情况 2 /* 3 4 注:这个语句不一定准确,尤其是在表未及时收集统计信息的时候 5 6 */ 7 8 --统计信息系统一般会自动收集,这只是首次建成表后需要操作一下,以方便测试 9 exec dbms_stats.gather_table_stats(ownname => ‘LJB‘,tabname => ‘RANGE_PART_TAB‘,estimate_percent => 10,method_opt=> ‘for all indexed columns‘,cascade=>TRUE) ; 10 11 12 --当前用户下,某个分区的记录数是平均记录数的2倍以上 13 set linesize 266 14 col table_name format a20 15 select table_name, 16 max(num_rows), 17 trunc(avg(num_rows),0), 18 sum(num_rows), 19 case when sum(num_rows),0 then 0,else trunc(max(num_rows) / sum(num_rows),2) end, 20 count(*) 21 from user_tab_partitions 22 group by table_name 23 having max(num_rows) / sum(num_rows) > 2 / count(*); 24 25 --也可用来作为判断查询当前用户下有因为疏于分区管理导致无大量数据进了建默认分区的参考。 26 27 select table_name, 28 partition_name, 29 num_rows 30 from user_tab_partitions 31 where table_name = ‘RANGE_PART_TAB‘ 32 order by num_rows desc; 33 34 35 --请试验<分区类型_范围分.sql>后,体会上述脚本
查询分区数过多的表
1 prompt <p>当前用户下分区最多的前10个对象 2 select * 3 from (select table_name, count(*) cnt 4 from user_tab_partitions 5 group by table_name 6 order by cnt desc) 7 where rownum <= 10; 8 9 prompt <p>当前用户下分区个数超过100个的表 10 select table_name, count(*) cnt 11 from user_tab_partitions 12 having count(*) >= 100 13 group by table_name, table_name 14 order by cnt desc; 15 16 --或者如下更方便 17 select table_name, partitioning_type, subpartitioning_type 18 from user_part_tables 19 where partition_count > 100;
查询当前有多少带子分区的分区表
1 select table_name, 2 partitioning_type, 3 subpartitioning_type, 4 partition_count 5 from user_part_tables 6 where subpartitioning_type <> ‘NONE‘; 7 8 9 select count(*) from user_part_tables where subpartitioning_type <> ‘NONE‘;
查询表中有没有过时类型的字段
1 select table_name, 2 column_name, 3 data_type 4 from user_tab_columns 5 where data_type in ( ‘LONG‘,‘CHAR‘); 6 7 8 9 10 11 12 13 14 15 16 17 ------------------------------------------------------------------------------------ 18 19 drop table t_long purge; 20 create table t_long (id int, name long); 21 drop table t_char purge; 22 create table t_char (id int, address char(10)); 23 set linesize 266 24 col table_name format a25 25 col column_name format a25 26 col data_type format a20 27 28 SQL> select table_name, 29 2 column_name, 30 3 data_type 31 4 from user_tab_columns 32 5 where data_type in ( ‘LONG‘,‘CHAR‘); 33 34 TABLE_NAME COLUMN_NAME DATA_TYPE 35 ------------------------- ---------------------------------- 36 T_CHAR ADDRESS CHAR 37 T_LONG NAME LONG
查询哪些外键未建索引
1 --查看当前数据库哪些对象外键没建索引 2 select table_name, 3 constraint_name, 4 cname1 || nvl2(cname2, ‘,‘ || cname2, null) || 5 nvl2(cname3, ‘,‘ || cname3, null) || 6 nvl2(cname4, ‘,‘ || cname4, null) || 7 nvl2(cname5, ‘,‘ || cname5, null) || 8 nvl2(cname6, ‘,‘ || cname6, null) || 9 nvl2(cname7, ‘,‘ || cname7, null) || 10 nvl2(cname8, ‘,‘ || cname8, null) columns 11 from (select b.table_name, 12 b.constraint_name, 13 max(decode(position, 1, column_name, null)) cname1, 14 max(decode(position, 2, column_name, null)) cname2, 15 max(decode(position, 3, column_name, null)) cname3, 16 max(decode(position, 4, column_name, null)) cname4, 17 max(decode(position, 5, column_name, null)) cname5, 18 max(decode(position, 6, column_name, null)) cname6, 19 max(decode(position, 7, column_name, null)) cname7, 20 max(decode(position, 8, column_name, null)) cname8, 21 count(*) col_cnt 22 from (select substr(table_name, 1, 30) table_name, 23 substr(constraint_name, 1, 30) constraint_name, 24 substr(column_name, 1, 30) column_name, 25 position 26 from user_cons_columns) a, 27 user_constraints b 28 where a.constraint_name = b.constraint_name 29 and b.constraint_type = ‘R‘ 30 group by b.table_name, b.constraint_name) cons 31 where col_cnt > ALL 32 (select count(*) 33 from user_ind_columns i 34 where i.table_name = cons.table_name 35 and i.column_name in (cname1, cname2, cname3, cname4, cname5, 36 cname6, cname7, cname8) 37 and i.column_position <= cons.col_cnt 38 group by i.index_name) 39 40 41 查询所有含外键的表 42 43 select count(*),TABLE_NAME,c_constraint_name from ( 44 select a.table_name, 45 substr(a.constraint_name, 1, 30) c_constraint_name, 46 substr(a.column_name, 1, 30) column_name, 47 position, 48 b.owner, 49 b.constraint_name, 50 b.constraint_type 51 from user_cons_columns a, user_constraints b 52 where a.constraint_name = b.constraint_name 53 and b.constraint_type = ‘R‘ ) 54 group by TABLE_NAME,c_constraint_name 55 56
时间: 2024-10-20 18:21:57