Open In App

How to Compare Indexes of Tables From Two Different Databases in Oracle?

Improve
Improve
Like Article
Like
Save
Share
Report

Structured Query Language supported by RDBMS(Relation Database Management Systems) such as Oracle, Mysql, PostgreSQL, and SQL Server. All relational database system support objects such as databases, tables, schema, indexes, views, functions, procedures, and many more.

RDBMS system performs best when the schema is on write. To make these systems perform well on the schema on reading we have indexes that help in improving the data read or search performance.

Comparing indexes of tables from two different databases requires their data and schema structure to be compared first. In a hierarchy of database management systems, we have a schema, databases, tables, columns then indexes.

Consider two different databases “Department_HR”  and “Department_CR” 

Step 1: Creating users for the databases.

create user source PASSWORD 's';
ALTER USER source WITH SUPERUSER;

 

create user target PASSWORD 't';

 

Step 2: Creating Database “Department_HR” and “Departement_CR”

create database Department_HR OWNER source;

 

create database Department_CR OWNER target;

 

Step 3: Create table “Employees”  in Database “Department_HR” and “Department_CR”

Create table Department_HR.Employees
(  EMPLOYEE_ID int,
  FIRST_NAME varchar(120),
  LAST_NAME varchar(120),
  EMAIL varchar(120),
  HIRE_DATE date,
  JOB_ID  varchar(120),
  SALARY float,
  COMMISSION_PCT float,
  MANAGER_ID int,
  DEPARTMENT_ID int
);

 

Create table Department_CR.Employees
(  EMPLOYEE_ID int,
  FIRST_NAME varchar(120),
  LAST_NAME varchar(120),
  EMAIL varchar(120),
  HIRE_DATE date,
  JOB_ID  varchar(120),
  SALARY float,
  COMMISSION_PCT float,
  MANAGER_ID int,
  DEPARTMENT_ID int
);

 

Step 4: Insert data in the department_HR.employees and department_CR.employees tables

insert into employees values(100,'Steven',
'King','SKING','17-Jun-03','AD_PRES',24000, null ,90);
insert into employees values(101,'Neena','
Kochhar','NKOCHHAR','21-Sept-05','AD_VP',17000,100,90);
insert into employees values(102,'Lex','DeHaan',
'LDEHAAN','13-Jan-01','AD_VP',17000,100,90);
insert into employees values(103,'Alexander',
'Hunold','AHUNOLD','03-Jan-06','IT_PROG',9000,102,60);
insert into employees values(104,'Bruce','
Ernst','BERNST','21-May-07','IT_PROG',6000,103,60);
insert into employees values(105,'David',
'Austin','DAUSTIN','25-Jun-05','IT_PROG',4800,103,60);
insert into employees values(106,'Valli',
'Pataballa','VPATABAL','05-Feb-06','IT_PROG',4800,103,60);
insert into employees values(107,'Diana',
'Lorentz','DLORENTZ','07-Feb-07','IT_PROG',4200,103,60);
insert into employees values(108,'Nancy',
'Greenberg','NGREENBE','17-Aug-02','FI_MGR',12008,101,100);

Step 5: Select the inserted data in the department_HR.employees and department_CR.employees table

select * from department_HR.employees;

 

select * from department_CR.employees;

 

Step 6: Compare the indexes from both databases we must have indexes on the table’s column

create index indx_dept_emp_id on
department_HR.employees(employee_id);
create index indx_dept_manager_id 
on department_HR.employees(manager_id);

 

create index indx_dept_emp_id on 
department_CR.employees(employee_id);
create index indx_dept_manager_id on 
department_CR.employees(manager_id);

 

Step 7: Create a link between two different databases to compare the indexes

Login to database  Source database Department_HR and create a DB LINK

  • Create an extension to establish a connection between two databases
CREATE EXTENSION dblink;

 

  • Create a server to make a connection to the target database.
CREATE SERVER server_Department_CR_remote 
FOREIGN DATA WRAPPER 
 dblink_fdw OPTIONS (host 'localhost', 
 dbname 'Department_CR', port '1521');

 

  •  Create user mapping from the source database for the target database.
CREATE USER MAPPING FOR Department_HR SERVER
 server_Department_CR_remote OPTIONS (user 'target',password 't');

 

  •  Provide select access to the source database on the remote server.
GRANT USAGE ON FOREIGN SERVER server_Department_CR_remote TO source;

 

  • dblink_connect function to establish the connection from the source to the target DB.
SELECT dblink_connect('myconn', 
'dbname=department_cr port=1521 
host=localhost user=target password=t');
 

Step 8: Query to compare the indexes.

# Login to source database
select distinct i1.index_name from user_ind_columns i1
where table_name='Department_HR' and not exists 
(select * from user_ind_columns i2 where 
i2.table_name = 'Department_CR' and i1.column_name = i2.column_name
and i1.column_position = i2.column_position);

Output:

Index_name
----------
indx_dept_emp_id 
indx_dept_manager_id 
 


Last Updated : 01 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads