Wednesday, October 20, 2021

Why and How to check the tablespace size using script file for specific size ?

  Why ?

To check the tablespace status in a script file that is return a value of 0 OK or 1 not OK a long with Tablespaces details , I am using the below bin bash script which is show all needed details and I configured a waring for 12GB then error for 9GB otherwise all are ok , you may change this upon needed .

How ?

Below is bin bash script and I am assuming you make sure to set the profile things in the running user  :

1- create and set up the script file:

vi new_check_tablespace.sh

chmod +x new_check_tablespace.sh

2- in the script.sh file  :

#!/bin/bash

export PATH=/usr/bin:/usr/sbin

export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1

export ORACLE_SID=ORCL

export PATH=$PATH:$ORACLE_HOME/bin


check_tbspace() {

temp1=`sqlplus -s / as sysdba <<EOF

set serveroutput on;

SET FEEDBACK OFF

DECLARE

temp integer ;

CURSOR table_space IS

SELECT tablespace_name,

       size_mb,

       free_mb,

       max_size_mb,

       max_free_mb,

       TRUNC((max_free_mb/max_size_mb) * 100) AS free_pct,

       RPAD(' '|| RPAD('X',ROUND((max_size_mb-max_free_mb)/max_size_mb*10,0), 'X'),11,'-') AS used_pct

FROM   (

        SELECT a.tablespace_name,

               b.size_mb,

               a.free_mb,

               b.max_size_mb,

               a.free_mb + (b.max_size_mb - b.size_mb) AS max_free_mb

        FROM   (SELECT tablespace_name,

                       TRUNC(SUM(bytes)/1024/1024) AS free_mb

                FROM   dba_free_space

                GROUP BY tablespace_name) a,

               (SELECT tablespace_name,

                       TRUNC(SUM(bytes)/1024/1024) AS size_mb,

                       TRUNC(SUM(GREATEST(bytes,maxbytes))/1024/1024) AS max_size_mb

                FROM   dba_data_files

                GROUP BY tablespace_name) b

        WHERE  a.tablespace_name = b.tablespace_name

       )

ORDER BY tablespace_name;

t_row table_space%ROWTYPE;

begin

temp := 0;

open table_space ;

loop

fetch table_space into t_row ;

exit when table_space%NOTFOUND;

if ( t_row.MAX_FREE_MB <=12288) and (temp <> 1)  then

temp :=1;

DBMS_OUTPUT.PUT_LINE(1);

elsif ( t_row.MAX_FREE_MB <= 119216) and (temp <> 1) then

temp :=2;

DBMS_OUTPUT.PUT_LINE(2);

end if;

END LOOP;

if temp = 0 then

dbms_output.put_line(0);

end if;

close table_space;

END;

/

EXIT;

EOF`

}

tbspace_details() {

sqlplus -s / as sysdba <<EOF

SET PAGESIZE 140 LINESIZE 200;

COLUMN used_pct FORMAT A11;

SELECT tablespace_name,

       size_mb,

       free_mb,

       max_size_mb,

       max_free_mb,

       TRUNC((max_free_mb/max_size_mb) * 100) AS free_pct,

       RPAD(' '|| RPAD('X',ROUND((max_size_mb-max_free_mb)/max_size_mb*10,0), 'X'),11,'-') AS used_pct

FROM   (

        SELECT a.tablespace_name,

               b.size_mb,

               a.free_mb,

               b.max_size_mb,

               a.free_mb + (b.max_size_mb - b.size_mb) AS max_free_mb

        FROM   (SELECT tablespace_name,

                       TRUNC(SUM(bytes)/1024/1024) AS free_mb

                FROM   dba_free_space

                GROUP BY tablespace_name) a,

               (SELECT tablespace_name,

                       TRUNC(SUM(bytes)/1024/1024) AS size_mb,

                       TRUNC(SUM(GREATEST(bytes,maxbytes))/1024/1024) AS max_size_mb

                FROM   dba_data_files

                GROUP BY tablespace_name) b

        WHERE  a.tablespace_name = b.tablespace_name

       )

ORDER BY free_pct ;

exit;

EOF

}


status() {


check_tbspace 

tbspace_details


if [ "$temp1" == "1" ]; then

echo "**Warning NOT All TableSpaces have free space more than 12GB "

return 1

elif [ "$temp1" == "2" ]; then

echo "--ERROR One or more TableSpaces have free space less than 9GB "

return 1

else 

echo "++OK All TableSpaces have free space more than 10GB "

return 0

fi


}


###################### END "STATUS" SECTION ##############################


case "$1" in

    'status')

        status

        ;;

    'root')

su  oracle -c "cd /u01/app/monit/script && ./new_check_tablespace.sh status"

        ;;

    *)

        echo  $"Usage: $0 {status}" 

        exit 1

esac

exit 0

Tuesday, October 19, 2021

Why and How to check and table size/actual size in oracle ?

 Why ?

In Oracle after the deletion of number of rows in the table you will expected a size change however due the high water mark things which mark the deleted space and keep the HWM thus the size will not be affected unless you do further action, however it is useful to know the actual size after this deletion and whether you will reclaim or not this is can be decided based on that.

How ?

Below are queries to this matter:

1- To check a table reserved size  :

col owner format A20

col SEGMENT_NAME format A20

set linesize 300

SELECT SEGMENT_NAME ,  OWNER , SUM(bytes)/1024/1024 "MB"

FROM DBA_SEGMENTS

WHERE segment_name=upper('&object_name') and segment_type= upper('&object_type')

Group by segment_name , owner;

 2- To check the waisted size and how many reclaimed size , you may check the below query , however it is mostly same to things in segment adviser that I will add about it last things , it is can help you to reach 90% of things in this matter:

SELECT TABLE_NAME

         ROUND((BLOCKS * 8/1024),0) "SIZE (MB)",

         ROUND((NUM_ROWS * AVG_ROW_LEN / 1024/1024), 0) "ACTUAL DATA (MB)",

         (ROUND((BLOCKS * 8/1024),0) - ROUND((NUM_ROWS * AVG_ROW_LEN / 1024/1024), 0)) "WASTED (MB)"

    FROM DBA_TABLES

   WHERE  OWNER LIKE upper('&owner_name') and table_name = upper('&table_name') ;

 3- If you just do the clean you , you may need to update the DB about this object using the below  :

analyze table &owner..&object COMPUTE STATISTICS;

 

4- Another option is to use the Segment advisor which part of tuning pack license so if you got this license you can run the advisor or it is daily run by default to check all these details , using Oracle Enterprise Manager ( OEM) ,

 


You may run it manually if you don’t have the OEM agent in this DB as below :

The example that follows shows how to use the DBMS_ADVISOR procedures to run the Segment Advisor for the sample table hr.employees.

variable id number;

begin

  declare

  name varchar2(100);

  descr varchar2(500);

  obj_id number;

  begin

  name:='Manual_Employees';

  descr:='Segment Advisor Example';

 

  dbms_advisor.create_task (

    advisor_name     => 'Segment Advisor',

    task_id          => :id,

    task_name        => name,

    task_desc        => descr);

 

  dbms_advisor.create_object (

    task_name        => name,

    object_type      => 'TABLE',

    attr1            => 'HR',

    attr2            => 'EMPLOYEES',

    attr3            => NULL,

    attr4            => NULL,

    attr5            => NULL,

    object_id        => obj_id);

 

  dbms_advisor.set_task_parameter(

    task_name        => name,

    parameter        => 'recommend_all',

    value            => 'TRUE');

 

  dbms_advisor.execute_task(name);

  end;

end;

/

** Then use select from view or dbms_space.asa_recommendations

The following example shows how to query the DBA_ADVISOR_* views to retrieve findings from all Segment Advisor runs submitted by user STEVE:

 

select af.task_name, ao.attr2 segname, ao.attr3 partition, ao.type, af.message

  from dba_advisor_findings af, dba_advisor_objects ao

  where ao.task_id = af.task_id

  and ao.object_id = af.object_id

  and ao.owner = 'STEVE';

TASK_NAME          SEGNAME      PARTITION       TYPE             MESSAGE

 

select tablespace_name, segment_name, segment_type, partition_name,

recommendations, c1 from

table(dbms_space.asa_recommendations('FALSE', 'FALSE', 'FALSE'));

** the above example is from Oracle document 

https://docs.oracle.com/cd/E18283_01/server.112/e17120/schema003.htm.

Why and How to install Grid 19c on RHEL 8?

  Why ? Simply we will be requested to install Oracle Grid RAC DB on Redhat RHEL 8, below is my note for this installation . How ? 1-  OS in...