Sunday, February 9, 2014

Important Dynamic Performance View

Some Important Dynamic Performance  View ,

• V$CONTROLFILE: Lists the names of the control files
• V$DATABASE: Contains database information from the control file.
• V$DATAFILE: Contains data file information from the control file
• V$INSTANCE: Displays the state of the current instance
• V$PARAMETER: Lists parameters and values currently in effect for the session
• V$SESSION: Lists session information for each current session
• V$SGA: Contains summary information on the system global area (SGA)
• V$SPPARAMETER: Lists the contents of the SPFILE
• V$TABLESPACE: Displays tablespace information from the control file
• V$THREAD: Contains thread information from the control file
• V$VERSION: Version numbers of core library components in the Oracle server

Oracle installation parameters file

Oracle offers two types of parameter files - INIT.ORA and SPFILE. The default location for a spfile or pfile is $ORACLE_HOME/dbs  .however if you are unsure as to where your spfile is located you can issue the following from SQLPLUS:

SQL> SHOW PARAMETER spfile;
NAME     TYPE       VALUE
--------     -------       ---------
spfile       string       /app/oracle/product/10.2.0.4server/db_1/dbs/spfileictst3f.ora

Create pfile from spfile
If you wish to backup your spfile, you can create a pfile from the spfile which will save the current parameter configuration.

SQL> create pfile='<pfile location>' from spfile;

or

SQL> CREATE PFILE='<pfile location>' FROM SPFILE = '<spfile location>';

Create spfile from pfile
If you want to then revert back to a saved pfile, you can overwrite your current spfile with the following:

SQL> CREATE SPFILE FROM PFILE = '<pfile location>';

or

SQL> CREATE SPFILE='<spfile location>' FROM PFILE='<pfile location>';

Check spfile parameters
To check all the spfile parameters you can issue the following:

SQL> SHOW PARAMETER;

Check pfile parameters
You can either check the pfile parameters, after starting a database up with a pfile, using the above method. Or you can go to the pfile location and either cat or view the file. To change the pfile parameters you can directly edit the pfile using vi from the command line.

Change spfile parameters
Some parameters you can save to memory, for use just within the current session; some you can save within the spfile; and some must be set at both. Parameters saved within the spfile can only be initiated following a database restart.

To save a new parameter within the spfile you issue the following within SQLPLUS:

SQL> ALTER SYSTEM SET <parameter name>='<value>' SCOPE=[SPFILE/MEMORY/BOTH];

Startup database with pfile or spfile
If you want to startup a database using either a different spfile or using a pfile you first need to shut it down. Then you should issue the following:

SQL> CONNECT sys/password AS SYSDBA
SQL> startup pfile='<pfile location>';

or

SQL> CONNECT sys/password AS SYSDBA

SQL> startup spfile='<spfile location>';

Saturday, February 8, 2014

v$active_session_history for system Slow issue

For the past few years I’ve been using a query that I refer to as ash report – recent spike”.
That’s the second thing I do when I get a call of the “the system is slow” type.

The first thing I do is run “top” (or whichever alternative for the OS) and check the overall CPU usage. IO wait on system using sar command.


Below sql is very helpful ,

select round(avg(max(cnt_tot)) over (order by sample_time RANGE BETWEEN INTERVAL '5' minute PRECEDING AND current row)) as avg,max(cnt_tot) as tot,
SAMPLE_time,max(cnt) as cnt, event,substr(sq.sql_text,1), ash.sql_id, ash.sql_child_number chd,/* plsql_entry_object_id, plsql_entry_subprogram_id,
plsql_object_id, plsql_subprogram_id, session_state, qc_session_id, qc_instance_id, blocking_session, blocking_session_status, blocking_session_serial#, event_id, event#, seq#, p1text, p1, p2text, p2, p3text, p3, wait_class, wait_class_id, wait_time, time_waited, xid, current_obj#, current_file#, current_block#, program, module, action, client_id*/to_char(round(sum(elapsed_time) / nullif(sum(executions), 0) / 1000000, 6), '9,999,999,990.999999') as "sec p",round(sum(disk_reads) / nullif(sum(executions), 0), 0) as "disk p", round(sum(buffer_gets) / nullif(sum(executions), 0), 0) as "gets p",round(sum(rows_processed) / nullif(sum(executions), 0), 0) as "rows p",round(sum(cpu_time) / 1000000 / nullif(sum(executions), 0), 3) as "cpu p", sum(executions) as exec, sum(users_opening) as open,sum(users_executing) as e
from ( select sum(count(*)) over (partition by sample_time) as cnt_tot, count(*) as cnt,SAMPLE_time, event,sql_id,sql_child_number from gv$active_session_history where 1=1
and sample_time > sysdate - interval '3' hour
group by event, sql_id,sql_child_number, SAMPLE_time having count(*) >= 2) ash, gv$sql sq
where  ash.sql_id = sq.sql_id(+) and ash.sql_child_number=sq.child_number (+)
group by event,sql_text,ash.sql_id,ash.sql_child_number, ash.SAMPLE_time order by sample_time desc;


It has two “variables” that you can adjust: how far back to look (I use two hours), and how aggressively to look for problems (having count(*) >= 3).

Here’s the explanations of each column:

AVG — the average "load" (active sessions) over a 5-minute interval. This should help you spot a problem when you scroll through the results.
TOT — total "load" (active sessions) for that sample time. RAC users: each RAC node will have its own sample time, within 1 second of each other, but not exactly spot-on. So, even if you have sessions waiting on the same event, they will not be grouped together. I kind of like it this way, for now.
SAMPLE_TIME — self-explanatory
CNT — the number of active sessions waiting on the same event and query
EVENT — the event been waited on
SQL_TEXT — self-explanatory, except when empty which means either not found in shared pool or not available in ASH
SQL_ID — if you need to find the SQL
CHD — the child number being executed

Oracle preferred tool for moving data



As we all know Datapump is the Oracle preferred tool for moving data and is soon will be the only option because traditional. exp/imp utilities will be deprecated. In following sections we will look at how you can use schema, table and data remapping to get more out of this powerful utility.Data remapping allows you to manipulate sensitive data before actually placing the data inside the dump file. This can happen on done at different stages including during schema remap, table remap and remapping
of individual rows inside tables i.e. data remapping. We will look at them one by one in this section.
First setp - 
Setup both your source and target database:

1. sqlplus to source and target DB as sys or system.

2. create directory with the following command:
CREATE DIRECTORY dpump_dir1 AS '/backup/folder complte location';
 Select the table DBA_DIRECTORIES will give you the list of all the directories that you have created using CREATE DIRECTORY command.

Exit sqlplus.

4. Run the following command to export using data pump:
expdp system/[PASSWORD]@[SID] schemas=[SCHEMA] DIRECTORY=dpump_dir1 JOB_NAME=hr DUMPFILE=[SCHEMA]_[SID]_%u.dmp PARALLEL=4
– replace strings in [ ] with the ones for your environment.
5. Run the following command to import using data pump:
impdp system/[PASSWORD]@[SID] schemas=[SCHEMA] DIRECTORY=dpump_dir1 JOB_NAME=hr DUMPFILE=[SCHEMA]_[SID]_%u.dmp PARALLEL=8
– replace strings in [ ] with the ones for your environment.
Notes:
- If your target database is on another host, be aware of the directory setup.
- If you want to import into existing table, simply add TABLE_EXISTS_ACTION=APPEND as part of the command.
- If you only want to import certain tables, use TABLES=[TABLE_NAME1,TABLE_NAME2 ...etc].
- You can also use TABLE_EXISTS_ACTION=TRUNCATE to first truncate the target table before the import.
- You can adjust PARALLEL parameter depending on the number of CPUs on your system.

Schema Remapping

When you export a schema or some objects of a schema from one database and import it to the other then
import utility expects the same schema to be present in second database. For example if you export EMP table of SCOTT schema
and import it to another then import utility will try to locate the SCOTT schema in second database and if not present,
it may create it for you depending on the options you specified.

But if you want to create the EMP table in SH schema instead. The remap_schema option of impdp utility will allow you to accomplish that.
 For example
$ impdp userid=rman/rman@orcl dumpfile=data_pump:SCOTT.dmp remap_schema=SCOTT:SH
Table Remapping
On similar grounds you can also import data from one table into a table with a different name by using
the REMAP_TABLE option. If you want to import data of EMP table to EMPTEST table then you just have to provide the
 REMAP_TABLE option with the new table name.
This option can be used for both partitioned and nonpartitioned tables.

On the other side however table remapping has the following restrictions.
 If partitioned tables were exported in a transportable mode then each partition or subpartition will be moved to a separate table of its own.
 Tables will not be remapped if they already exist even if you specify the TABLE_EXIST_ACTION to truncate or append.
  The export must be performed in non transportable mode.
The syntax of REMAP_TABLE is as follows:
REMAP_TABLE=[old_schema_name.old_table_name]:[new_schema_name.new_table_name]


Adding space on tablespace.


We can  create new tablespaces with the CREATE TABLESPACE command. Before we create the tablespace we should decide below point:

1. How big you wish the tablespace to be.
2. Where you want to put the datafile or datafiles that will be associated with that tablespace.
3. What you want to call the tablespace and the datafiles.

We recommend that you include the following in the datafile name when you create the tablespace:
1. The name of the database
2. The name of the tablespace
3. A number that makes the datafile unique

Select
   fs.tablespace_name                          "Tablespace",
  df.totalspace                               "TOT_SIZE",
  fs.freespace                                "TOT_FREE",
  (round(100 * (fs.freespace / df.totalspace))) pct_Free
from
   (select      tablespace_name,
      round(sum(bytes) / 1048576) TotalSpace
   from
      dba_data_files
   group by
      tablespace_name
   ) df,
   (select
      tablespace_name,
      round(sum(bytes) / 1048576) FreeSpace
   from
      dba_free_space
   group by
      tablespace_name  ) fs
where   (df.tablespace_name = fs.tablespace_name ) and fs.tablespace_name like '%&TBS%' ORDER BY pct_free DESC ;

Tablespace TOT_SIZE   TOT_FREE   PCT_FREE
------------------------------ ---------- ---------- ----------
REPORT_VIEW      500 500    100
UNDOTBS1      446 411     92
SYSAUX      500 191     38
SYSTEM      500 187     37
XXX_AC_TEST     7500 727     10
XXX_AC_XXX     7500 191      3

6 rows selected.
---Here it showing we need space on XXX_AC_xxx tablespace.

SQL> /
Enter value for tbs: XXX_AC_xxx
old  21: where (df.tablespace_name = fs.tablespace_name ) and fs.tablespace_name like '%&TBS%' ORDER BY pct_free DESC
new  21: where (df.tablespace_name = fs.tablespace_name ) and fs.tablespace_name like '%XXX_AC_DEMO%' ORDER BY pct_free DESC

Tablespace TOT_SIZE   TOT_FREE   PCT_FREE
------------------------------ ---------- ---------- ----------
XXX_AC_DEMO     7500 191      3

-- Here we are going to check datafile available name,

SQL> select FILE_NAME,bytes/1024/1024 from dba_data_files where TABLESPACE_NAME ='&TABLESPACE_NAME';
Enter value for tablespace_name: XXX_AC_XXX
old   1: select FILE_NAME,bytes/1024/1024 from dba_data_files where TABLESPACE_NAME ='&TABLESPACE_NAME'
new   1: select FILE_NAME,bytes/1024/1024 from dba_data_files where TABLESPACE_NAME ='XXX_AC_XXX'

FILE_NAME
--------------------------------------------------------------------------------
BYTES/1024/1024
---------------
/shared/XXXREPORT/fro_ac_demo01.dbf
  7500
--- Verfiy avilable space into Mountpoint -

SQL> !df -h /shared/XXXREPORT/fro_ac_demo01.dbf
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdc3             141G   58G   77G  43% /shared
---- checking new datafile name to be eliminate duplicate datafile name into db-
SQL>  select TABLESPACE_NAME,FILE_NAME from dba_data_files where FILE_NAME like '%&file_name';
Enter value for file_name: fro_ac_demo02.dbf
old   1:  select TABLESPACE_NAME,FILE_NAME from dba_data_files where FILE_NAME like '%&file_name'
new   1:  select TABLESPACE_NAME,FILE_NAME from dba_data_files where FILE_NAME like '%fro_ac_demo02.dbf'

no rows selected

--- Below i am adding new datafile into Tablespace-
SQL> alter tablespace XXX_AC_TEST add datafile '/shared/XXXREPORT/fro_ac_test02.dbf' size 1G;

Tablespace altered.

SQL> 


How to create user in MY SQL

Create  a new MySQL user Account mysql > CREATE USER ' newuser '@'localhost' IDENTIFIED BY ' password '...