Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, March 17, 2020

Oracle Database Table Fragmentation:

In oracle schema sometimes found in some tables having big difference in actual size (from User_segments) and expected size from user_tables (Num_rows*avg_row_length (in bytes)).This happens due to fragmentation in the table or stats for table are not updated into user_tables.

If only insert happens in a table then there will not be any fragmentation.Fragmentation happens when we update/delete data in table.
The space which gets freed up during non-insert DML operations is not immediately re-used (or sometimes, may not get  reuse ever at all). This leaves behind holes in table which results in table fragmentation.

how oracle manages space for tables.

When rows are not stored contiguously, or if rows are split onto more than one block, performance decreases because these rows require additional block accesses.

When a lot of DML operations are applied on a table, the table will become fragmented because DML does not release free space from the table below the HWM.(High Water Mark).

When rows are inserted into the table, the high watermark(used blocks) of the table moved forward in order to accommodate new rows into the table. But during delete operation, oracle doesn’t allow high watermark to shift backward in order to decrease table size and release the free space.Ideally once we delete the data from the table, the free space should be released or reused but additionally oracle acquire new blocks to accommodate the new rows for insertion which causes hole into the table.

Table fragmentation, which cause slowness and a wastage of space.
Fragmentation is a common issue in oracle database which occurs due to excessive dml operations like insert followed by update and delete operations.


Gather table statistics:

In order to check exact difference in table actual size (dba_segments) and stats size (dba_tables). The difference between these value will report actual fragmentation to DBA. So, We have to have updated stats on the table stored in dba_tables. Check LAST_ANALYZED value for table in dba_tables. If this value is recent you can skip this step. Other wise i would suggest to gather table stats to get updated stats.

To fins LAST_ANALYZED:
select table_name,last_analyzed,stale_stats from user_tab_statistics where table_name='&TABLE_NAME';

To find Table size:
select table_name,bytes/(1024*1024*1024) from dba_table where table_name='&table_name';
select sum(bytes)/1024/1024/1024 from dba_segments where segment_name='&TABLE_NAME';

EXEC dbms_stats.gather_table_stats(ownname => 'TABLE_OWNER', tabname => 'TABLE_NAME', method_opt=> 'for all indexed columns size skewonly', granularity => 'ALL', degree => 8 ,cascade => true,estimate_percent => 15);

To find actual table size, fragmented size and percentage of fragmentation in a table.:

set pages 50000 lines 32767;
select owner,
       table_name,
       round((blocks * 8), 2) || 'kb' "Fragmented size",
       round((num_rows * avg_row_len / 1024), 2) || 'kb' "Actual size",
       round((blocks * 8), 2) - round((num_rows * avg_row_len / 1024), 2) || 'kb',
       ((round((blocks * 8), 2) - round((num_rows * avg_row_len / 1024), 2)) /
       round((blocks * 8), 2)) * 100 - 10 "reclaimable space % "
  from dba_tables
 where table_name = '&table_Name'
   AND OWNER LIKE '&schema_name';
=========== 
 
select table_name,avg_row_len,round(((blocks*16/1024)),2)||'MB' "TOTAL_SIZE",
round((num_rows*avg_row_len/1024/1024),2)||'Mb' "ACTUAL_SIZE",
round(((blocks*16/1024)-(num_rows*avg_row_len/1024/1024)),2) ||'MB' "FRAGMENTED_SPACE",
(round(((blocks*16/1024)-(num_rows*avg_row_len/1024/1024)),2)/round(((blocks*16/1024)),2))*100 "percentage"
from all_tables WHERE table_name='&TABLE_NAME';

***If you find more than 20% fragmentation then you can proceed for de-fragmentation.

To find Top 10 fragmentation tables:
select *
      from (select table_name,
               round((blocks * 8), 2) "size (kb)",
               round((num_rows * avg_row_len / 1024), 2) "actual_data (kb)",
               (round((blocks * 8), 2) -
               round((num_rows * avg_row_len / 1024), 2)) "wasted_space (kb)"
          from dba_tables
         where (round((blocks * 8), 2) >
               round((num_rows * avg_row_len / 1024), 2))
         order by 4 desc)
 WHERE ROWNUM <= 10;



To find indexes on the table:
select index_name from dba_indexes where table_name='&TABLE_NAME';

To reset HWM / remove fragmentation:

There are three way to do this:

1. Alter table move (to another tablespace, or same tablespace) and rebuild indexes:-(Depends upon the free space available in the tablespace) 
2. Export and import the table:- (difficult to implement in production environment)
3. Shrink command (from Oracle 10g onwards)(Shrink command is only applicable for tables which are tablespace with auto segment space management)

Way1:

select index_name,status from dba_indexes  where table_name like '&table_name';

For same tablespace:

alter table <table_name> move;

For  new tablespace:

alter table <table_name> enable row movement;
alter table <table_name> move tablespace <new_tablespace_name>;

rebuild all the indexes:
alter index <INDEX_NAME> rebuild online;

Way2: 
Export and import the table.

Way3:
Shrink command:
Its in introduced in 10g,applicable for tables which are tablespace with auto segment space management.It is online automatic segment space.

row movement should be  enabled.
alter table <table_name> enable row movement;

alter table <table_name> shrink space compact; >>In this Rearrange rows and then reset HWM,All DML's will happen during this time.

alter table <table_name> shrink space;>>In Reset HWM, No DML will happen,its very quick

After completing these steps, table statistics must be gathered.

Monday, March 16, 2020

Oracle function-based index:

We will give demonstration with table name department created in the CREATE INDEX. 

Statement to creates an index on the first_name column of the department table:

CREATE INDEX department_i ON members(first_name);

if in WHERE clause first_name column is being used ,optimizer will definitely use the index:

select * from  department  WHERE first_name = 'Man';

if function is being used on the indexed column first_name as below then optimizer will not use index.

select * from  department  WHERE UPPER(first_name) = 'MAN';

To overcome above concern, Oracle introduced function-based indexes:

A function-based index calculates the result of a function that involves one or more columns and stores that result in the index.

syntax of creating a function-based index:

CREATE INDEX index_name ON table_name (expression)

index expression can be an arithmetic expression or an expression that contains a function such as a SQL function, PL/SQL function, and package function.

*** function-based index can be a btree or bitmap index.
example:

CREATE INDEX department_i ON members(UPPER(first_name));

Here Oracle will convert all values of first_name column to uppercase and stored  in index department_i .

Now below quey will use index to fetch the data.

select * from  department  WHERE UPPER(first_name) = 'MAN';

Saturday, March 14, 2020

Oracle database Indexes

Index:
An index stores the values in the indexed column(s). And for each value the locations of the rows that have it. Just like the index at the back of a book.

Types of index:

B-tree vs. Bitmap:
B-tree:
By default indexes are B-tree.These are balanced. This means that all the leaf nodes are at the same depth in the tree.

Bitmap:
A bitmap index is a two-dimensional array with zero and one (bit) values. it stores the indexing information in bit arrays. The query and search result is done by performing logical bitwise 0 or 1 operation. This make it very fast.

Comparison in both:
Bitmaps is it's easy to compress all those ones and zeros. So a bitmap index is typically smaller than the same B-tree index.

Rows where all the indexed values are null are NOT included in a B-tree. But they are in a bitmap! So the optimizer can use a bitmap to answer queries like:
where indexed_column is null;

You can get around this with B-trees by adding a constant to the end of an index. This makes the following composite index:
create index enable_for_is_null_i on tab ( indexed_column, 1 );

Bitmap indexes may lead to concurrency issues however, possibly blocking other DML on the same table. Therefore these should be avoided in an OLTP applications.

Why are B-trees the default instead of bitmaps?

Killing write concurrency.

They're one of the few situations in Oracle Database where an insert in one session can block an insert in another. This makes them questionable for most OLTP applications.

Well, whenever you insert, update or delete table rows, the database has to keep the index in sync. This happens in a B-tree by walking down the tree, changing the leaf entry as needed. You can see how this works with this visualization tool.

But bitmap locks the entire start/end rowid range! So say you add a row with the value RED. Any other inserts which try to add another row with the value RED to the same range are blocked until the first one commits!

This is an even bigger problem with updates. An update from a B-tree is really a delete of the old value and insert of the new one. But with a bitmap Oracle Database has to lock the affected rowid ranges for both the old and new values!

Function-based Indexes:

Unique Indexes:

Descending Indexes:

Concatenate or Composite index: 
It is an index on multiple columns in a table.

Global Partitioned Index:

Local Partitioned Index:

https://www.tutorialsteacher.com/sqlserver/indexes

Friday, March 13, 2020

Database Trace

Alter session set sql_trace=true;
alter system set trace_enabled=true;
execute dbms_support.start_trace_in_session (sid, serial#,bind=>true,wait=>true)
alter session set tracefile_identifier='577' event '10046 trace name context forever, level 1;
alter session set event '10046 trace name context off';


select * from dba_enabled_traces;

Remove persistence

applmgr@node101]$rm -rf $INST_TOP/ora/10.1.3/j2ee/oacore/persistence/*
applmgr@node101]$rm -rf $INST_TOP/ora/10.1.3/j2ee/oafm/persistence/*
applmgr@node101]rm -rf $INST_TOP/ora/10.1.3/j2ee/forms/persistence/*

Database Upgrade From 11.2.0.4 to 19.2.0.0 Using Manual Method:

1.Install new software for Oracle 19c. 

Make sure you have all the OS prerequisites in place by running the 19c preinstall package. 
yum install -y oracle-database-preinstall-19c
yum update -y
Install 19c Software
Run the root scripts when prompted.

2.Run preupgrade.jar

Put the latest “preupgrade.jar” into the 19c Oracle home.
Make sure you are using the original Oracle home and run the “preupgrade.jar”.

$ $ORACLE_BASE/product/19.0.0/dbhome_1/jdk/bin/java -jar 
$ORACLE_BASE/product/19.0.0/dbhome_1/rdbms/admin/preupgrade.jar TERMINAL TEXT

$OLD_ORACLE_HOME/jdk/bin/java -jar $NEW_ORACLE_HOME/rdbms/admin/preupgrade.jar -FILE DIR $OS_DIRECTORY


>The output from the “preupgrade.jar” lists a number of pre-upgrade recommendations. Some must be manually applied. Others are incorporated into the “preupgrade_fixups.sql” script. 

>Next step is to follow the guidelines displayed by the output, i.e. changing parameters, gathering stats, running the preupgrade_fixups.sql then shutt down the source database and copying the SPFILE and creating a new password file.

3.Upgrade the Database:

Start the database in STARTUP UPGRADE mode and run the upgrade.

# Regular upgrade command.
cd $ORACLE_HOME/rdbms/admin
$ORACLE_HOME/perl/bin/perl catctl.pl catupgrd.sql

$ORACLE_HOME/perl/bin/perl catctl.pl -n 6 -l /home/oracle catupgrd.sql

# Shorthand command.
$ORACLE_HOME/bin/dbupgrade

4.Post-Upgrade:

Postupgrade_fixups.sql and time zone adjustment.

Database is now fully upgraded and ready to go.

5.Plugin the upgraded database as a PDB into a CDB

==============================
Assumptions
Prerequisities
Install 19c Software
Run preupgrade.jar
Perform Pre-Upgrade Actions
Upgrade the Database
Perform Post-Upgrade Actions
Create New Container Database (CDB)
Convert Non-CDB to PDB
Final Steps
===================
Upgrade Oracle 12.2.0.1.0 to Oracle 19.3

High Level steps :

  • Install Oracle 19c in the target server
  • Apply latest patch (not covered in this article)
  • Take RMAN and Export backup of 12c
  • EXECUTE DBMS_STATS.GATHER_DICTIONARY_STATS; in 12c database
  • PURGE DBA_RECYCLEBIN; in 12c database
  • Run Pre upgrade.jar
  • Wait until all materialized views have completed refreshing
  • Create restore point for flashback incase upgrade fails
  • Check Timezone version
  • Take care of the Required and Recommended actions before upgrade
  • Stop LISTENER in 12c
  • Stop the 12c Database
  • Copy listener, tnsnames and sqlnet.ora and password file to 19c network/admin
  • Copy init file to 19c dbs location
  • Start Listener in 19c location
  • Start the 19c DB in upgrade mode
  • Upgrade the Time Zone
  • Gather dictionary statistics after the upgrade
  • Execute the postupgrade fixups
  • Drop restore point

#) Time zone file.
sqlplus / as sysdba <<EOF

-- Check current settings.
SELECT * FROM v$timezone_file;

SHUTDOWN IMMEDIATE;
STARTUP UPGRADE;

-- Begin upgrade to the latest version.
SET SERVEROUTPUT ON
DECLARE
  l_tz_version PLS_INTEGER;
BEGIN
  l_tz_version := DBMS_DST.get_latest_timezone_version;

  DBMS_OUTPUT.put_line('l_tz_version=' || l_tz_version);
  DBMS_DST.begin_upgrade(l_tz_version);
END;
/

SHUTDOWN IMMEDIATE;
STARTUP;

-- Do the upgrade.
SET SERVEROUTPUT ON
DECLARE
  l_failures   PLS_INTEGER;
BEGIN
  DBMS_DST.upgrade_database(l_failures);
  DBMS_OUTPUT.put_line('DBMS_DST.upgrade_database : l_failures=' || l_failures);
  DBMS_DST.end_upgrade(l_failures);
  DBMS_OUTPUT.put_line('DBMS_DST.end_upgrade : l_failures=' || l_failures);
END;
/

-- Check new settings.
SELECT * FROM v$timezone_file;

COLUMN property_name FORMAT A30
COLUMN property_value FORMAT A20

SELECT property_name, property_value
FROM   database_properties
WHERE  property_name LIKE 'DST_%'
ORDER BY property_name;

exit;
EOF

# 12) Ignored
# 13) AUTOFIXUP

# 14) Gather fixed object stats.
sqlplus / as sysdba <<EOF
EXECUTE DBMS_STATS.GATHER_FIXED_OBJECTS_STATS;
exit;
EOF

# AUTOFIXUP
sqlplus / as sysdba <<EOF
@/u01/app/oracle/cfgtoollogs/db11g/preupgrade/postupgrade_fixups.sql
exit;
EOF

Convert Non-CDB to PDB:

Convert Oracle 19c Non-CDB to PDB

Execute DBMS_PDB.DESCRIBE

oracle@db21:/opt/oracle/product/19.0.0/dbhome_1/network/admin$ sqlplus sys as sysdba

Connected to:

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Version 19.6.0.0.0

 SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

 SQL> startup mount;

ORACLE instance started.

Total System Global Area 2147478488 bytes

Fixed Size                  8874968 bytes

Variable Size            1258291200 bytes

Database Buffers          872415232 bytes

Redo Buffers                7897088 bytes

Database mounted.

 SQL> alter database open read only;

 Database altered.

 SQL> exec DBMS_PDB.DESCRIBE ('/export/home/oracle/dgpdevl.xml');

 PL/SQL procedure successfully completed.

 SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

 Plug in the database to existing Oracle 19c CDB

 SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED

---------- ------------------------------ ---------- ----------

         2 PDB$SEED                       READ ONLY  NO

 SQL> create pluggable database dgpdevl using '/export/home/oracle/dgpdevl.xml' nocopy tempfile reuse;

Pluggable database created.

SQL> show pdbs

 CON_ID CON_NAME                       OPEN MODE  RESTRICTED

---------- ------------------------------ ---------- ----------

         2 PDB$SEED                       READ ONLY  NO

         3 DGPDEVL                        MOUNTED

 SQL> alter pluggable database open;

 Pluggable database altered.

 Execute noncdb_to_pdb.sql script

 SQL> alter session set container=DGPDEVL;

 Session altered.

 SQL> @?/rdbms/admin/noncdb_to_pdb.sql

….

….

 13:47:57 SQL> set tab OFF

13:47:57 SQL> set termout ON

13:47:57 SQL> set time OFF

SQL> set timing OFF

SQL> set trimout ON

SQL> set trimspool ON

SQL> set underline "-"

SQL> set verify OFF

SQL> set wrap ON

SQL> set xmloptimizationcheck OFF

 Verify PDB plug-in operation via PDB_PLUG_IN_VIOLATIONS

 SQL> select con_id, type, message, status

  from PDB_PLUG_IN_VIOLATIONS

 where status <>'RESOLVED'

 order by time; 

    CON_ID TYPE      MESSAGE                                                                                              STATUS

---------- --------- ---------------------------------------------------------------------------------------------------- ---------

         4 WARNING   Character set mismatch: PDB character set US7ASCII. CDB character set AL32UTF8.                      PENDING

         4 WARNING   Database option APS mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.          PENDING

         4 WARNING   Database option CATJAVA mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.      PENDING

         4 WARNING   Database option CONTEXT mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.      PENDING

         4 WARNING   Database option DV mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.           PENDING

         4 WARNING   Database option JAVAVM mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.       PENDING

         4 WARNING   Database option OLS mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.          PENDING

         4 WARNING   Database option ORDIM mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.        PENDING

         4 WARNING   Database option OWM mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.          PENDING

         4 WARNING   Database option SDO mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.          PENDING

         4 WARNING   Database option XML mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.          PENDING

         4 WARNING   Database option XOQ mismatch: PDB installed version NULL. CDB installed version 19.0.0.0.0.          PENDING

 

 Verify PDB is open in READ WRITE mode

 SQL> conn / as sysdba

Connected.

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED

---------- ------------------------------ ---------- ----------

         2 PDB$SEED                       READ ONLY  NO

         3DGPDEVL                        READ WRITE NO


https://isqlplus.com/oracle/upgrade/upgrading-oracle-database-11g-to-oracle-database-19c-non-cdb-to-cdb/

https://mikedietrichde.com/2019/07/23/database-migration-from-non-cdb-to-pdb-upgrade-plug-in-convert/

https://oracle-base.com/articles/19c/upgrading-to-19c#create-new-cdb

HOW TO FIND UNUSED INDEXES

Enable monitoring on an index:

ALTER INDEX index_name MONITORING USAGE;

Monitoring Query:

SELECT * FROM v$object_usage;

Disable monitoring of an index:

ALTER INDEX index_name NOMONITORING USAGE;

To enable monitoring on all indexes:

SET heading off
SET echo off
SET pages 10000
SPOOL start_monitoring.sql
SELECT 'alter index ' || owner || '.' || index_name || ' monitoring usage;'
FROM dba_indexes
WHERE owner NOT IN ('SYS', 'SYSTEM');
SPOOL off
@start_monitoring.sql

To stop monitoring on all indexes:

SET heading off
SET echo off
SET pages 10000
SPOOL stop_monitoring.sql
SELECT 'alter index ' || owner || '.' || index_name || ' nomonitoring usage;'
FROM dba_indexes
WHERE owner NOT IN ('SYS', 'SYSTEM');
SPOOL off
@stop_monitoring.sql

Query to find unused indexes:

SELECT * FROM dba_object_usage WHERE used = 'NO';

Imp:If you analyze an index, it will be marked as “used”.

Tuesday, March 10, 2020

Oracle 19c Feature

ORACLE 19C NEW FEATURE COMPARE EXECUTION PLANS WITH DBMS_XPLAN.COMPARE_PLANS

A new feature in Oracle 19c is the ability to use DBCA to not only perform a remote clone of a PDB, but also to relocate a PDB from one Container Database to another.


ORACLE 19C NEW FEATURE AUTOMATIC FLASHBACK OF STANDBY DATABASE

ORACLE 19C NEW FEATURE AUTOMATIC INDEXING

  • DBCA silent mode improvements. Use DBCA in silent mode to perform the following tasks:        
                           Clone a remote PDB.        
                           Relocate a PDB to another container database (CDB).
                          Create a duplicate of an Oracle Database.
  • Dry-run validation of an Oracle Clusterware upgrade.
  • Flashback in Data Guard.
  • DML mode in Data Guard.
  • Network file retention and size limits.
  • Automatic indexing feature.
  • Image-based Oracle client installation.
  • AutoUpgrade for an Oracle database.



https://gavinsoorma.com.au/knowledge-base/19c-new-feature-dbca-pluggable-database-remote-clone-and-relocate/
https://oracle-base.com/articles/19c/multitenant-dbca-pdb-remote-clone-19c

ORA-00600: internal error code, arguments: [kjxmgmb_nreq:!bat]

Issue:
We are running a RAC 2 nodes with database version:11.2.0.4.This morning after a kernel update or host reboot one of the instance i.e 2nd instance  refuse to startup due to the following error :

ORA-00600: internal error code, arguments: [kjxmgmb_nreq:!bat], [17], [56], [9], [], [], [], [], [], [], [], []
LMS0 (ospid: 24928): terminating the instance due to error 484
System state dump requested by (instance=2, osid=24928 (LMS0)), summary=[abnormal instance termination].

Fixes:

As temporary fix was :-

1. After another reboot, the instance has finally started.

Complete instance bounce including database

2.The permanent fix of the issue should be the patch 20250147 - ORA 600 [KJXMGMB_NREQ:!BAT]


Bug 20250147  - ORA-600 [kjxmgmb_nreq:!bat] can occur in RAC crashing the instance (Doc ID 20250147.8)


ORA-600 [kjxmgmb_nreq:!bat] (Doc ID 2211714.1)

Main Fix:

LMS are not running in RT and same as VKTM.

In 10gR2 and above the LMS process is intended to run in the real time scheduling class. In some instances we have seen this prevented due to incorrect ownership or permissions for the oradism executable which is stored in the $ORACLE_HOME/bin directory. See Document 602419.1 for more details on this.

 Check the file permissions of '$ORACLE_HOME/bin/oradism' the database instance.

 The permissions for 'oradism' must be 6550 with the setid for the group: e.g. (-r-sr-s--- 1 root oinstall 129 12 Feb 16 2008 oradism) .

 REFERENCE:  RAC and Oracle Clusterware Best Practices and Starter Kit (Platform Independent) (Doc ID 810394.1)

$ ls -l $ORACLE_HOME/bin/oradism
-rwxr-x--- 1 oracle orainst 71780 Sept 24  2011 $ORACLE_HOME/bin/oradism

Exachk reports incorrect oradism ownership and permission (Doc ID 2009788.1)

Also, the exachk must be run as root user. If not run as root user, warnings such as this can be flagged incorrectly.

Ensure root user was used to run exachk. The user will be reported in the top summary of the html report.

If reported on the db home, you can do the following to change permissions.

Run the following commands with root permission to resolve the issue.

#cd $ORACLE_HOME/bin/

#chown root oradism

#chmod u+s oradism

You can make the changes with the db open.