Google it ....

Showing posts with label oracle database. Show all posts
Showing posts with label oracle database. Show all posts

Friday, March 15, 2019

How to find BLOB/CLOB columns size in Oracle tables

Here I'll show you how to calculate blob, clob columns sizes in tables.
1) Find the size of individual rows with the CLOB/BLOB column. DBMS_LOB.GETLENGTH returns the number of characters (bytes) in the CLOB/BLOB column.
SELECT DBMS_LOB.GetLength("BLOB_COLUMN") / 1024 / 1024 AS SizeMB
  FROM SCHEMA_NAME.TABLE_NAME_WITH_BLOB;

2) find the size of all rows with BLOB/CLOB for particular tables
SELECT SUM(DBMS_LOB.GetLength("BLOB_COLUMN")) / 1024 / 1024 AS SizeMB
  FROM SCHEMA_NAME.TABLE_NAME_WITH_BLOB

3) If you want to find all BLOB/CLOB sizes in your oracle database for a particular schema here is code:
DECLARE
    v_Size NUMBER := 0;
    v_TotalSize NUMBER := 0;
BEGIN
    FOR v_Rec IN (
                  SELECT OWNER || '.' || TABLE_NAME || '.' || COLUMN_NAME AS TableAndColumn,
                     'SELECT SUM(DBMS_LOB.GetLength("' || COLUMN_NAME || '"))/1024/1024 AS SizeMB FROM ' || OWNER || '.' || TABLE_NAME AS sqlstmt
                  FROM DBA_TAB_COLUMNS
                  WHERE DATA_TYPE LIKE '_LOB'
                        AND OWNER LIKE '%SCHEMA_NAME%' -- SCHEMA_NAME
                        AND TABLE_NAME NOT LIKE 'BIN$%' -- EXCLUDE DELETED TABLES FROM RECYCLE_BIN
                 )
    LOOP
        EXECUTE IMMEDIATE v_Rec.sqlstmt INTO v_Size;
 
        DBMS_OUTPUT.PUT_LINE (v_Rec.TableAndColumn || ' size in MB is ' || ROUND(NVL(v_Size,0),2));
        v_TotalSize := v_TotalSize + NVL(v_Size,0);
    END LOOP;
 
    DBMS_OUTPUT.PUT_LINE ('Total size in MB is ' || ROUND(v_TotalSize,2));
END;

Tuesday, January 9, 2018

How to change initrans on table for avoiding enq: TX – allocate ITL entry

In this blog post I will show you how to change initrans on table in Oracle Database without recreate table, for tuning enq: TX – allocate ITL entry waits in database.

First of all what is initrans? it is initial number of concurrent entries allocated within each data block allocated to the database object, this value can range from 1 to 255 and default is 1 with following exceptions:
  • The default INITRANS value for a cluster is 2 or the default INITRANS value of the tablespace in which the cluster resides, whichever is greater.
  • The default value for an index is 2.

Each transaction that updates a block requires a transaction entry in the block. This parameter ensures that a minimum number of concurrent transactions can update the block and helps avoid the overhead of dynamically allocating a transaction entry.

1) check if table have child tables with pk-fk relationship, if table have child table(s) then consider to change initrans also for child table(s)
select *
  from dba_constraints a, dba_constraints b
 where a.constraint_name = b.r_constraint_name
   and a.table_name = 'TABLE_NAME';
2) check if Table have indexes, then you must rebuild indexes, and also change initrans for indexes.
commands will be like:
ALTER TABLE TABLE_NAME MOVE INITRANS 10;
ALTER INDEX TABLE_NAME_PK REBUILD INITRANS 20;
ALTER INDEX TABLE_NAME_I1 REBUILD INITRANS 20;
......

3) check invalid objects and compile if any.

Let's demonstrate small demo :
[oracle@testdb ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Jan 9 16:42:12 2018

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

SQL> set linesize 500
SQL> select *
  from dba_constraints a, dba_constraints b
 where a.constraint_name = b.r_constraint_name
   and a.table_name = 'TABLE_NAME';

no rows selected

SQL> select table_name, INI_TRANS
  from dba_tables
 where table_name = 'TABLE_NAME';

TABLE_NAME                      INI_TRANS
------------------------------ ----------
TABLE_NAME                              1

SQL> SELECT table_name, index_name, status, ini_trans
  FROM DBA_INDEXES
 WHERE TABLE_NAME = 'TABLE_NAME'; 

TABLE_NAME                     INDEX_NAME                     STATUS    INI_TRANS
------------------------------ ------------------------------ -------- ----------
TABLE_NAME                     TABLE_NAME_PK                  VALID             2
TABLE_NAME                     TABLE_NAME_I1                  VALID             2

SQL> ALTER TABLE TABLE_NAME MOVE INITRANS 10;

Table altered.

SQL> select table_name, INI_TRANS
  from dba_tables
 where table_name = 'TABLE_NAME';

TABLE_NAME                      INI_TRANS
------------------------------ ----------
TABLE_NAME                             10

SQL> SELECT table_name, index_name, status, ini_trans
  FROM DBA_INDEXES
 WHERE TABLE_NAME = 'TABLE_NAME';

TABLE_NAME                     INDEX_NAME                     STATUS    INI_TRANS
------------------------------ ------------------------------ -------- ----------
TABLE_NAME                     TABLE_NAME_PK                  UNUSABLE          2
TABLE_NAME                     TABLE_NAME_I1                  UNUSABLE          2

SQL> ALTER INDEX TABLE_NAME_PK REBUILD INITRANS 20;

Index altered.

SQL> ALTER INDEX TABLE_NAME_I1 REBUILD INITRANS 20;

Index altered.

SQL> SELECT table_name, index_name, status, ini_trans
  FROM DBA_INDEXES
 WHERE TABLE_NAME = 'TABLE_NAME';

TABLE_NAME                     INDEX_NAME                     STATUS    INI_TRANS
------------------------------ ------------------------------ -------- ----------
TABLE_NAME                     TABLE_NAME_PK                  VALID            20
TABLE_NAME                     TABLE_NAME_I1                  VALID            20

SQL>


Thursday, September 14, 2017

How to use vi editor in SQL PLUS

Here I'll show you how to use vi editor in SQL PLUS, It is very easy and comfortable when working with SQLPLUS and writing scripts.
vi is a screen-oriented text editor originally created for the Unix operating system.

Tempororay define editor in session level

SQL> define_editor=vi
SQL> select * from duall;
select * from duall
              *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> ed
Wrote file afiedt.buf

  1* select * from dual
SQL> /

D
-
X

SQL>

To make permanent above settings for SQLPLUS utility you need to define it in $ORACLE_HOME/sqlplus/admin/glogin.sql and it will be effective for all session with sqlplus.

vi $ORACLE_HOME/sqlplus/admin/glogin.sql
define _editor='vi'

Tuesday, February 4, 2014

Automatically start oracle database on linux after server reboot

Here I'll show you how to configure system for automatically start oracle database after server reboot.
oracle database 11g --- Oracle Linux 6.3
First of all, you need to make sure that any database instances you want to auto start are set to "Y" in the /etc/oratab file
#
# This file is used by ORACLE utilities.  It is created by root.sh
# and updated by either Database Configuration Assistant while creating
# a database or ASM Configuration Assistant while creating ASM instance.

# A colon, ':', is used as the field terminator.  A new line terminates
# the entry.  Lines beginning with a pound sign, '#', are comments.
#
# Entries are of the form:
#   $ORACLE_SID:$ORACLE_HOME::
#
# The first and second fields are the system identifier and home
# directory of the database respectively.  The third filed indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
# Multiple entries with the same $ORACLE_SID are not allowed.
#
#
orcl:/u0/app/oracle/product/11.2.0/dbhome_1:Y

Oracle 11g includes 2 scripts which can be used to start or shut down Oracle databases on Linux. Both scripts are in $ORACLE_HOME/bin and called dbstart and dbshut. We can add some more actions for example start enterprise manager if we create our scripts. Let's make two scripts db_start.sh and db_stop.sh
vi /u0/app/oracle/db_start.sh
# script to start the Oracle database, listener and dbconsole
. ~/.bash_profile
# start the listener and the database
$ORACLE_HOME/bin/dbstart $ORACLE_HOME
# start the Enterprise Manager db console
$ORACLE_HOME/bin/emctl start dbconsole
exit 0

vi /u0/app/oracle/db_stop.sh
# script to stop the Oracle database, listener and dbconsole
. ~/.bash_profile
# stop the Enterprise Manager db console
$ORACLE_HOME/bin/emctl stop dbconsole
# stop the listener and the database
$ORACLE_HOME/bin/dbshut $ORACLE_HOME
exit 0

inside script we are calling the .bash_profile file of the user "oracle" for export environment variables, we need it for $ORACLE_HOME.
give execute right:
chmod u+x db_start.sh db_stop.sh
With user root, create a file called "oracle" under /etc/init.d
vi /etc/init.d/oracle
ORA_OWNER=oracle
RETVAL=0
 
case "$1" in
    'start')
        # Start the Oracle databases:
        su - $ORA_OWNER -c "/u0/app/oracle/db_start.sh"
        touch /var/lock/subsys/oracle
        ;;
    'stop')
        # Stop the Oracle databases:
        su - $ORA_OWNER -c "/u0/app/oracle/db_stop.sh"
        rm -f /var/lock/subsys/oracle
        ;;
    *)
        echo $"Usage: $0 {start|stop}"
        RETVAL=1
esac
exit $RETVAL

change permission for /etc/init.d/oracle file:
chmod 750 /etc/init.d/oracle
to create service of this script:
chkconfig --add oracle

and now we can start and stop database with:
service oracle stop
service oracle start

Now it's time to test our automatically startup procedure, for this reboot your server and check if your database starts automatically after reboot.

Tuesday, September 3, 2013

ORA-03113: end-of-file on communication channel

Hello,
While trying to open database encounter that error : ORA-03113: end-of-file on communication channel
[oracle@oel6 admin]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Productionon Tue Sep 3 14:31:43 2013
Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup;
ORACLE instance started.

Total System Global Area  835104768 bytes
Fixed Size      2232960 bytes
Variable Size    637537664 bytes
Database Buffers   188743680 bytes
Redo Buffers      6590464 bytes
Database mounted.
ORA-03113: end-of-file on communication channel
Process ID: 13889
Session ID: 10 Serial number: 3
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

Now see alert.log file
[oracle@oel6 admin]$ cd /u0/app/oracle/diag/rdbms/orcl/orcl/trace/
tail -100 alert_orcl.log

shows last 100 row in alert log file, which tells that
ORA-19815: WARNING: db_recovery_file_dest_size of 5368709120 bytes is 100.00% used, and has 0 remaining bytes available.
We have following choices
1) Add disk space and increase db_recovery_file_dest_size parameter
2) Delete unnecessary files using RMAN DELETE command. If an operating
system command was used to delete files, then use RMAN CROSSCHECK and
DELETE EXPIRED commands.
now we show second method, delete archivelogs with operating system command.
[oracle@oel6 trace]$ . oraenv
ORACLE_SID = [+ASM] ? 
The Oracle base remains unchanged with value /u0/app/oracle
[oracle@oel6 trace]$ asmcmd
ASMCMD> cd Disks/orcl/archivelog
ASMCMD> ls
2013_06_30/
2013_07_01/
2013_07_02/
2013_07_03/
2013_07_04/
2013_07_05/
2013_07_06/
2013_07_07/
2013_07_08/
2013_07_09/
2013_07_10/
2013_07_11/
2013_07_12/
2013_09_03/
ASMCMD> rm -rf 2013_06_30/
ASMCMD> rm -rf 2013_07_01/ 
ASMCMD> rm -rf 2013_07_02/ 2013_07_03/
ASMCMD> rm -rf 2013_07_04/ ....

after that we can startup database
[oracle@oel6 trace]$ . oraenv
ORACLE_SID = [+ASM] ? orcl
The Oracle base remains unchanged with value /u0/app/oracle
[oracle@oel6 trace]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Sep 3 15:28:41 2013

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup;
ORACLE instance started.

Total System Global Area  835104768 bytes
Fixed Size      2232960 bytes
Variable Size    637537664 bytes
Database Buffers   188743680 bytes
Redo Buffers      6590464 bytes
Database mounted.
Database opened.
SQL> 

And then use crosschek and delete expired commands.
SQL> !rman target /

Recovery Manager: Release 11.2.0.3.0 - Production on Tue Sep 3 15:32:32 2013

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: ORCL (DBID=1332821480)

RMAN> crosscheck archivelog all;
................................
................................
RMAN> delete expired archivelog all;

that's all.
thank you.