Find long-running queries

Find long-running queries

This script will get read/write times and file directories from your Oracle database. It proved useful to my developers to determine long-running query problems. Developers could now see if the DB was a major factor in query performance and also see the sqltext being run from a function call. It has been tested on Oracle 9.1.0.1, 9.2.0.2.1 and 9.2.0.4.0.

SELECT d.name file_name, 
d.blocks, 
f.phywrts, 
f.phyrds, 
f.readtim, 
f.writetim, 
t.name tablespace_name, 
f.phywrts/d.blocks wrts_per_block, 
f.phyrds/d.blocks rds_per_block 
FROM v$filestat f, 
v$datafile d, 
sys.ts$ t 
WHERE f.file#=d.file# 
AND d.blocks >0 
AND t.ts#=d.ts# 
UNION ALL 
SELECT d.name file_name, 
d.blocks, 
f.phywrts, 
f.phyrds, 
f.readtim, 
f.writetim, 
t.name tablespace_name, 
f.phywrts/d.blocks wrts_per_block, 
f.phyrds/d.blocks rds_per_block 
FROM v$tempstat f, 
v$tempfile d, 
sys.ts$ t 
WHERE f.file#=d.file# 
AND d.blocks >0 
AND t.ts#=d.ts# 

CREATE OR REPLACE 
FUNCTION getusername(sidval number) return varchar2 is 
username varchar2(256); 
begin 
select username into username from v$session where sid = sidval; 
return username; 
end; 
/ 

SELECT DISTINCT /*+ORDERED*/ 
sql.sql_text, 
sql.address, 
sql.hash_value, 
0 piece, 
sid, 
serial#, 
getusername(sid)username 
FROM v$session s, 
v$sql sql 
WHERE sql.address=s.sql_address 
AND sql.hash_value=s.sql_hash_value 

For More Information

    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

  • the editor with your thoughts about this tip.
  • More tips: Hundreds of free Oracle tips and scripts.
  • Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our applications, SQL, database administration, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: Oracle tips, tutorials, and scripts from around the Web.

This was first published in February 2004

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.

    Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.