Professional Documents
Culture Documents
Oracle 2 PM
Oracle 2 PM
Materialized View:
• M.View is a Database Object.
• M.View is not a virtual table. It contains data
physically.
• It holds result of SELECT query.
• It precomputed result.
• It is mainly in Dataware Housing [DWH] to
maintain summarized data physically.
• If apply any changes to base table, it will not
be applied to m.view. That is why m.view must
be refreshed.
3 ways:
• ON DEMAND [Default] =>
○ Ex: SQL> EXEC dbms_mview.refresh('MV1');
• ON COMMIT =>
○ Ex: SQL> COMMIT;
Sales
Example:
Syntax:
3 Types:
• COMPLETE [default]
• FAST
• FORCE
COMPLETE:
• In COMPLTE refresh,
○ truncates all records form m.view.
○ then runs SELECT query associated with
M.view.
ORACLE2PM Page 2
M.view.
○ This new result will be filled in m.view.
NOTE:
When large amounts of data is there, performance
will be degraded.
FAST:
ORACLE2PM Page 3
1002 10 15000 20 40000
1003 20 12000
1004 20 8000
1005 10 10000
1006 20 20000
1005 10 10000
1006 20 20000
FORCE:
• it gives first priority for FAST refresh. If m.view log file
is not there, it performs COMPLETE refresh.
• Here, refresh will not be failed.
3 refresh types:
user_mviews:
• "user_mviews" is a system table.
• It maintains all m.views information.
DESC user_mviews;
ORACLE2PM Page 5
SELECT mview_name, query, last_refresh_type
FROm user_mviews;
Syntax:
Ex:
DROP MATERIALIZED VIEW mv1;
Altering m.view:
JAVA DEVELOPER:
SELECT deptno,sum(Sal)
FROM emp
GROUP BY deptno; -- from emp table
ORACLE2PM Page 6
GROUP BY deptno; -- from emp table
SELECT deptno,sum(Sal)
FROM emp
GROUP BY deptno; --retrieves from m.view mv10
ORACLE2PM Page 7
Indexes
Tuesday, December 13, 2022 2:13 PM
ORACLE
INDEX
Indexes:
• INDEX is a Database Object.
• INDEX is used to improve the performance of
data retrieval.
• Example: To refer JOINS topic in ORACLE book,
we use INDEX. With the help of INDEX , we can
refer the chapter quickly.
• Index will be created on a column which is
frequently used in WHERE clause.
• If INDEX is created, performance of data
retrieval will be improved.
SELECT empno,ename,sal
FROM emp
WHERE sal>3000; --Table Scan => slower
SELECT empno,ename,sal
FROM emp
WHERE sal>3000; --Index Scan => faster
WHERE sal>3000
INDEX ON SAL => INDEX SCAN
Sal
sal>3000 => place it at right side
----------
sa<=3000 => place it at left side
4000
5000
1800 3000
2500
4500
3500 2000 4000
3300
4500
2500
1000 1000 * 2500 ** 3300 * 4500 **
1200 1200 * 3500 * 5000 *
1300 1300 * 4000 *
1800 *
ORACLE2PM Page 9
Types of Indexes:
2 Types:
• B-Tree Index / Normal Index
• Bitmap Index
• Bitmap Index:
If Binary Tree is not created for the index then it
is called "Bitmap Index".
Simple Index:
If index is created on one column then it is called
"Simple Index".
Ex:
CREATE INDEX i1 ON emp(Sal);
Composite Index:
If index is created on multiple columns then it is called
"Composite Index".
Frequent operation:
deptno and job
deptno=10 job='MANAGER'
deptno=20 job='CLERK'
deptno=30 job='SALESMAN'
Example:
CREATE INDEX i2 ON emp(deptno,job);
ORACLE2PM Page 10
SELECT * FROM emp
WHERE job='CLERK'; -- Table Scan / Index Scan
Unique Index:
This index will be created on a column which has
unique values.
Syntax:
NOTE:
We have no need to create index on Primary Key
column.
When we create a table with Primary Key,
implicitly one INDEX will be created on Primary
Key Column.
user_tables
user_views
user_mviews
user_sequences
user_indexes
CREATE TABLE t1
(
f1 NUMBER(4) CONSTRAINT zz PRIMARY KEY,
f2 VARCHAR2(10)
);
Example:
CREATE UNIQUE INDEX i3 ON dept(dname);
Function-Based Index:
ORACLE2PM Page 11
Function-Based Index:
If index is created based on function or expression then it is called
"Function-Based Index".
Example:
Frequent operation is retrieving data based on ename
WHERE ename='SMITH'
WHERE ename IN('ALLEN','BLAKE')
WHERE sal*12>30000
WHERE sal*12<50000
B-Tree Index:
When we create the index, If B-Tree is created then it
ORACLE2PM Page 12
• When we create the index, If B-Tree is created then it
is called "B-Tree Index".
• It stores values & Row IDs
• B-Tree Index will be created on High Cardinality
Columns.
• Ex: ename, sal, empno
Bitmap Index:
• It stores bits [0s & 1s]
• Every bit will be associated [mapped] with Row ID.
• In this, bits will be converted row ids, goes to those
records & picks the data.
• Bitmap Index will be created on Low Cardinality
Columns.
GENDER DEPTNO
--------------- -------------
M 10
M M 30 10
F F 30 20
M 20 30
F 20
F 10
F 10
M 20
F 30
EMPNO ENAME
-------------- ---------
1001 SRINU
1002 ARUN
1003 20 distinct values VIJAY
1004 SRINU
. .
. .
1020
ORACLE2PM Page 13
CREATE BITMAP INDEX <index_name>
ON <table_name>(<column>);
WHERE gender='M'
WHERE gender='F'
WHERE
GENDER gender='M'
--------------- BI1 => Bitmap Index
M
M M F
F 1 0
M 1 0
F 0 1
F
1 0
F
0 1
M
F 0 1
0 1
ORACLE2PM Page 14
0 1
M
F 0 1
0 1
1 0
0 1
• It stores values & Row IDs • It stores bits [0s & 1s]
USER_INDEXES:
• It is a system table / built-in table
• It maintains all Indexes Information
DESC user_indexes;
Dropping Index:
Syntax:
DROP INDEX <index_name>;
Ex:
DROP INDEX i1;
deptno 10 20 30
------------ 1 0 0
10 0 0 1
30
30
20
10
20
ORACLE2PM Page 16
Synonyms
Thursday, December 15, 2022 2:20 PM
SYNONYM:
• SYNONYM is a DB Object.
SELECT ename as A, sal as B
• It is used to give alias name [alternative name] for the
FROM emp;
DB objects like tables.
• Table alias is temporary. Its scope will be limited to
A B
that query only. To give permanent alias name we use
------- -----------
SYNONYM.
• Table alias is Temporary. SYNONYM is permanent.
Column alias is temporary
Advantages:
SELECT e.ename,d.dname
• It makes table name short.
FROM emp e, dept d …
○ Ex: EMPLOYEE_SALARY_DETAILS => e
…….;
c##userA
SELECT * FROM c##oracle2pm.emp
creates a synonym
c##oracle2pm.emp => e
Types of Synonyms:
2 Types:
• Private Synonym
• Public Synonym
Private Synonym:
• It is created by the user
Example:
Log in as DBA:
username: system
password: nareshit
ORACLE2PM Page 17
GRANT CREATE SYNONYM TO c##oracle2pm;
Log in as c##oracle2pm:
SELECT * FROM e;
Public Synonym:
• It is created by DBA
Log in as DBA:
log in as c##userA:
SELECT * FROM e;
log in as c##userB:
SELECT * FROM e;
log in as c##userC:
SELECT * FROM e;
ORACLE2PM Page 18
Dropping Private Synonym:
Syntax:
Ex:
DROP SYNONYM e;
Syntax:
Ex:
DROP public synonym e;
user_synonyms:
• It is a system table / built-in table
• It maintains all synonyms information.
DESC user_synonyms;
SELECT synonym_name,table_name
FROM user_synonyms;
ORACLE
SQL Database
PL/SQL Tables
Rows & COlumns
SQL:
• Non-Procedural Language.
• Non-Procedural means, we will not write any set
of statements or programs.
ORACLE2PM Page 19
of statements or programs.
• Just we write queries
• Query => is a request i.e. sent to DB SERVER
5 Sub Languages:
DDL CREATE
ALTER
DROP
FLASHBACK
PURGE
TRUNCATE
RENAME
DRL / DQL SELECT
DML INSERT => emp joined
UPDATE => emp promoted
DELETE => emp resigned
INSERT ALL
MERGE
DCL GRANT
REVOKE
TCL COMMIT [save]
ROLLBACK [undo all]
SAVEPOINT
Built-In Functions:
NVL()
NVL2()
Clauses in SELECT:
ORACLE2PM Page 20
FROM FROM emp
WHERE WHERE sal>3000
Sub Queries:
writing query in another query
Types:
Single Row Sub Query Sub query returns 1 row
Multi Row Sub query returns multiple
rows
Correlated Outer query passes value to
inner query
Inline View We write Sub Query in FROM
clause
Scalar We write sub query in
SELECT clause
Joins:
Types:
SET OPERATORS:
UNION
UNION ALL
INTERSECT
ORACLE2PM Page 21
INTERSECT
MINUS
VIEWS:
View => virtual table
no physical data
holds SELECT query
Advantages:
• Security
• simplifies queries
Disadvantage:
• Less performance
Sequences:
generate sequential integers
cid
-------- NEXTVAL
123456 CURRVAL
123457
123458
Materialized View:
• it is not virtual table
• it holds data physically
• it is used to maintain summary of detailed
tables physically
• it must be refreshed
Advantages:
• It improves the performance
• can maintain local copy of remote database
Indexes:
Index is used to improve the performance of
data retrieval
Index will be created on column which is
frequently used in WHERE clause
ORACLE2PM Page 22
Bitmap Index => stores 0s and 1s
Synonym:
to give permanent alias name for DB object
use SYNONYM
ORACLE2PM Page 23