Sunday, October 25, 2020

FULL TABLE SCAN VS INDEX SCAN PERFORMACE in Oracle

 Let’s take the employees database, and slightly modify the employees tables:

 

 

mysql> ALTER TABLE employees ADD INDEX idx_first (first_name),ENGINE=InnoDB;

1

SELECT * FROM employees ORDER BY first_name;

This query can ofcourse by executed by running a full table scan, but we could also take advantage of the idx_first index, which will translate to a full index scan.

Let’s see how the optimizer will execute it:

 

1

2

3

4

5

6

7

8

9

10

11

12

mysql> EXPLAIN SELECT * FROM employees ORDER BY first_name\G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: employees

         type: ALL

possible_keys: NULL

          key: NULL

      key_len: NULL

          ref: NULL

         rows: 300363

        Extra: Using filesort

 

1

2

3

4

5

6

7

8

9

10

11

12

mysql> EXPLAIN SELECT * FROM employees FORCE INDEX(idx_first) ORDER BY first_name\G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: employees

         type: index

possible_keys: NULL

          key: idx_first

      key_len: 16

          ref: NULL

         rows: 300363

        Extra:

Honestly, it looks better: the number of rows is the same, but a full index scan instead of a full table scan and no filesort. But predicting how a query will perform by only looking at the execution plan is not enough, we must run both queries and compare execution time.

First case: the employees table does not fit in memory
With the full table scan, the query runs in about 4s.
With the full index scan, it runs in about 30s.

So the optimizer was right after all. But why? Simply because all access patterns are not equal. When we are doing a full table scan, we are doing sequential reads, which are quite fast even with slow disks. But when we are using the index, we first have to do a full index scan (fast sequential reads, no problem) and then lots of random reads to fetch rows by index value. And random reads are orders of magnitude slower than sequential reads.

The optimizer has this knowledge, so it is able to pick the right execution plan.

Second case: the employees table fits in memory
With the full table scan, the query runs in about 3.3s.
With the full index scan, the query runs in about 2.6s.

We can see here a limitation of the optimizer: it does not know on which kind of media data is stored. If it is stored on spinning disks, the assumption that random reads are much slower than sequential reads is correct, but it is not the case anymore if data is stored in memory. That’s why when execution plans look similar, you should always execute the query to really see which execution plan should be chosen. Here if we know that the table will always be in memory, we should add the FORCE INDEX hint to ensure optimal response time.

Now let’s modify the query by selecting only the first_name field instead of selecting all the fields:

 

1

2

3

4

5

6

7

8

9

10

11

12

mysql> explain SELECT first_name FROM employees ORDER BY first_name\G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: employees

         type: index

possible_keys: NULL

          key: idx_first

      key_len: 16

          ref: NULL

         rows: 300584

        Extra: Using index

The optimizer chooses the full index scan. It is a good choice because the index now covers the query, which means that reading the index is enough to get the results.

Conclusions:

  • For a non-covering index, the difference between a full table scan and an execution plan based on a full index scan is basically the difference between sequential reads and random reads: it can be close if you have fast storage or it can be very different if you have slow storage.
  • A full index scan can become interesting when the index is covering.
  • Don’t forget to measure response time when you are trying different execution plans. It is too easy to get focused on having a good-looking execution, but the end user only cares about response time!

 

Saturday, October 24, 2020

How to change archivelog destination in oracle

 STEP 1:

Open the  Database

                                                   

STEP 2: 

Verifying recovery file and archive log state:

command: show parameter recovery;





STEP 3:

altering archvie destination location -

command : alter system set log_archive_dest_1= 'LOCATION=/u01/app/oracle/fast_recovery_area/ELEVENG/archivelog/01' scope=both;

 


 




select destination,STATUS from v$archive_dest where statuS='VALID';




verfications: Check whether archives are getting generated at new location:








Friday, October 16, 2020

Backup based Cloning Database using RMAN in Oracle 12c

 Target Database : test

Auxiliary Database : clone

Step 1:
Take the incremental level 0 backup of the Target database using RMAN.
In my case, I had the backup of my target database (test db).

[oracle@trichy ~]$ . ./ora12c.env

[oracle@trichy ~]$ rman target /

Recovery Manager: Release 12.2.0.1.0 - Production on Fri Oct 16 23:42:11 2020

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

connected to target database: TEST (DBID=2343286063)

RMAN> BACKUP INCREMENTAL LEVEL=0 DATABASE;

Starting backup at 16-OCT-20

using target database control file instead of recovery catalog

allocated channel: ORA_DISK_1

channel ORA_DISK_1: SID=52 device type=DISK

channel ORA_DISK_1: starting incremental level 0 datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

input datafile file number=00003 name=/u01/app/oracle/oradata/test/sysaux01.dbf

input datafile file number=00005 name=/u01/app/oracle/product/12.2.0.1/apex01.dbf

input datafile file number=00001 name=/u01/app/oracle/oradata/test/system01.dbf

input datafile file number=00004 name=/u01/app/oracle/oradata/test/undotbs01.dbf

input datafile file number=00007 name=/u01/app/oracle/oradata/test/users01.dbf

channel ORA_DISK_1: starting piece 1 at 16-OCT-20

channel ORA_DISK_1: finished piece 1 at 16-OCT-20

piece handle=/u01/arch/TEST/backupset/2020_10_16/o1_mf_nnnd0_TAG20201016T234303_hrmroyoq_.bkp tag=TAG20201016T234303 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:02:35

Finished backup at 16-OCT-20

Starting Control File and SPFILE Autobackup at 16-OCT-20

piece handle=/u01/arch/TEST/autobackup/2020_10_16/o1_mf_s_1053992740_hrmrtdxn_.bkp comment=NONE

Finished Control File and SPFILE Autobackup at 16-OCT-20

RMAN>

Step 2:
Copy these backup pieces from the Target server (location /u02/bkp) to the auxiliary server (location /u02/bkp)
Also, copy the pfile (initclone.ora) of the Target database to the Auxiliary server.

Copying Backup Pieces:

[oracle@trichy 2020_10_16]$ pwd

/u01/arch/TEST/backupset/2020_10_16

[oracle@trichy 2020_10_16]$ scp o1_mf_nnnd0_TAG20201016T234303_hrmroyoq_.bkp oracle@192.168.160.137:/u02/bkp

oracle@192.168.160.137's password:

o1_mf_nnnd0_TAG20201016T234303_hrmroyoq_.bkp                                                                 100% 2079MB  33.0MB/s   01:03    

[oracle@trichy 2020_10_16]$ cd

Copying pfile of Target Database to Auxiliary Server:

[oracle@trichy dbs]$ scp inittest.ora oracle@192.168.160.137:/u01/app/oracle/product/12.2.0.1/db_1/dbs/initclone.ora

oracle@192.168.160.137's password:

inittest.ora                                                                                                 100% 1222     1.2KB/s   00:00     

Step 3:

On the Auxiliary server, edit the pfile that was copied earlier to the desired entries (dump locations, control file location, datafile locations, if using ASM then specify the desired disk group) and rename it to the desired instance name file (init<SID>.ora). Below is the sample I had it done.

initclone.ora

test.__large_pool_size=33554432

test.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment

test.__pga_aggregate_target=2046820352

test.__sga_target=2046820352

test.__shared_io_pool_size=100663296

test.__shared_pool_size=419430400

test.__streams_pool_size=0

*.audit_file_dest='/u01/app/oracle/admin/clone/adump'

*.audit_trail='DB'

*.compatible='12.2.0'

*.db_file_name_convert='/u01/app/oracle/oradata/test','/u01/app/oracle/oradata/clone'

*.log_file_name_convert='/u01/app/oracle/oradata/test','/u01/app/oracle/oradata/clone'

*.control_files='/u01/app/oracle/oradata/clone/control01.ctl','/u01/app/oracle/oradata/clone/control02.ctl'

*.db_block_size=8192

*.db_name='clone'

*.db_recovery_file_dest_size=10737418240

*.db_recovery_file_dest='/u01/arch'

*.diagnostic_dest='/u01/app/oracle'

*.dispatchers='(PROTOCOL=TCP) (SERVICE=testXDB)'

*.nls_language='AMERICAN'

*.nls_territory='AMERICA'

*.open_cursors=300

*.pga_aggregate_target=1941M

*.processes=300

*.remote_login_passwordfile='EXCLUSIVE'

*.resource_limit=TRUE

*.sga_target=1952M

*.undo_tablespace='UNDOTBS1'

 Step 4:

Create a password file for the Auxiliary Database using the ORAPWD utility.

orapwd file=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/orapwtestdb password=<mypassword>

 Step 5:Start the auxiliary instance using the modified by pfile.

[oracle@trichy ~]$ . ./clone.env

[oracle@trichy ~]$ export ORACLE_SID=clone

[oracle@trichy ~]$ export PATH=$ORACLE_HOME/bin:$PATH

[oracle@trichy ~]$ export ORACLE_HOME=/u01/app/oracle/product/12.2.0.1/db_1

[oracle@trichy ~]$ sqlplus / as sysdba

SQL*Plus: Release 12.2.0.1.0 Production on Sat Oct 17 01:47:52 2020

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

Connected to an idle instance.

SQL> startup nomount pfile=/u01/app/oracle/product/12.2.0.1/db_1/dbs/initclone.ora

ORACLE instance started.

Total System Global Area 2046820352 bytes

Fixed Size     8622480 bytes

Variable Size   587206256 bytes

Database Buffers  1442840576 bytes

Redo Buffers     8151040 bytes

SQL>

Step 6:

Connect the auxiliary instance through RMAN and start the duplication.
The duplication is done by specifying the location of the backup pieces. The command to be used is DUPLICATE DATABASE TO ‘<auxiliary dbname>’ BACKUP LOCATION ‘<location of the backup pieces on the auxiliary server>’

[oracle@trichy ~]$ . ./clone.env

[oracle@trichy ~]$ rman auxiliary /

Recovery Manager: Release 12.2.0.1.0 - Production on Sat Oct 17 01:49:23 2020

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

connected to auxiliary database: CLONE (not mounted)

RMAN> duplicate database to 'clone' backup location '/u02/bkp' nofilenamecheck;

Starting Duplicate Db at 17-OCT-20

contents of Memory Script:

{

   sql clone "create spfile from memory";

}

executing Memory Script

sql statement: create spfile from memory

contents of Memory Script:

{

   shutdown clone immediate;

   startup clone nomount;

}

executing Memory Script

Oracle instance shut down

connected to auxiliary database (not started)

Oracle instance started

Total System Global Area    2046820352 bytes

Fixed Size                     8622480 bytes

Variable Size                587206256 bytes

Database Buffers            1442840576 bytes

Redo Buffers                   8151040 bytes

contents of Memory Script:

{

   sql clone "alter system set  db_name =

 ''TEST'' comment=

 ''Modified by RMAN duplicate'' scope=spfile";

   sql clone "alter system set  db_unique_name =

 ''clone'' comment=

 ''Modified by RMAN duplicate'' scope=spfile";

   shutdown clone immediate;

   startup clone force nomount

   restore clone primary controlfile from  '/u02/bkp/o1_mf_s_1053992740_hrmrtdxn_.bkp';

   alter clone database mount;

}

executing Memory Script

sql statement: alter system set  db_name =  ''TEST'' comment= ''Modified by RMAN duplicate'' scope=spfile

sql statement: alter system set  db_unique_name =  ''clone'' comment= ''Modified by RMAN duplicate'' scope=spfile

Oracle instance shut down

Oracle instance started

Total System Global Area    2046820352 bytes

Fixed Size                     8622480 bytes

Variable Size                587206256 bytes

Database Buffers            1442840576 bytes

Redo Buffers                   8151040 bytes

Starting restore at 17-OCT-20

allocated channel: ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: SID=34 device type=DISK

channel ORA_AUX_DISK_1: restoring control file

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:03

output file name=/u01/app/oracle/oradata/clone/control01.ctl

output file name=/u01/app/oracle/oradata/clone/control02.ctl

Finished restore at 17-OCT-20

 database mounted

released channel: ORA_AUX_DISK_1

allocated channel: ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: SID=34 device type=DISK

checkpoint of the data file is more recent than the last archived log

contents of Memory Script:

{

   set until scn  6429840;

   sql clone 'alter database flashback off';

   set newname for datafile  1 to

 "/u01/app/oracle/oradata/clone/system01.dbf";

   set newname for datafile  3 to

 "/u01/app/oracle/oradata/clone/sysaux01.dbf";

   set newname for datafile  4 to

 "/u01/app/oracle/oradata/clone/undotbs01.dbf";

   set newname for datafile  5 to

 "/u01/app/oracle/product/12.2.0.1/apex01.dbf";

   set newname for datafile  7 to

 "/u01/app/oracle/oradata/clone/users01.dbf";

   restore

clone database   ;

}

executing Memory Script

executing command: SET until clause

sql statement: alter database flashback off

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting restore at 17-OCT-20

using channel ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: starting datafile backup set restore

channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set

channel ORA_AUX_DISK_1: restoring datafile 00001 to /u01/app/oracle/oradata/clone/system01.dbf

channel ORA_AUX_DISK_1: restoring datafile 00003 to /u01/app/oracle/oradata/clone/sysaux01.dbf

channel ORA_AUX_DISK_1: restoring datafile 00004 to /u01/app/oracle/oradata/clone/undotbs01.dbf

channel ORA_AUX_DISK_1: restoring datafile 00005 to /u01/app/oracle/product/12.2.0.1/apex01.dbf

channel ORA_AUX_DISK_1: restoring datafile 00007 to /u01/app/oracle/oradata/clone/users01.dbf

channel ORA_AUX_DISK_1: reading from backup piece /u02/bkp/o1_mf_nnnd0_TAG20201016T234303_hrmroyoq_.bkp

channel ORA_AUX_DISK_1: piece handle=/u02/bkp/o1_mf_nnnd0_TAG20201016T234303_hrmroyoq_.bkp tag=TAG20201016T234303

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:27

Finished restore at 17-OCT-20

contents of Memory Script:

{

   switch clone datafile all;

}

executing Memory Script

datafile 1 switched to datafile copy

input datafile copy RECID=5 STAMP=1054000364 file name=/u01/app/oracle/oradata/clone/system01.dbf

datafile 3 switched to datafile copy

input datafile copy RECID=6 STAMP=1054000364 file name=/u01/app/oracle/oradata/clone/sysaux01.dbf

datafile 4 switched to datafile copy

input datafile copy RECID=7 STAMP=1054000364 file name=/u01/app/oracle/oradata/clone/undotbs01.dbf

datafile 5 switched to datafile copy

input datafile copy RECID=8 STAMP=1054000365 file name=/u01/app/oracle/product/12.2.0.1/apex01.dbf

datafile 7 switched to datafile copy

input datafile copy RECID=9 STAMP=1054000365 file name=/u01/app/oracle/oradata/clone/users01.dbf

Oracle instance started

Total System Global Area    2046820352 bytes

Fixed Size                     8622480 bytes

Variable Size                587206256 bytes

Database Buffers            1442840576 bytes

Redo Buffers                   8151040 bytes

contents of Memory Script:

{

   sql clone "alter system set  db_name =

 ''CLONE'' comment=

 ''Reset to original value by RMAN'' scope=spfile";

   sql clone "alter system reset  db_unique_name scope=spfile";

}

executing Memory Script

sql statement: alter system set  db_name =  ''CLONE'' comment= ''Reset to original value by RMAN'' scope=spfile

sql statement: alter system reset  db_unique_name scope=spfile

Oracle instance started

Total System Global Area    2046820352 bytes

Fixed Size                     8622480 bytes

Variable Size                587206256 bytes

Database Buffers            1442840576 bytes

Redo Buffers                   8151040 bytes

sql statement: CREATE CONTROLFILE REUSE SET DATABASE "CLONE" RESETLOGS ARCHIVELOG

  MAXLOGFILES     16

  MAXLOGMEMBERS      3

  MAXDATAFILES      100

  MAXINSTANCES     8

  MAXLOGHISTORY      292

 LOGFILE

  GROUP   1 ( '/u01/app/oracle/oradata/clone/redo01.log' ) SIZE 200 M  REUSE,

  GROUP   2 ( '/u01/app/oracle/oradata/clone/redo02.log' ) SIZE 200 M  REUSE,

  GROUP   3 ( '/u01/app/oracle/oradata/clone/redo03.log' ) SIZE 200 M  REUSE

 DATAFILE

  '/u01/app/oracle/oradata/clone/system01.dbf'

 CHARACTER SET AL32UTF8

contents of Memory Script:

{

   set newname for tempfile  1 to

 "/u01/app/oracle/oradata/clone/temp01.dbf";

   switch clone tempfile all;

   catalog clone datafilecopy  "/u01/app/oracle/oradata/clone/sysaux01.dbf",

 "/u01/app/oracle/oradata/clone/undotbs01.dbf",

 "/u01/app/oracle/product/12.2.0.1/apex01.dbf",

 "/u01/app/oracle/oradata/clone/users01.dbf";

   switch clone datafile all;

}

executing Memory Script

executing command: SET NEWNAME

renamed tempfile 1 to /u01/app/oracle/oradata/clone/temp01.dbf in control file

cataloged datafile copy

datafile copy file name=/u01/app/oracle/oradata/clone/sysaux01.dbf RECID=1 STAMP=1054000386

cataloged datafile copy

datafile copy file name=/u01/app/oracle/oradata/clone/undotbs01.dbf RECID=2 STAMP=1054000386

cataloged datafile copy

datafile copy file name=/u01/app/oracle/product/12.2.0.1/apex01.dbf RECID=3 STAMP=1054000386

cataloged datafile copy

datafile copy file name=/u01/app/oracle/oradata/clone/users01.dbf RECID=4 STAMP=1054000386

 datafile 3 switched to datafile copy

input datafile copy RECID=1 STAMP=1054000386 file name=/u01/app/oracle/oradata/clone/sysaux01.dbf

datafile 4 switched to datafile copy

input datafile copy RECID=2 STAMP=1054000386 file name=/u01/app/oracle/oradata/clone/undotbs01.dbf

datafile 5 switched to datafile copy

input datafile copy RECID=3 STAMP=1054000386 file name=/u01/app/oracle/product/12.2.0.1/apex01.dbf

datafile 7 switched to datafile copy

input datafile copy RECID=4 STAMP=1054000386 file name=/u01/app/oracle/oradata/clone/users01.dbf

Reenabling controlfile options for auxiliary database

Executing: alter database force logging

contents of Memory Script:

{

   Alter clone database open resetlogs;

}executing Memory Script

database opened

Executing: alter database flashback on

Cannot remove created server parameter file

Finished Duplicate Db at 17-OCT-20

RMAN>

Step 7:Connect to the newly created database.

[oracle@trichy ~]$ . ./clone.env

[oracle@trichy ~]$ sqlplus / as sysdba

SQL*Plus: Release 12.2.0.1.0 Production on Sat Oct 17 02:02:00 2020

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

Connected to an idle instance.

SQL> startup

ORACLE instance started.

Total System Global Area 2046820352 bytes

Fixed Size     8622480 bytes

Variable Size   587206256 bytes

Database Buffers  1442840576 bytes

Redo Buffers     8151040 bytes

Database mounted.

Database opened.

SQL> select status,instance_name from v$instance;

STATUS      INSTANCE_NAME

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

OPEN      clone

SQL> select name from v$database;

NAME

---------

CLONE

Screen shots                                                           

                                                                       

 

 


                                                                                                                    

 


ASM

Rename Asm DiskGroup Name On Oracle 12c

  Step1:Startup asm with created asm pfile. startup pfile=$ORACLE_HOME/dbs/init+ASM.ora; Step2:Dismount all disks.  alter diskgroup all dism...