You are on page 1of 9

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

***********************OrAclE SesSioN 4************************


----------------
*********session.txt*******

Hi! In today session 4 we will cover the topics under Database


Security ,Performance Tuning ,Backup & Recovery

Okay! now move on to the session....

The first topic is....

Database Security :
------------------

It helps to secure the data inside the database.

It allows certain privileges using Grant and Revoke statements.

The GRANT command is used for providing the authorization to the users and then
REVOKE command is used for withdrawing the authorization.

It allows us to access table from different schemas.

It used to give privileges to the group of users.

_______________________________________________________________

And then the next topic is....How

To CREATE A NEW USER AND PASSWORD:


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

The syntax for creating a user....

Syntax: Create user user_name identified by password.


-------

SQL>create user santhosh identified by peri123;

After creating certain username u must grant permission for login,here it shows
some permission i.e.,create session.so grant tat permission.

________________________________________________________________

Change password:
---------------

There are two methods to changing the password...

Method 1:
---------
Syntax: Alter user user_name identified by new_password.
-------

SQL>alter user santhosh identified by oracle123;


Syntax: Connect user/old_password
-------

Method 2:
---------

SQL> password
old password : old_password
new password : new_password
retype password : new_password
password change

________________________________________________________________

And then the next topic is ....

PRIVILEGE:
----------

Provides permission to users.

Types of Privileges are.....

* Object Privileges
* System Privileges

Object Privileges:
------------------

It allows to perform certain action on the database objects.


Example : INSERT, UPDATE, DELETE, SELECT, EXECUTE.

System privileges:
------------------

It allows to perform certain action with in the database.

Example : CREATE VIEW, CREATE TABLE, CREATE SESSION

##In object priv lets consider u have many table.If im giving object privilg like
insert,update,delete,then its not im grant permission to all table under db,u have
to mention to which table u have to grant permission.
________________________________________________________________

PROVIDING AUTHORIZATION:
------------------------

The GRANT command is used for providing the authorization to the users...some of
the grant commands are....

SQL>grant create session,create table to santhosh;

SQL>grant create session to username ; #to log into the user...

SQL>grant create table to username; #to create a table...

SQL>>grant resource to username; #To give privileges on tablespace for 'USERS'...


#grant list_of_privileges on object_name to user_name;

SQL>grant insert, update, delete, select on emp to username;

#To insert into the table on otheruser...

SQL>insert into test.emp values(8,'nim') ))))))

For giving privileges to both obj and sys use

SQL>grant all privileges to username;

______________________________________________________________

Prevent Permissions:
--------------------
REVOKE command is used for withdrawing the authorization.

SQL>revoke create session,create table from santhosh;

SQL>revoke all privileges from username;

To Drop the username & the table:

SQL>drop user username cascade;


________________________________________________________________

Roles:
------

Role is an object created with set of privileges and password in order to maintain
user permissions effectively.

##instead of granting each permission separately,u can also store certain


permission under name.tat is role.

##so u already create some role and then u grant tat role for user,

SQL>create role developer identified by peri123;#create role

SQL>grant create session,create table to developer;#grant permsn

SQL>grant develper to santhosh;


________________________________________________________________

PERFORMANCE TUNING:
---------------------

Performance tuning means what??

Is the improvement of system performance,writing efficient SQL,

Some times if you want to delete the whole table values okay..we can use truncate
instead of delete ..This is one kind of performance tuning..
________________________________________________________________
Query Analysis:
---------------
Query analysis is helps to improve overall performance of query, which will speed
up the database functions..okay...

If we are doing any query(select* from emp)it just return some o/p.then we don't
know what is happening in execution then what happening in background ..

So Analysing means what...Analysing the query

So to know what is happening in background nahh

In Oracle it provides three methods for EXECUTION PLAN

1) Using AUTOTRACE command


2) Using EXPLAIN PLAN statement
3) Using TRACE files

1.Using AUTOTRACE command:


--------------------------
The autotrace provides feedback including the returned rows, execution plan, and
statistics.

So first what i do nahhh.. I enable a autotrace to on;

SQL> set autotrace on;


SQL> select * from divi;

ID NAME
---------- --------------------
1 Ragav
2 Kiru
3 nandy
4 Harsh
5 Zubair
5 Anchal
4 Ankit

7 rows selected.

Elapsed: 00:00:00.01

Execution Plan
----------------------------------------------------------
Plan hash value: 1898697977

--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 25 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DIVI | 1 | 25 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
578 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
7 rows processed

SQL>

Here what is plan hash value nahh:


-----------------------------------
It is a value assigned to the sql statement and it store into the library cache..
cache means temporary memory right.. So whenever a user request that query for
another time nahh... it fetch that hash value and execute the same execution
plan..This is hash value...

Explanation:
-----------

So ,If we simply give autotrace on nahh..it gives all rows, and execution plan.It
show u execute select st,tat st is accessing full table and table name and total
num of rows and its statistics okay..

We can also give specifically...

If i give autotrace explain nahh...

with this autotrace it display all rows in table then exec plan and
statistics,which i didn't want to display,if you want to see only exec plan then
use explain plan method.

________________________________________________________________

Next is EXPLAIN PLAN


------------
SQL> set autotrace off;

To explain a SQL statement, use the EXPLAIN PLAN FOR before the statement. For
example:

SQL> explain plan for select * from divi;


Explained.

This explains the plan into the PLAN_TABLE .So The PLAN_TABLE is What is nahh
automatically created as a global temporary table to hold the output of an EXPLAIN
PLAN .

Now, You can then select the execution plan from PLAN_TABLE by using
dbms_xplan.display.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------
Plan hash value: 1898697977
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 25 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DIVI | 1 | 25 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------

8 rows selected

So ,by using autotrace or explain plan we can see the performance by using time or
CPU cost okay...

Incase if you want to store all this exec plan u must have to save all this in some
files.

________________________________________________________________
(((((((((
Now Index on Performance tuning:
--------------------------------
SQL> select id,name from divi where id=1;

ID NAME
---------- --------------------
1 Ragav

Elapsed: 00:00:00.01

Execution Plan
----------------------------------------------------------
Plan hash value: 1898697977

--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 25 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| DIVI | 1 | 25 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):


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

1 - filter("ID"=1)

SQL> create index i_di on divi(id);

SQL> select id,name from divi where id=1;

ID NAME
---------- --------------------
1 Ragav

Elapsed: 00:00:00.01

Execution Plan
----------------------------------------------------------
Plan hash value: 1898697977
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 25 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| DIVI | 1 | 25 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):


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

1 - filter("ID"=1)

select id,name from divi where id=1;

ID NAME
---------- --------------------
1 Ragav
xecution Plan
----------------------------------------------------------
Plan hash value: 2206057246

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

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time


|

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

| 0 | SELECT STATEMENT | | 1 | 25 | 1 (0)| 00:00:


01 |

| 1 | TABLE ACCESS BY INDEX ROWID| DIVI | 1 | 25 | 1 (0)| 00:00:


01 |

|* 2 | INDEX RANGE SCAN | I_DI | 1 | | 1 (0)| 00:00:


01 |

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

Can you see the performance is increasing...That First CPU cost is 4 after creating
index for column now the CPU cost is 3

Here We use small table so,If we compare the performance by using time,CPU nah.. it
remains same right..But we can see little difference...

If we have large table is not a problem we can see the difference here we using
small small tables right!.. so we can't see the as much performance improvement
here...okay....
________________________________________________________________

TRACE FILE:
-----------
A trace file is a file containing a execution status during some process

So,trace file is a normal file to store the execution plan

##but i don't have permission for using this trace file.i will show how to work
with this trace file and where it is located inside oracle.

##If u want to work with this u have some tools,only if u have privilege to work
with that tool then only u can see the content of tat file.

To open a trace file:


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

SQL>alter session set sql_trace=true;

but here i cant find the name of this trace file since it is going to assign some
pseudo names..

SQL>alter session set tracefile_identifier='santhosh';

##here we naming our file,because it hard to find which par file is having exection
file for ur table.or else it can assign name randomly.Bcoz in oracle there will be
many trace file.By using name we easily identified our trace file.

SQL>select * from divi where name='Ragav';

SQL>select * from divi where id=1;

now the execution plans for this two queries will be stored in the trace file.

To close the trace file:


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

SQL>alter session set sql_trace=false;

but we dont have the permission to open this files..

Let me tel u the path to view the trace file.

#trace file is very secure.is not like normal file.it stored in the route directory
of oracle.

SQL>host
[test@localhost ~]$ cd $ORACLE_BASE
cd admin/
cd XE
ls
cd udump/
ls

but we dont have the permission to open this files..

cat filename.trc

we need tool to open this file.

even though you read this ,it will be in a unreadable form

for this.
tkprof utility(transient kernel profiler) by this we can make trace files in a
readable form...

[test@localhost]tkprof filename.trc output=name.txt

________________________________________________________________

BACKUP AND RECOVERY


--------------------
##In this session i will show wat is backup recovery,in next session i will show
how to perform this by using some tool

Backup and recovery will protect our database against data loss and can be used to
recover the data.

A backup is like a copy of data.This copy can include important parts of the
database, such as the control file and datafiles so that we can recover the
data,when an unexpected data loss occured.

SQL>!cat backup.txt

Back up & Recovery


==================
-to protect our DB against data loss and to recover the data
-backup=>copy of data(database files(.df),control files(.ctl) and redolog(.log)

Types: 1.Cold Backup(Offline Backup) 2.Hot Backup(Online Backup)

Cold backup: It is taken when the database is closed and not available to users.
All files of the database are copied (image copy). The datafiles cannot be changed
during the backup as they are locked, so the database remains in sync upon restore.

Hot backup: While taking the backup, if the database remains open and available to
users then this kind of back up is referred to as hot backup. Image copy is made
for all the files. As, the database is in use the entire time, so

You might also like