oracle Find and Stop Long-Running Queries

It is all too easy to set off a query that will run for several hours, consuming system resources and disturbing other users. Spot long-running queries and kill them off.

Long-running queries are surprisingly easy to produce. It is not always clear how long a query will take to run, and even correct queries may take a long time to execute. Incorrectly written queries, such as ones withJOIN conditions missing, may take a considerable time to solve. Oracle, SQL Server, MySQL, and PostgreSQL each support ways to detect long-running queries. They also offer ways to terminate running queries. The commands to manage queries have been implemented differently for each of these database systems.

In Oracle and SQL Server, you can monitor activity by querying a system view. This means that you can useSELECT statements to choose the columns and rows that you want to be shown.

Oracle

In Oracle, you can find a long-running database session (which could have been involved with many different transactions). The query to find long-running sessions uses the system viewV$SESSION:

SELECT username,sid, serial#,
       TO_CHAR(sysdate,'YYYY-MM-DD HH24:MI:SS') "CURRENT",
       TO_CHAR(logon_time,'YYYY-MM-DD HH24:MI:SS') "LOGON",
       (sysdate - logon_time)*24*60 "MINS"
FROM V$SESSION
WHERE (sysdate - logon_time)*24*60 > 1 
AND    username is not NULL;

USERNAME  SID SERIAL# CURRENT             LOGON                MINS
----------------------------------------------------------------------------
GRUSSELL  246 52683   2006-06-16 14:38:22 2006-06-16 14:30:51     7.51
DBRW      251 49308   2006-06-16 14:38:22 2006-06-15 17:33:48  1264.56

You can subtract the logon_time fromsysdate (the current time) to get the length of time each session has been running. This floating-point value is in units of days; so you can multiply by 24 to get this in hours and multiply again by 60 to get the number of minutes.

To kill a session use:

ALTER SYSTEM KILL SESSION 'sid,serial'

To kill off user DBRW's query use:

ALTER SYSTEM KILL SESSION '251,49308'

转载http://codeidol.com/sql/sql-hack/Wider-Access/Find-and-Stop-Long-Running-Queries/
备注:type为background类型的session不可以kill,请参照上篇文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值