http://mysqldatabaseadministration.blogspot.com/2006/02/innodb-or-myisam-whats-your-preference.html
If there are many modifications of the data, it's said that InnoDB works faster because it uses row locking instead of table locking, like MyISAM. However, if there are mainly SELECT statements, a MyISAM table might be faster.
http://dev.mysql.com/doc/refman/5.0/en/converting-tables-to-innodb.html
Make sure that you do not fill up the tablespace: InnoDB tables require a lot more disk space than MyISAM tables. If an ALTER TABLE operation runs out of space, it starts a rollback, and that can take hours if it is disk-bound.
This blog is a note for self learning. Some writings are done by myself and some are collected, just to keep things in a organized way.
8.17.2009
6.29.2009
DDL - DML - DCL - TCL
DDL
Data Definition Language (DDL) statements are used to define the database structure or schema. Examples:
DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:
DCL
Data Control Language (DCL) statements. Some examples:
TCL
Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
DML commands can not be rollback when a DDL command is executed immediately after a DML. DDL after DML means "auto commit".
Data Definition Language (DDL) statements are used to define the database structure or schema. Examples:
- CREATE - to create objects in the database
- ALTER - alters the structure of the database
- DROP - delete objects from the database
- TRUNCATE - remove all records from a table, including all spaces allocated for the records
- COMMENT - add comments to the data dictionary
- RENAME - rename an object
DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:
- SELECT - retrieve data from the a database
- INSERT - insert data into a table
- UPDATE - updates existing data within a table
- DELETE - deletes all records from a table, the space for the records remain
- MERGE - UPSERT operation (insert or update)
- CALL - call a PL/SQL or Java subprogram
- EXPLAIN PLAN - explain access path to data
- LOCK TABLE - control concurrency
DCL
Data Control Language (DCL) statements. Some examples:
- GRANT - gives user's access privileges to database
- REVOKE - withdraw access privileges given with the GRANT command
TCL
Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
- COMMIT - save work done
- SAVEPOINT - identify a point in a transaction to which you can later roll back
- ROLLBACK - restore database to original since the last COMMIT
- SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use
DML commands can not be rollback when a DDL command is executed immediately after a DML. DDL after DML means "auto commit".
6.08.2009
MySQL - Overview
My SQL, not "My sequel" is a relational database management system (RDBMS). As the world's most popular open source database, MySQL is used by a wide range of organizations to manage their data.
web: www.mysql.com- Managing the Database
- Understanding MySQL Table Type
- Working with tables
- Creating and removing Index
- Querying data from MySQL
- INSERT-UPDATE-DELETE
- Database Table Maintanance
More..
Table Types
MySQL supports various of table types or storage engines. These are:
ISAM
ISAM had been deprecated and removed from version 5.x. All of it functionality entire replace by MyISAM. ISAM table has a hard size 4GB and is not portable.
MyISAM
InnoDB
Disadvantage - In comparison with MyISAM is it take more disk space.
BDB
MERGE
Merge table type is added to treat multiple MyISAM tables as a single table so it remove the size limitation from MyISAM tables.
HEAP
Disadvantage: Heap tables do not support columns with AUTO_INCREMENT, BLOB and TEXT characteristics.
- ISAM
- MyISAM
- InnoDB
- BerkeleyDB (BDB)
- MERGE
- HEAP
- Only InnoDB and BDB tables are transaction safe and
- Only MyISAM tables support full-text indexing and searching feature.
- MyISAM is also the default table type.
ISAM
ISAM had been deprecated and removed from version 5.x. All of it functionality entire replace by MyISAM. ISAM table has a hard size 4GB and is not portable.
MyISAM
- This is default type when you create table.
- MyISAM table work very fast but not transaction-safe.
- The size of table depends on the OS and the data file are portable.
- Hard size - 64 keys per table and maximum key length of 1024 bytes.
InnoDB
- InnoDB table are transaction safe.
- Supports row-level locking.
- Foreign keys are supported in InnoDB tables.
- The data file of InnoDB table can be stored in more than one file. So,
- The size of table depends on the disk space.
- Like the MyISAM table type, data file of InnoDB is portable.
Disadvantage - In comparison with MyISAM is it take more disk space.
BDB
- BDB is similar to InnoDB in transaction safe.
- It supports page level locking but data file are not portable.
MERGE
Merge table type is added to treat multiple MyISAM tables as a single table so it remove the size limitation from MyISAM tables.
HEAP
- Heap table is stored in memory so it is the fastest one.
- Because of storage mechanism, the data will be lost when the power failure and sometime it can cause the server run out of memory.
Disadvantage: Heap tables do not support columns with AUTO_INCREMENT, BLOB and TEXT characteristics.
DDL
CREATE TABLE:
Statement Pattern
CREATE TABLE [IF NOT EXISTS] table_name(
column_list
) type=table_type
Example-1
CREATE TABLE employees (
employeeNumber into(11) NOT NULL,
lastName varchar(50) NOT NULL,
officeCode varchar(10) NOT NULL,
reportsTo int(11) default NULL,
PRIMARY KEY (employeeNumber)
);
Example-2: Defining Duplicate Primary Key
CREATE TABLE payments (
customerNumber int(11) NOT NULL,
checkNumber varchar(50) NOT NULL,
paymentDate datetime NOT NULL,
amount double NOT NULL,
PRIMARY KEY (customerNumber,checkNumber)
);
Example-3: Defining Storage Engine
CREATE TABLE database_name.table_name(
column1 NOT NULL AUTO_INCREMENT ,
column2 VARCHAR( 20 ) NOT NULL ,
column3 VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL ,
PRIMARY KEY ( column1)
) ENGINE = MYISAM ;
Example-4: Create table from another table
CREATE TABLE new_table_name
AS (select * from old_table);
CREATE TABLE new_table_name
AS (select col1, col2 from old_table where cond1);
DESCRIBE TABLE: DESCRIBE table_name;
SHOW TABLES: SHOW TABLES
This will show all the tables.
More..
Statement Pattern
CREATE TABLE [IF NOT EXISTS] table_name(
column_list
) type=table_type
Example-1
CREATE TABLE employees (
employeeNumber into(11) NOT NULL,
lastName varchar(50) NOT NULL,
officeCode varchar(10) NOT NULL,
reportsTo int(11) default NULL,
PRIMARY KEY (employeeNumber)
);
Example-2: Defining Duplicate Primary Key
CREATE TABLE payments (
customerNumber int(11) NOT NULL,
checkNumber varchar(50) NOT NULL,
paymentDate datetime NOT NULL,
amount double NOT NULL,
PRIMARY KEY (customerNumber,checkNumber)
);
Example-3: Defining Storage Engine
CREATE TABLE database_name.table_name(
column1 NOT NULL AUTO_INCREMENT ,
column2 VARCHAR( 20 ) NOT NULL ,
column3 VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL ,
PRIMARY KEY ( column1)
) ENGINE = MYISAM ;
Example-4: Create table from another table
CREATE TABLE new_table_name
AS (select * from old_table);
CREATE TABLE new_table_name
AS (select col1, col2 from old_table where cond1);
DESCRIBE TABLE: DESCRIBE table_name;
SHOW TABLES: SHOW TABLES
This will show all the tables.
More..
Internal Locking Methods
Locking performed within the MySQL server itself to manage contention for table contents by multiple sessions. This type of locking is internal because it is performed entirely by the server and involves no other programs. External locking occurs when the server and other programs lock table files to coordinate among themselves which program can access the tables at which time.
MySQL uses -
Table-level locking for -
Row-level locking for -
MySQL uses -
Table-level locking for -
MyISAM, MEMORY, and MERGE tables, andRow-level locking for -
InnoDB tables.Generally it is difficult to say that a given lock type is better than another. Everything depends on the application and different parts of an application may require different lock types.
If you want to use a storage engine with row-level locking, you should look at what your application does and what mix of select and update statements it uses.
For example, most Web applications perform many selects, relatively few deletes, updates based mainly on key values, and inserts into a few specific tables. The base MySQL MyISAM setup is very well tuned for this.
Table locking (in MySQL) is deadlock-free for storage engines that use table-level locking. Deadlock avoidance is managed by always requesting all needed locks at once at the beginning of a query and always locking the tables in the same order.
MySQL grants table write locks as follows:
1. If there are no locks on the table, put a write lock on it.
2. Otherwise, put the lock request in the write lock queue.
MySQL grants table read locks as follows:
1. If there are no write locks on the table, put a read lock on it.
2. Otherwise, put the lock request in the read lock queue.
If you want to use a storage engine with row-level locking, you should look at what your application does and what mix of select and update statements it uses.
For example, most Web applications perform many selects, relatively few deletes, updates based mainly on key values, and inserts into a few specific tables. The base MySQL MyISAM setup is very well tuned for this.
Table locking (in MySQL) is deadlock-free for storage engines that use table-level locking. Deadlock avoidance is managed by always requesting all needed locks at once at the beginning of a query and always locking the tables in the same order.
MySQL grants table write locks as follows:
1. If there are no locks on the table, put a write lock on it.
2. Otherwise, put the lock request in the write lock queue.
MySQL grants table read locks as follows:
1. If there are no write locks on the table, put a read lock on it.
2. Otherwise, put the lock request in the read lock queue.
Table updates are given higher priority than table retrievals. Therefore, when a lock is released, the lock is made available to the requests in the write lock queue and then to the requests in the read lock queue. This ensures that updates to a table are not “starved” even if there is heavy
The MyISAM storage engine supports concurrent inserts to reduce contention between readers and writers for a given table: If a MyISAM table has no free blocks in the middle of the data file, rows are always inserted at the end of the data file. In this case, you can freely mix concurrent INSERT and SELECT statements for a MyISAM table without locks. That is, you can insert rows into a MyISAM table at the same time other clients are reading from it. Holes can result from rows having been deleted from or updated in the middle of the table. If there are holes, concurrent inserts are disabled but are enabled again automatically when all holes have been filled with new data.. This behavior is altered by the concurrent_insert system variable.
Advantages of row-level locking:
SELECT activity for the table. However, if you have many updates for a table, SELECT statements wait until there are no more updates.The MyISAM storage engine supports concurrent inserts to reduce contention between readers and writers for a given table: If a MyISAM table has no free blocks in the middle of the data file, rows are always inserted at the end of the data file. In this case, you can freely mix concurrent INSERT and SELECT statements for a MyISAM table without locks. That is, you can insert rows into a MyISAM table at the same time other clients are reading from it. Holes can result from rows having been deleted from or updated in the middle of the table. If there are holes, concurrent inserts are disabled but are enabled again automatically when all holes have been filled with new data.. This behavior is altered by the concurrent_insert system variable.
Advantages of row-level locking:
- Fewer lock conflicts when different sessions access different rows
- Fewer changes for rollbacks
- Possible to lock a single row for a long time
- Requires more memory than table-level locks
- Slower than table-level locks when used on a large part of the table because you must acquire many more locks
- Slower than other locks if you often do GROUP BY operations on a large part of the data or if you must scan the entire table frequently
- Most statements for the table are reads
- Statements for the table are a mix of reads and writes
- SELECT combined with concurrent INSERT statements, and very few UPDATE or DELETE statements
- Many scans or GROUP BY operations on the entire table without any writers
6.07.2009
Managing the Database
CREATE DB:
To create a database in MySQL, you use the CREATE DATABASE statement:
Command: CREATE DATABASE [IF NOT EXISTS] database_name;
OR: CREATE DATABASE database_name;
SHOW DB:
SHOW DATABASE statement will show all databases in your server.
Command: SHOW DATABASES;
SELECT DB:
To select a database which you will work with, you use this statement.
Command: USE database_name;
REMOVE DB:
Removing database means you delete the database, all the data and related objects inside the database permanently and cannot undo it.
Command: DROP DATABASE [IF EXISTS] database_name;
Link -1 : www.mysqltutorial.org
To create a database in MySQL, you use the CREATE DATABASE statement:
Command: CREATE DATABASE [IF NOT EXISTS] database_name;
OR: CREATE DATABASE database_name;
CREATE DATABASE statement will create the database with the given name you specified. IF NOT EXISTS is an option part of the statement, this part prevents you from error if there is a database with the given name exists on the database server.
SHOW DB:
SHOW DATABASE statement will show all databases in your server.
Command: SHOW DATABASES;
SELECT DB:
To select a database which you will work with, you use this statement.
Command: USE database_name;
REMOVE DB:
Removing database means you delete the database, all the data and related objects inside the database permanently and cannot undo it.
Command: DROP DATABASE [IF EXISTS] database_name;
Link -1 : www.mysqltutorial.org
2.23.2009
Oracle DBA scripts - Performance related
1. identify heavy SQL (Get the SQL with heavy BUFFER_GETS)
2. identify heavy SQL (Get the SQL with heavy DISK_READS)
3. Last 30 minutes result those resources that are in high demand on your system.
4. What user is waiting the most?
5. What SQL is currently using the most resources?
6. What object is currently causing the highest resource waits?
7. Wait related.
8. From a given Time range.
9. How many Times a query executed?
1.Identify heavy SQL (Get the SQL with heavy BUFFER_GETS)
2.Identify heavy SQL (Get the SQL with heavy DISK_READS)
3. Last 30 minutes result those resources that are in high demand on your system.
4.What user is waiting the most?
select sesion.sid,
5. What SQL is currently using the most resources?
6. What object is currently causing the highest resource waits?
column OBJECT_NAME format a30
column EVENT format a30
select dba_objects.object_name,
dba_objects.object_type,
active_session_history.event,
sum(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
from v$active_session_history active_session_history,
dba_objects
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
and active_session_history.current_obj# = dba_objects.object_id
group by dba_objects.object_name, dba_objects.object_type, active_session_history.event
order by 4 desc;
7.Wait related..
SELECT distinct wait_class#, wait_class
FROM v$event_name ORDER BY wait_class#;
SELECT wait_class_id, wait_class#, wait_class, total_waits, time_waited
FROM v$system_wait_class
Order by time_waited desc;
8. Top SQLs Elaps time and CPU time in a given time range..
SELECT SQL_TEXT,X.CPU_TIME
FROM DBA_HIST_SQLTEXT DHST,
(SELECT DHSS.SQL_ID SQL_ID,SUM(DHSS.CPU_TIME_DELTA) CPU_TIME
FROM DBA_HIST_SQLSTAT DHSS
WHERE DHSS.SNAP_ID IN(SELECT SNAP_ID FROM DBA_HIST_SNAPSHOT
WHERE BEGIN_INTERVAL_TIME>=TO_DATE('10/16/2008','MM/DD/YYYY')
AND END_INTERVAL_TIME<=TO_DATE('10/18/2008','MM/DD/YYYY')) GROUP BY DHSS.SQL_ID) X WHERE X.SQL_ID=DHST.SQL_ID ORDER BY X.CPU_TIME DESC; More.. --X.ELAPSED_TIME/1000000 => From Micro second to second
--X.ELAPSED_TIME/1000000/X.EXECUTIONS_DELTA => How many times the sql ran
SELECT SQL_TEXT
,ROUND(X.ELAPSED_TIME/1000000/X.EXECUTIONS_DELTA,3) ELAPSED_TIME_SEC
,ROUND(X.CPU_TIME /1000000/X.EXECUTIONS_DELTA,3) CPU_TIME_SEC
, EXECUTIONS_DELTA
,X.ELAPSED_TIME
,X.CPU_TIME
,X.EXECUTIONS_DELTA
FROM DBA_HIST_SQLTEXT DHST,
(SELECT DHSS.SQL_ID SQL_ID,SUM(DHSS.CPU_TIME_DELTA) CPU_TIME,
SUM (DHSS.ELAPSED_TIME_DELTA) ELAPSED_TIME
, SUM(DHSS.EXECUTIONS_DELTA) EXECUTIONS_DELTA
FROM DBA_HIST_SQLSTAT DHSS
WHERE DHSS.SNAP_ID IN(SELECT SNAP_ID FROM DBA_HIST_SNAPSHOT
WHERE BEGIN_INTERVAL_TIME >= TO_DATE('17-feb-2009 08:00', 'dd-mon-yyyy hh24:mi')
AND END_INTERVAL_TIME <= TO_DATE('17-feb-2009 16:00', 'dd-mon-yyyy hh24:mi')) GROUP BY DHSS.SQL_ID) X WHERE X.SQL_ID=DHST.SQL_ID ORDER BY ELAPSED_TIME_SEC DESC;
For specific owner..
SELECT SQL_TEXT
,ROUND(X.ELAPSED_TIME/1000000,3) ELAPSED_TIME_SEC
,ROUND(X.CPU_TIME /1000000,3) CPU_TIME_SEC
FROM DBA_HIST_SQLTEXT DHST,
(SELECT DHSS.SQL_ID SQL_ID, SUM(DHSS.CPU_TIME_DELTA) CPU_TIME
, SUM (DHSS.ELAPSED_TIME_DELTA) ELAPSED_TIME
, SUM(DHSS.EXECUTIONS_DELTA) EXECUTIONS_DELTA
FROM DBA_HIST_SQLSTAT DHSS
WHERE DHSS.SNAP_ID IN
(SELECT SNAP_ID FROM DBA_HIST_SNAPSHOT
WHERE BEGIN_INTERVAL_TIME >= TO_DATE('01-feb-2009 08:00', 'dd-mon-yyyy hh24:mi')
AND END_INTERVAL_TIME <= TO_DATE('17-feb-2009 16:00', 'dd-mon-yyyy hh24:mi')) AND DHSS.parsing_schema_name='PROD8' GROUP BY DHSS.SQL_ID ) X WHERE X.SQL_ID = DHST.SQL_ID ORDER BY ELAPSED_TIME_SEC DESC;
--Latest
SELECT dbms_lob.substr(SQL_TEXT,4000,1) as SQL
,ROUND(X.ELAPSED_TIME/1000000,2) "ELAPSED TIME (sec)" --From Micro second to second
,ROUND(X.CPU_TIME /1000000,2) "CPU TIME (sec)"
,X.EXECUTIONS_DELTA as "TOTAL NO OF EXECUTIONS"
,ROUND(((X.ELAPSED_TIME/1000000) /X.EXECUTIONS_DELTA),2) as "Execution Time Per Query (sec)"
FROM DBA_HIST_SQLTEXT DHST,
(
SELECT DHSS.SQL_ID SQL_ID, SUM(DHSS.CPU_TIME_DELTA) CPU_TIME
, SUM (DHSS.ELAPSED_TIME_DELTA) ELAPSED_TIME
, SUM (DHSS.EXECUTIONS_DELTA) EXECUTIONS_DELTA --DHSS.EXECUTIONS_DELTA = No of queries execution (per hour)
FROM DBA_HIST_SQLSTAT DHSS
WHERE DHSS.SNAP_ID IN
(
SELECT SNAP_ID FROM DBA_HIST_SNAPSHOT
WHERE BEGIN_INTERVAL_TIME > TO_DATE('20-dec-10 23:59', 'dd-mon-yy hh24:mi')
AND END_INTERVAL_TIME < TO_DATE('28-dec-10 00:01', 'dd-mon-yy hh24:mi')
)
AND DHSS.parsing_schema_name='PROD7' GROUP BY DHSS.SQL_ID
) X
WHERE X.SQL_ID = DHST.SQL_ID
ORDER BY "ELAPSED TIME (sec)" DESC;
9. How many Times a query executed?
Per Hour sql execution growth:
Begin_interval_time means snapshot interval time.
As we fixed this 60 min so here you can see the execution growth (per hour).
The execution delta will show you the no of queries execution (per hour).
select s.begin_interval_time, sql.sql_id as sql_id, sql.executions_delta as exe_delta, sql.EXECUTIONS_TOTAL
from dba_hist_sqlstat sql, dba_hist_snapshot s
where sql_id='b4q5gbua0dzy3'
and s.snap_id = SQL.snap_id
and s.begin_interval_time> TO_date('18-oct-2008 14:00', 'dd-mon-yyyy hh24:mi')
and s.begin_interval_time< TO_date('21-oct-2008 18:30', 'dd-mon-yyyy hh24:mi') order by s.begin_interval_time;
2. identify heavy SQL (Get the SQL with heavy DISK_READS)
3. Last 30 minutes result those resources that are in high demand on your system.
4. What user is waiting the most?
5. What SQL is currently using the most resources?
6. What object is currently causing the highest resource waits?
7. Wait related.
8. From a given Time range.
9. How many Times a query executed?
1.Identify heavy SQL (Get the SQL with heavy BUFFER_GETS)
select sql_text ,executions ,disk_reads ,buffer_gets
from v$sqlarea
where decode(executions,0,buffer_gets,buffer_gets/executions)
> (select avg(decode(executions,0,buffer_gets,buffer_gets/executions))
+ stddev(decode(executions,0,buffer_gets,buffer_gets/executions))
from v$sqlarea) and parsing_user_id !=3D;
from v$sqlarea
where decode(executions,0,buffer_gets,buffer_gets/executions)
> (select avg(decode(executions,0,buffer_gets,buffer_gets/executions))
+ stddev(decode(executions,0,buffer_gets,buffer_gets/executions))
from v$sqlarea) and parsing_user_id !=3D;
2.Identify heavy SQL (Get the SQL with heavy DISK_READS)
select sql_text ,executions ,disk_reads ,buffer_gets
from v$sqlarea
where decode(executions ,0,disk_reads,disk_reads/executions)
> (select avg(decode(executions,0,disk_reads,disk_reads/executions))
+ stddev(decode(executions,0,disk_reads,disk_reads/executions))
from v$sqlarea)
and parsing_user_id !=3D;
from v$sqlarea
where decode(executions ,0,disk_reads,disk_reads/executions)
> (select avg(decode(executions,0,disk_reads,disk_reads/executions))
+ stddev(decode(executions,0,disk_reads,disk_reads/executions))
from v$sqlarea)
and parsing_user_id !=3D;
3. Last 30 minutes result those resources that are in high demand on your system.
select active_session_history.event,
sum(active_session_history.wait_time +
active_session_history.time_waited) total_wait_time
from v$active_session_history active_session_history
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
group by active_session_history.event
order by 2;
sum(active_session_history.wait_time +
active_session_history.time_waited) total_wait_time
from v$active_session_history active_session_history
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
group by active_session_history.event
order by 2;
4.What user is waiting the most?
select sesion.sid,
sesion.username,
sum(active_session_history.wait_time +
active_session_history.time_waited) total_wait_time
from v$active_session_history active_session_history,
v$session sesion
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
and active_session_history.session_id = sesion.sid
group by sesion.sid, sesion.username
order by 3;
sum(active_session_history.wait_time +
active_session_history.time_waited) total_wait_time
from v$active_session_history active_session_history,
v$session sesion
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
and active_session_history.session_id = sesion.sid
group by sesion.sid, sesion.username
order by 3;
5. What SQL is currently using the most resources?
select active_session_history.user_id,
dba_users.username,
sqlarea.sql_text,
sum(active_session_history.wait_time +
active_session_history.time_waited) total_wait_time
from v$active_session_history active_session_history,
v$sqlarea sqlarea,
dba_users
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
and active_session_history.sql_id = sqlarea.sql_id
and active_session_history.user_id = dba_users.user_id
group by active_session_history.user_id,sqlarea.sql_text, dba_users.username
order by 4 desc;
dba_users.username,
sqlarea.sql_text,
sum(active_session_history.wait_time +
active_session_history.time_waited) total_wait_time
from v$active_session_history active_session_history,
v$sqlarea sqlarea,
dba_users
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
and active_session_history.sql_id = sqlarea.sql_id
and active_session_history.user_id = dba_users.user_id
group by active_session_history.user_id,sqlarea.sql_text, dba_users.username
order by 4 desc;
6. What object is currently causing the highest resource waits?
column OBJECT_NAME format a30
column EVENT format a30
select dba_objects.object_name,
dba_objects.object_type,
active_session_history.event,
sum(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
from v$active_session_history active_session_history,
dba_objects
where active_session_history.sample_time between sysdate - 60/2880 and sysdate
and active_session_history.current_obj# = dba_objects.object_id
group by dba_objects.object_name, dba_objects.object_type, active_session_history.event
order by 4 desc;
7.Wait related..
SELECT distinct wait_class#, wait_class
FROM v$event_name ORDER BY wait_class#;
SELECT wait_class_id, wait_class#, wait_class, total_waits, time_waited
FROM v$system_wait_class
Order by time_waited desc;
8. Top SQLs Elaps time and CPU time in a given time range..
SELECT SQL_TEXT,X.CPU_TIME
FROM DBA_HIST_SQLTEXT DHST,
(SELECT DHSS.SQL_ID SQL_ID,SUM(DHSS.CPU_TIME_DELTA) CPU_TIME
FROM DBA_HIST_SQLSTAT DHSS
WHERE DHSS.SNAP_ID IN(SELECT SNAP_ID FROM DBA_HIST_SNAPSHOT
WHERE BEGIN_INTERVAL_TIME>=TO_DATE('10/16/2008','MM/DD/YYYY')
AND END_INTERVAL_TIME<=TO_DATE('10/18/2008','MM/DD/YYYY')) GROUP BY DHSS.SQL_ID) X WHERE X.SQL_ID=DHST.SQL_ID ORDER BY X.CPU_TIME DESC; More.. --X.ELAPSED_TIME/1000000 => From Micro second to second
--X.ELAPSED_TIME/1000000/X.EXECUTIONS_DELTA => How many times the sql ran
SELECT SQL_TEXT
,ROUND(X.ELAPSED_TIME/1000000/X.EXECUTIONS_DELTA,3) ELAPSED_TIME_SEC
,ROUND(X.CPU_TIME /1000000/X.EXECUTIONS_DELTA,3) CPU_TIME_SEC
, EXECUTIONS_DELTA
,X.ELAPSED_TIME
,X.CPU_TIME
,X.EXECUTIONS_DELTA
FROM DBA_HIST_SQLTEXT DHST,
(SELECT DHSS.SQL_ID SQL_ID,SUM(DHSS.CPU_TIME_DELTA) CPU_TIME,
SUM (DHSS.ELAPSED_TIME_DELTA) ELAPSED_TIME
, SUM(DHSS.EXECUTIONS_DELTA) EXECUTIONS_DELTA
FROM DBA_HIST_SQLSTAT DHSS
WHERE DHSS.SNAP_ID IN(SELECT SNAP_ID FROM DBA_HIST_SNAPSHOT
WHERE BEGIN_INTERVAL_TIME >= TO_DATE('17-feb-2009 08:00', 'dd-mon-yyyy hh24:mi')
AND END_INTERVAL_TIME <= TO_DATE('17-feb-2009 16:00', 'dd-mon-yyyy hh24:mi')) GROUP BY DHSS.SQL_ID) X WHERE X.SQL_ID=DHST.SQL_ID ORDER BY ELAPSED_TIME_SEC DESC;
For specific owner..
SELECT SQL_TEXT
,ROUND(X.ELAPSED_TIME/1000000,3) ELAPSED_TIME_SEC
,ROUND(X.CPU_TIME /1000000,3) CPU_TIME_SEC
FROM DBA_HIST_SQLTEXT DHST,
(SELECT DHSS.SQL_ID SQL_ID, SUM(DHSS.CPU_TIME_DELTA) CPU_TIME
, SUM (DHSS.ELAPSED_TIME_DELTA) ELAPSED_TIME
, SUM(DHSS.EXECUTIONS_DELTA) EXECUTIONS_DELTA
FROM DBA_HIST_SQLSTAT DHSS
WHERE DHSS.SNAP_ID IN
(SELECT SNAP_ID FROM DBA_HIST_SNAPSHOT
WHERE BEGIN_INTERVAL_TIME >= TO_DATE('01-feb-2009 08:00', 'dd-mon-yyyy hh24:mi')
AND END_INTERVAL_TIME <= TO_DATE('17-feb-2009 16:00', 'dd-mon-yyyy hh24:mi')) AND DHSS.parsing_schema_name='PROD8' GROUP BY DHSS.SQL_ID ) X WHERE X.SQL_ID = DHST.SQL_ID ORDER BY ELAPSED_TIME_SEC DESC;
--Latest
SELECT dbms_lob.substr(SQL_TEXT,4000,1) as SQL
,ROUND(X.ELAPSED_TIME/1000000,2) "ELAPSED TIME (sec)" --From Micro second to second
,ROUND(X.CPU_TIME /1000000,2) "CPU TIME (sec)"
,X.EXECUTIONS_DELTA as "TOTAL NO OF EXECUTIONS"
,ROUND(((X.ELAPSED_TIME/1000000) /X.EXECUTIONS_DELTA),2) as "Execution Time Per Query (sec)"
FROM DBA_HIST_SQLTEXT DHST,
(
SELECT DHSS.SQL_ID SQL_ID, SUM(DHSS.CPU_TIME_DELTA) CPU_TIME
, SUM (DHSS.ELAPSED_TIME_DELTA) ELAPSED_TIME
, SUM (DHSS.EXECUTIONS_DELTA) EXECUTIONS_DELTA --DHSS.EXECUTIONS_DELTA = No of queries execution (per hour)
FROM DBA_HIST_SQLSTAT DHSS
WHERE DHSS.SNAP_ID IN
(
SELECT SNAP_ID FROM DBA_HIST_SNAPSHOT
WHERE BEGIN_INTERVAL_TIME > TO_DATE('20-dec-10 23:59', 'dd-mon-yy hh24:mi')
AND END_INTERVAL_TIME < TO_DATE('28-dec-10 00:01', 'dd-mon-yy hh24:mi')
)
AND DHSS.parsing_schema_name='PROD7' GROUP BY DHSS.SQL_ID
) X
WHERE X.SQL_ID = DHST.SQL_ID
ORDER BY "ELAPSED TIME (sec)" DESC;
9. How many Times a query executed?
Per Hour sql execution growth:
Begin_interval_time means snapshot interval time.
As we fixed this 60 min so here you can see the execution growth (per hour).
The execution delta will show you the no of queries execution (per hour).
select s.begin_interval_time, sql.sql_id as sql_id, sql.executions_delta as exe_delta, sql.EXECUTIONS_TOTAL
from dba_hist_sqlstat sql, dba_hist_snapshot s
where sql_id='b4q5gbua0dzy3'
and s.snap_id = SQL.snap_id
and s.begin_interval_time> TO_date('18-oct-2008 14:00', 'dd-mon-yyyy hh24:mi')
and s.begin_interval_time< TO_date('21-oct-2008 18:30', 'dd-mon-yyyy hh24:mi') order by s.begin_interval_time;
2.22.2009
Oracle DBA scripts - Bind variable related
Bind variable
1. Find the value of Bind variable
select NAME,POSITION,DATATYPE_STRING,VALUE_STRING from v$sql_bind_capture where sql_id='d9kf91muzy2wq';
v$sql_bind_capture Details: http://youngcow.net/doc/oracle10g/server.102/b14237/dynviews_2114.htm
To get data from history:
DBA_HIST_SQLBIND
DBA_HIST_SQL_BIND_METADATA
1. Find the value of Bind variable
select NAME,POSITION,DATATYPE_STRING,VALUE_STRING from v$sql_bind_capture where sql_id='d9kf91muzy2wq';
v$sql_bind_capture Details: http://youngcow.net/doc/oracle10g/server.102/b14237/dynviews_2114.htm
To get data from history:
DBA_HIST_SQLBIND
DBA_HIST_SQL_BIND_METADATA
Subscribe to:
Posts (Atom)