0% found this document useful (0 votes)
30 views

Untitled

The document discusses views, materialized views, table partitioning, and indexing in Oracle databases. It defines views as virtual tables created by queries that do not store data. Materialized views physically store query results. The document compares views and materialized views. It describes different types of table partitioning including range, list, and hash partitioning. It also provides examples of creating partitions and indexes.

Uploaded by

MOHAN R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Untitled

The document discusses views, materialized views, table partitioning, and indexing in Oracle databases. It defines views as virtual tables created by queries that do not store data. Materialized views physically store query results. The document compares views and materialized views. It describes different types of table partitioning including range, list, and hash partitioning. It also provides examples of creating partitions and indexes.

Uploaded by

MOHAN R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

DAY 12

 View and how to create it ?


 Materialized View and how to create it ?

View, Partition, Indexing  Diff between View and MV


 Table Partition
 Types of Table Partition
 Range Partition
 List Partition
 Hash partition
 Indexing
View

 An Oracle VIEW, in essence, is a virtual table that does not physically exist.
Rather, it is created by a query joining one or more tables.
 A view is a virtual table because you can use it like a table in your SQL queries.
 Every view has columns with data types so you can execute a query against views
or manage their contents (with some restrictions) using the INSERT, UPDATE,
DELETE, and MERGE statements.
 Unlike a table, a view does not store any data.
Continue..
Continue..
Also, one can use below pages for more detail:
https://www.oracletutorial.com/oracle-view/oracle-create-view/

Views are generally used when data is to be accessed infrequently and data in table gets
updated on frequent basis.

Example:
CREATE OR REPLACE VIEW SAMPLE_VW
AS
SELECT Department, SUM (Salary) AS TotalSal
FROM EmpDept
GROUP BY Department;
Continue..
When you create view, it just stores the name of the query, it won't store the data of the
result.
When you query a view:
SELECT * FROM ViewName
Since the view doesn't have any physical data, it always fetches from the base table.

Assignments:

1. What is the purpose of using the force option while creating views?
2. Create a view in your own schema and perform DML operations
3. Create a complex view. Can we perform DML operations on this, if so how can we
do it?
Quiz

Question: Can you update the data in an Oracle VIEW?

Question: A view always fetch up to date data from the table because it runs it’s query
each time it receives a call, is this statement correct?
Question: Does the Oracle View exist if the table is dropped from the database?
Materialized View

A materialized view in Oracle is a database object that contains the results of a query.

A materialized view, is a table segment whose contents are periodically refreshed based on a query,
either against a local or remote table.
Materialized View Example

CREATE MATERIALIZED VIEW SEARCH_CATEGORY_SALES_MV


AS
SELECT CASE
WHEN category_path1 IS NULL THEN 'Un-Categorized'
ELSE category_path1
END AS category_path1,
SUM (qty_sold * sale_price * rate_mult) AS total_sales_usd
FROM dwrp.dw_ods_sales os
INNER JOIN dwrp.ol_products op ON op.inv_item_id = os.inv_item_id
LEFT OUTER JOIN dwrp.exchange_rate_daily ex
ON os.invoice_dt = ex.cur_date
AND ex.cur_date BETWEEN '31-jan-2019' AND '31-jan-2020'
WHERE os.invoice_dt BETWEEN '31-jan-2019' AND '31-jan-2020’

GROUP BY category_path1;
Difference b/w View and Materialized View

View Materialized View

Views are not stored physically on the disk MV are stored on the disk.

View can be defined as a virtual table created MV is a physical copy, picture or snapshot of
as a result of the query expression. the base table.
View is just a display; it do not require memory MV utilizes the memory space as it stored on
space. the disk.
No need to refresh, since the data is directly MV can be set to refresh manually, on a set
fetched from base tables. schedule, or based on the database detecting a
change in data from one of the underlying
tables.

Reference Link: https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_6002.htm


Table Partition

Partition allows table, index or index organized tables to be subdivided into smaller
pieces and each piece of table, index or index organized table is called as Partition.

Following are Advantages of Partition:


 Increase Performance
 Increases availability
 Enable storage cost optimization
 Enables Simpler management
When to partition the table?

 Table should be greater than 2 GB.


 Tables which contain historical data in which new data will be added into newest partition.
The real-life example of this is historical table which contains updatable data for one-
year other data is read only.
 When contents of the table needs to be distributed in different storage devices.
 When table performance is weak, and we need to improve performance of application.
 Each row in partitioned table is unambiguously assigned to single partition table. The
Partitioning key is comprised of one or more columns that determine the partition where
each row will be stored.
Types Of Table partitioning:
There are following types of Table partition:
C:\Users\swsingh\
 Range Partition Desktop\Codes\RangePart

 List Partition
 Hash Partition

Syntax:
1. Range Partition:
Create table TableName
When in the specified table the data is based on the (ColumnName1 datatype(size)….
specific date range, and it is properly divided in some ColumnName-n datatype(size))
range then user should go for the partitioned named as Partition by range(Column needs to be partitioned)
‘Range Partition’. (Partition PartitionName1 values less than(value1)….
Partition PartitionName-n values less than(maxvalue));
2. List Partition:

List partition enables you to explicitly control how the partition of tables need to be done by specifying list
of distinct values as partition key in description of each partition.

Syntax:
ListPartition.sql
Create table TableName
(ColumnName1 datatype(size)….
ColumnName-n datatype(size))
Partition by range(Column needs to be partitioned)
(Partition partitionName1 values less than(value1)….
Partition partitionName-n values less than(maxvalue));
3. Hash Partition:
Hash partitioning is type of partitioning where data is partitioned by using the hashing
algorithms. Different hashing algorithms are applied to partition key that you identify.

Syntax:
HashPartition.sql

Create table TableName


(ColumnName1 datatype1…
ColumnName n datatype ‘n’)
Partition by Hash(columnName)
Partitions partitionNumber)

Reference Link: https://www.complexsql.com/table-partitioning/


How to create partition on non partitioned tables?
If you have Employee table which is not partitioned, and you need to add the partition to the
Employee table. There is direct way to add the partition to the table. The ‘Alter Table Modify’
clause is used to add the partition to the existing non partitioned table. In addition, we need to
use the keyword named ‘Online’.

Syntax: Suppose you want to add the partition to the existing table
named ‘Employee’ and partition it by using City column.
ALTER TABLE TableName Query used:
Modify ALTER TABLE Employee
Partition by PartitionName(Column_name)( Modify
Partition PartitionName values …… Partition by LIST(Employee_City)
) online; (
Partition P_Kolhapur values(‘Kolhapur’),
Partition P_Sangali values(‘Sangli’),
Partition P_OTH values(default)
) online;
Indexing

SQL Indexes are used in relational databases to quickly retrieve data. They are like indexes
at the start/end of the books whose purpose is to find a topic quickly. SQL provides Create
Index, Alter Index, and Drop Index commands that are used to create a new index, update
an existing index, and delete an index in SQL Server.

 Data is internally stored in a SQL Server database in “pages” where the size of each page
is 8KB.
 A continuous 8 pages is called an “Extent”.
 When we create the table then one extent will be allocated for two tables and when
that extent is computed it is filled with the data then another extent will be allocated,
and this extent may or may not be continuous to the first extent.
Reference Link: https://docs.microsoft.com/en-us/sql/relational-databases/indexes/indexes?view=sql-
server-ver15

You might also like