Sunteți pe pagina 1din 7

Futurematics.Pvt.

Ltd
Performance Tuning

IOT (Index Organized Table)


=======================
sql>create table table_name (col_name datatype primary key,col_name datatype)
organization index;

Partition Tables
=============

<TIP>
inserting multiple values with the help of for loop in sql using simple PL/SQL.
create a sql script:
$] vi insert.sql
then write the following code for inserting in single column table

begin
for I in 1..500000 loop
insert into table_name values(I)
end loop
end
/
save this with .sql extension and run on sql prompt as
sql>@insert.sql

OR
fire this query on sql

insert into <emp>(select rownum from dual connect by level<=500);

Creating Partition Table:


Range Partition :
sql>create table emp(empno number(10),ename varchar2(10),sal number(10))
partition by range(id)
(
partition e1 values less than (10000) tablespace users,
partition e2 values less than (20000) tablespace example,
partition e3 values less than (30000) tablespace users,
partition e4 values less than (MAXVALUE) tablespace users);
Hash Partition
Sql>create table emp(empno number(10),ename varchar2(10),sal number(10))
Partition by hash(empno)
(
Partition p1 tablespace tab1,
Partition p2 tablespace tab2
);
Futurematics.Pvt.Ltd
Performance Tuning

List Partition
Sql>create table sales_by_region(deptno number(10),deptname varchar2(20),state
varchar2(2))
Partition by list(state)
(
Partition north values(‘DL’,’JP’) tablespace tbs1,
Partition south values(‘HY’,’PU’) tablespace tbs2
);
Imp views
dba_tab_partitions
user_tab_partitions
dba_indexes
index_stats
user_indexes

Materialized View

==============

The BUILD clause options are shown below.

 IMMEDIATE : The materialized view is populated immediately.


 DEFERRED : The materialized view is populated on the first requested
refresh.

The following refresh types are available.

 FAST : A fast refresh is attempted. If materialized view logs are not


present against the source tables in advance, the creation fails.
 COMPLETE : The table segment supporting the materialized view is
truncated and repopulated completely using the associated query.
 FORCE : A fast refresh is attempted. If one is not possible a complete
refresh is performed.
 None ????

A refresh can be triggered in one of two ways.


Futurematics.Pvt.Ltd
Performance Tuning

 ON COMMIT : The refresh is triggered by a committed data change in


one of the dependent tables.
 ON DEMAND : The refresh is initiated by a manual request or a
scheduled task.

The QUERY REWRITE clause tells the optimizer if the materialized view should be
consider for query rewrite operations. An example of the query rewrite
functionality is shown below.

The ON PREBUILT TABLE clause tells the database to use an existing table
segment, which must have the same name as the materialized view and
support the same column structure as the query

Check Privileges
Check the user who will own the materialized views has the correct privileges.
At minimum they will require the CREATE MATERIALIZED VIEW privilege. If they are
creating materialized views using database links, you may want to grant
them CREATE DATABASE LINK privilege also.

CONNECT / As sysdba

GRANT CREATE MATERIALIZED VIEW TO scott;


GRANT CREATE DATABASE LINK TO scott;

Basic Syntax
The full syntax description for the CREATE MATERIALIZED VIEW command is
available in the documentation. Here we will only concern ourselves with the
basics.

CREATE MATERIALIZED VIEW view-name


BUILD [IMMEDIATE | DEFERRED]
REFRESH [FAST | COMPLETE | FORCE ]
ON [COMMIT | DEMAND ]
[[ENABLE | DISABLE] QUERY REWRITE]
[ON PREBUILT TABLE]
Futurematics.Pvt.Ltd
Performance Tuning

AS
SELECT ...;

Create Materialized View


Connect to the materialized view owner and create the database link and the
materialized view itself.

CREATE MATERIALIZED VIEW emp_mv


BUILD IMMEDIATE
REFRESH FORCE
ON DEMAND
AS
SELECT * FROM employee, dept where dept.dept_id=employee.dept_id;

CREATE MATERIALIZED VIEW emp_mv


BUILD IMMEDIATE
REFRESH COMPLETE
ON COMMIT
AS
SELECT * FROM employee, dept where dept.dept_id=employee.dept_id;

CREATE MATERIALIZED VIEW emp_mv


BUILD IMMEDIATE
REFRESH FAST
ON DEMAND
AS
SELECT * FROM employee, dept here dept.dept_id=employee.dept_id;

Remember to gather stats after building the materialized view.

BEGIN
Futurematics.Pvt.Ltd
Performance Tuning

DBMS_STATS.gather_table_stats(
ownname => 'SCOTT',
tabname => 'EMP_MV');
END;
/

Create Materialized View Logs

Since a complete refresh involves truncating the materialized view segment


and re-populating it using the related query, it can be quite time consuming
and involve a considerable amount of network traffic when performed against
a remote table. To reduce the replication costs, materialized view logs can be
created to capture all changes to the base table since the last refresh. This
information allows a fast refresh, which only needs to apply the changes
rather than a complete refresh of the materialized view.

To take advantage of the of the fast refresh, connect to the master instance
and create the materialized view log.

CONNECT scott/tiger

CREATE MATERIALIZED VIEW LOG ON scott.emp


TABLESPACE users
WITH PRIMARY KEY
INCLUDING NEW VALUES;

Refresh Materialized Views


If a materialized view is configured to refresh on commit, you should never
need to manually refresh it, unless a rebuild is necessary. Remember,
refreshing on commit is a very intensive operation for volatile base tables. It
makes sense to use fast refreshes where possible.

For on demand refreshes, you can choose to manually refresh the materialized
view or refresh it as part of a refresh group.
Futurematics.Pvt.Ltd
Performance Tuning

A materialized view can be manually refreshed using the DBMS_MVIEW package.

EXEC DBMS_MVIEW.refresh('EMP_MV');

The following code creates a refresh group defined to refresh every minute
and assigns a materialized view to it.

BEGIN
DBMS_REFRESH.make(
name => 'SCOTT.MINUTE_REFRESH',
list => '',
next_date => SYSDATE,
interval => '/*1:Mins*/ SYSDATE + 1/(60*24)',
implicit_destroy => FALSE,
lax => FALSE,
job => 0,
rollback_seg => NULL,
push_deferred_rpc => TRUE,
refresh_after_errors => TRUE,
purge_option => NULL,
parallelism => NULL,
heap_size => NULL);
END;
/

BEGIN
DBMS_REFRESH.add(
name => 'SCOTT.MINUTE_REFRESH',
list => 'SCOTT.EMP_MV',
lax => TRUE);
END;
/

Cleaning Up
Futurematics.Pvt.Ltd
Performance Tuning

To clean up we must remove all objects.

CONNECT scott/tiger
DROP MATERIALIZED VIEW emp_mv;
BEGIN
DBMS_REFRESH.destroy(name => 'SCOTT.MINUTE_REFRESH');
END;
/
CONNECT scott/tiger
DROP MATERIALIZED VIEW LOG ON scott.emp;

S-ar putea să vă placă și