Monday, March 21, 2011
Track Database Growth
Thus, tracking the consumption of disk space is one of the frequent tasks in administrative checklist. Here is a SQL script that gives you the current size of database and average disk space consumed in each day.
SELECT b.tsname tablespace_name
, MAX(b.used_size_mb) cur_used_size_mb
, round(AVG(inc_used_size_mb),2)avg_increas_mb
FROM (
SELECT a.days,a.tsname
, used_size_mb
, used_size_mb - LAG (used_size_mb,1) OVER ( PARTITION BY a.tsname ORDER BY a.tsname,a.days) inc_used_size_mb
FROM (
SELECT TO_CHAR(sp.begin_interval_time,'MM-DD-YYYY') days
,ts.tsname
,MAX(round((tsu.tablespace_usedsize* dt.block_size )/(1024*1024),2)) used_size_mb
FROM DBA_HIST_TBSPC_SPACE_USAGE tsu
, DBA_HIST_TABLESPACE_STAT ts
,DBA_HIST_SNAPSHOT sp, DBA_TABLESPACES dt
WHERE tsu.tablespace_id= ts.ts# AND tsu.snap_id = sp.snap_id
AND ts.tsname = dt.tablespace_name AND sp.begin_interval_time > sysdate-7
GROUP BY TO_CHAR(sp.begin_interval_time,'MM-DD-YYYY'), ts.tsname
ORDER BY ts.tsname, days
) a
) b GROUP BY b.tsname ORDER BY b.tsname;
Wednesday, January 26, 2011
ORA-14519 Conflicting tablespace blocksizes while importing partition tables
ORA-14519: Conflicting tablespace blocksizes for table : Tablespace T2 block size 16384 [partition specification] conflicts with previously specified/implied tablespace T1 block size 8192 [object-level default]
Well :) the error massage clearly indicate that I am trying to create a partition table in more than one tablespaces where the blocksize of the tablespaces are different. We all know that table partitions can not be placed in different blocksize tablespaces. Though I know this and not did this mistake before, I get this error when I am trying to make a replica schema in local test database.
I took dump by using EXPDP without any error and get the above error when try to import it by using REMAP_SCHEMA option of IMPDP. It is really annoying to get such error that can not be explainable. All of my table partitions are placed in a single tablespace but the error message says that I am trying to place table partitions in two tablespaces where their blocksize is different ! It sounds simply horrible.
But at the end of a long research :) I have found the mistake.The user's default tablespace is T1 [8k blocksize] and I create the table in tablespace T2 [16K blocksize] just like this -
CREATE TABLE TEST (ID NUMBER (10), TEXT VARCHAR2(50)
) PARTITION BY RANGE (ID) (
PARTITION TEST_PART_1 VALUES LESS THAN (100001) TABLESPACE T2 ,
PARTITION "TEST_PART_2" VALUES LESS THAN (MAXVALUE) TABLESPACE T2
);
This script create 2 table partitions of TEST table in T2 tablespace but unfortunately the default tablespace attribute of TEST table set to T1 (As I did not mansion default table space of table TEST when I create it. So ORACLE set this attribute same as user's default tablespace). See! Sometime ORACLE does something smartly that can cause stupid scenario. IF you want to see this unbelievable thing, just generate the table's SQL. You will see this
CREATE TABLE TEST (ID NUMBER (10),TEXT VARCHAR2(50)
) TABLESPACE T1
PARTITION BY RANGE (ID) (
PARTITION TEST_PART_1 VALUES LESS THAN (100001) TABLESPACE T2 ,
PARTITION "TEST_PART_2" VALUES LESS THAN (MAXVALUE) TABLESPACE T2
);
This sturdiness of oracle cause ORA-14519 when I tryed to import TEST table because oracle can't create a partition table in different blocksize tablespaces.
So The solution is simple; mention default tablespace when you create a partition table. Just like this
CREATE TABLE TEST (
ID NUMBER (10),
TEXT VARCHAR2(50)
) TABLESPACE T2
PARTITION BY RANGE (ID) (
PARTITION TEST_PART_1 VALUES LESS THAN (100001) TABLESPACE T2 ,
PARTITION "TEST_PART_2" VALUES LESS THAN (MAXVALUE) TABLESPACE T2
);
[ It is not mandatory that all tablespace should be same (t2 in this case). You can put 3 different tablespace but their blocksize should be same.]
But, what is the solution for already created partition tables ? Well, the solution is very simple, just change the default tablespace attribute of your partition tables that are already created. Here is a script to identify the problematic tables.
SELECT * FROM (
SELECT x.table_name, x.def_tablespace_name
, (SELECT block_size from user_tablespaces where tablespace_name=x.def_tablespace_name) df
, x.tablespace_name
, (SELECT block_size from user_tablespaces where tablespace_name = x.tablespace_name) tb
FROM (
SELECT distinct t.table_name, t.def_tablespace_name, p.tablespace_name
FROM user_part_tables t, user_tab_partitions p
WHERE t.table_name = p.table_name
) x ) k WHERE k.tb <> k.df;
Then reset the default tablespace attribute
ALTER TABLE TEST MODIFY default attributes TABLESPACE T2;
Now take dump and apply it without facing ORA-14519 !