<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DBAPundits.com &#187; Queries-Scripts</title>
	<atom:link href="http://www.dbapundits.com/blog/category/query-script/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dbapundits.com/blog</link>
	<description>Articles and resources for the DBAs.</description>
	<lastBuildDate>Wed, 23 Nov 2011 03:22:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Find temporary tablespace usage</title>
		<link>http://www.dbapundits.com/blog/query-script/find-temporary-tablespace-usage/</link>
		<comments>http://www.dbapundits.com/blog/query-script/find-temporary-tablespace-usage/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 01:28:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Queries-Scripts]]></category>

		<guid isPermaLink="false">http://www.dbapundits.com/blog/?p=387</guid>
		<description><![CDATA[<script type="text/javascript"><!--
google_ad_client = "pub-9448554670318827";
google_alternate_color = "FFFFFF";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "image";
google_ad_channel ="Pundits";
google_color_border = "FFFFFF";
google_color_link = "0022C9";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "128A00";
google_ui_features = "rc:0";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><p>Use the below query to find out the current usage of temporary tablespace(sort usage) by active users.</p> SELECT S.sid &#124;&#124; ',' &#124;&#124; S.serial# sid_serial, S.username, S.osuser, P.spid, S.module, S.program, SUM (T.blocks) * TBS.block_size /1024/1024 mb_used, T.tablespace, COUNT(*) sort_ops FROM gv$sort_usage T, gv$session S, dba_tablespaces TBS, gv$process P WHERE T.session_addr = S.saddr AND S.paddr = P.addr [...]]]></description>
			<content:encoded><![CDATA[<p>Use the below query to find out the current usage of temporary tablespace(sort usage) by active users.</p>
<pre class="brush: plain; title: ; notranslate">
SELECT   S.sid || ',' || S.serial# sid_serial, S.username, S.osuser, P.spid, S.module,
         S.program, SUM (T.blocks) * TBS.block_size /1024/1024 mb_used, T.tablespace,
         COUNT(*) sort_ops
FROM     gv$sort_usage T, gv$session S, dba_tablespaces TBS, gv$process P
WHERE    T.session_addr = S.saddr
AND      S.paddr = P.addr
AND      T.tablespace = TBS.tablespace_name
GROUP BY S.sid, S.serial#, S.username, S.osuser, P.spid, S.module,
         S.program, TBS.block_size, T.tablespace
ORDER BY sid_serial
</pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.dbapundits.com%2Fblog%2Fquery-script%2Ffind-temporary-tablespace-usage%2F&amp;title=Find%20temporary%20tablespace%20usage" id="wpa2a_2"><img src="http://www.dbapundits.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbapundits.com/blog/query-script/find-temporary-tablespace-usage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Set trace on user sessions</title>
		<link>http://www.dbapundits.com/blog/query-script/set-trace-on-user-sessions/</link>
		<comments>http://www.dbapundits.com/blog/query-script/set-trace-on-user-sessions/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 05:19:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Queries-Scripts]]></category>

		<guid isPermaLink="false">http://www.dbapundits.com/blog/?p=337</guid>
		<description><![CDATA[Oracle 10046 sesssion tracing <p>There are different ways to enable tracing on an Oracle database user session. They are:</p> Using DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION procudure. Using oradebug ipc setorapid/setospiod Using a Logon trigger <p>The first 2 methods are used used when the sessions are already connected to the database. For the first method, use the session ID, Serial# [...]]]></description>
			<content:encoded><![CDATA[<h2>Oracle 10046 sesssion tracing</h2>
<p>There are different ways to enable tracing on an Oracle database user session. They are:</p>
<ol>
<li>Using DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION procudure.</li>
<li>Using oradebug ipc setorapid/setospiod</li>
<li>Using a Logon trigger</li>
</ol>
<p>The first 2 methods are used used when the sessions are already connected to the database. For the first method, use the session ID, Serial# and execute the procedure to start tracing.</p>
<h3>Using oradebug</h3>
<p>1) Connect to the database Instance, On RAC, connect to the right instance.</p>
<p>$  connect / as sysdba</p>
<p>2) Get the OS PID of the connected session and run the command<br />
Eg:</p>
<pre class="brush: sql; title: ; notranslate">
oradebug setospid 16211
</pre>
<p>Alternatively, Oracle PID can also be used to start the tracing.<br />
Eg:</p>
<pre class="brush: sql; title: ; notranslate">
oradebug setorapid 27853
</pre>
<p>Set the tracing level to 12 for an exhaustive trace(4 and 8 are lower levels).</p>
<pre class="brush: sql; title: ; notranslate">
  oradebug unlimit
  oradebug event 10046 trace name context forever,level 12
</pre>
<p>Once the tracing is done, tracing can be disabled using the below procedure:</p>
<p>connect &#8220;/ as sysdba&#8221;</p>
<pre class="brush: sql; title: ; notranslate">
oradebug setospid 16211
oradebug event 10046 trace name context off
</pre>
<h3>Using a LOGON trigger</h3>
<p>Using a LOGON trigger may be the preferred method to trace sessions in situations where it is difficult to start tracing when user sessions get active after getting connected to the database. Also, in RAC environments where the services are configured as multi-preferred, it may be difficult to logon to each instance to set the tracing. In such scenarios, setting a logon trigger at the database can help to set tracing on sessions. Here are the steps:</p>
<p>1) Check to see if &#8220;alter session&#8221; privilege is granted to the required users. Else grant &#8220;alter session&#8221; privilege to the users.</p>
<p>For eg, to trace the sessions of SCOTT and HR users:</p>
<pre class="brush: sql; title: ; notranslate">
grant alter session to SCOTT;
grant alter session to HR;
</pre>
<p>2) Create a logon trigger at the database level:</p>
<pre class="brush: sql; title: ; notranslate">
CREATE OR REPLACE TRIGGER SYS.set_trace_scott
  AFTER LOGON ON DATABASE
  WHEN (USER in ('SCOTT','HR'))
  DECLARE
      lcommand varchar(200);
  BEGIN&lt;br /&gt;
      EXECUTE IMMEDIATE 'alter session set statistics_level=ALL';
      EXECUTE IMMEDIATE 'alter session set max_dump_file_size=UNLIMITED';
      EXECUTE IMMEDIATE 'alter session set events ''10046 trace name context forever, level 12''';
  END set_trace;
</pre>
<p>3) Trace files are generated once users sessions are established. Check the user_dump_dest for the trace files once the sessions are active. On RAC, Identify the instance to which the user(s) sessions got connected and check the appropriate server.</p>
<p>4) Once the trace files are obtained, remove the trigger to disable further tracing without fail. Otherwise, there are high chances of generating huge trace files which may fill up the filesystem. You may also want to remove the ALTER SESSION privilege from the users if granted for this purpose.</p>
<p>Use Oracle supplied utility TRCA(Trace Analyzer) to analyze the trace files.</p>
<pre class="brush: sql; title: ; notranslate">
drop trigger SYS.set_trace_scott;
</pre>
<p>Good luck in finding what you are looking for!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.dbapundits.com%2Fblog%2Fquery-script%2Fset-trace-on-user-sessions%2F&amp;title=Set%20trace%20on%20user%20sessions" id="wpa2a_4"><img src="http://www.dbapundits.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbapundits.com/blog/query-script/set-trace-on-user-sessions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>dbms_system.ksdwrt–Write messages to Oracle alert log</title>
		<link>http://www.dbapundits.com/blog/query-script/dbms_system-ksdwrt%e2%80%93write-messages-to-oracle-alert-log/</link>
		<comments>http://www.dbapundits.com/blog/query-script/dbms_system-ksdwrt%e2%80%93write-messages-to-oracle-alert-log/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 13:39:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Queries-Scripts]]></category>

		<guid isPermaLink="false">http://www.dbapundits.com/blog/?p=304</guid>
		<description><![CDATA[<p>Oracle provides a procedure to insert messages to the alert log and/or trace files for testing/development purposes. This can be used to check the effectiveness of the monitoring tools/scripts used in the environment, to understand how well the monitoring tool captures the messages in the alert log.</p> <p>Usage:</p> SQL&#62; exec dbms_system.ksdwrt(1, 'This message goes to [...]]]></description>
			<content:encoded><![CDATA[<p>Oracle provides a procedure to insert messages to the alert log and/or trace files for testing/development purposes. This can be used to check the effectiveness of the monitoring tools/scripts used in the environment, to understand how well the monitoring tool captures the messages in the alert log.</p>
<p>Usage:</p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; exec dbms_system.ksdwrt(1, 'This message goes to trace file in the udump location');

PL/SQL procedure successfully completed.

SQL&gt; exec dbms_system.ksdwrt(2, 'This message goes to the alert log');

PL/SQL procedure successfully completed.

SQL&gt; exec dbms_system.ksdwrt(3, 'This message goes to the alert log and trace file in the udump location');

PL/SQL procedure successfully completed.

SQL&gt;
</pre>
<p>To test whether your monitoring tool captures error messages such as an ORA-00600, try executing the below:</p>
<pre class="brush: sql; title: ; notranslate">

SQL&gt; exec dbms_system.ksdwrt(2, 'ORA-00600: Testing monitoring tool');

PL/SQL procedure successfully completed.

SQL&gt;
</pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.dbapundits.com%2Fblog%2Fquery-script%2Fdbms_system-ksdwrt%25e2%2580%2593write-messages-to-oracle-alert-log%2F&amp;title=dbms_system.ksdwrt%E2%80%93Write%20messages%20to%20Oracle%20alert%20log" id="wpa2a_6"><img src="http://www.dbapundits.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbapundits.com/blog/query-script/dbms_system-ksdwrt%e2%80%93write-messages-to-oracle-alert-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ORA-00054 resource busy and acquire with NOWAIT specified</title>
		<link>http://www.dbapundits.com/blog/query-script/ora-00054-resource-busy-and-acquire-with-nowait-specified/</link>
		<comments>http://www.dbapundits.com/blog/query-script/ora-00054-resource-busy-and-acquire-with-nowait-specified/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 15:34:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Queries-Scripts]]></category>

		<guid isPermaLink="false">http://www.dbapundits.com/blog/?p=279</guid>
		<description><![CDATA[<p>The below query helps to identify the session that has locked the resource that your session is trying to lock. </p> <p>You can try executing the query/job after some time (to allow the other session to complete) or kill the session that has locked the resource to proceed with this situation, use the below query [...]]]></description>
			<content:encoded><![CDATA[<p>The below query helps to identify the session that has locked the resource that your session is trying to lock. </p>
<p>You can try executing the query/job after some time (to allow the other session to complete) or kill the session that has locked the resource to proceed with this situation, use the below query to identify the session.</p>
<pre class="brush: sql; title: ; notranslate">

set linesize 140
set pages 100
col username       format a20
col &quot;SID,SESSION#&quot; format a20
col sess_id        format a10
col object format a30
col mode_held      format a10
select     oracle_username || ' (' || s.osuser || ')' username
  ,  s.sid || ',' || s.serial# &quot;SID,SESSION#&quot;
  ,  owner || '.' || object_name object
  ,  object_type
  ,  decode( l.block
     ,       0, 'Not Blocking'
     ,       1, 'Blocking'
     ,       2, 'Global') status
  ,  decode(v.locked_mode
    ,       0, 'None'
    ,       1, 'Null'
    ,       2, 'Row-S (SS)'
    ,       3, 'Row-X (SX)'
    ,       4, 'Share'
    ,       5, 'S/Row-X (SSX)'
    ,       6, 'Exclusive', TO_CHAR(lmode)) mode_held
 from       v$locked_object v
 ,  dba_objects d
 ,  v$lock l
 ,  v$session s
 where      v.object_id = d.object_id
 and        v.object_id = l.id1
 and        v.session_id = s.sid
 order by oracle_username,session_id;
</pre>
<p>Sample output:</p>
<pre class="brush: sql; title: ; notranslate">
USERNAME	SID,SESSION#	OBJECT		OBJECT_TYPE	STATUS		MODE_HELD
--------	------------	-------------	-----------	------		---------
SCOTT (oracle)	86,30		SCOTT.LOCK_TEST	TABLE		Not Blocking 	Row-X (SX)
</pre>
<p>To kill the locking session, do as below:</p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; alter system kill session '86,30';
</pre>
<p>To identify the locked rows, use the below query:</p>
<pre class="brush: sql; title: ; notranslate">
set lines 200
col object_name for a30
select do.object_name
, row_wait_obj#
, row_wait_file#
, row_wait_block#
, row_wait_row#
, dbms_rowid.rowid_create (1, ROW_WAIT_OBJ#, ROW_WAIT_FILE#,
				ROW_WAIT_BLOCK#, ROW_WAIT_ROW#) ROW_ID
from	v$session s
,	dba_objects do
where	s.ROW_WAIT_OBJ# = do.OBJECT_ID
and do.object_name=upper('&lt;object_name_from_above_query&gt;')
;

OBJECT_NAME	ROW_WAIT_OBJ#	ROW_WAIT_FILE#	ROW_WAIT_BLOCK#	ROW_WAIT_ROW#	ROW_ID
-----------	-------------	--------------	---------------	-------------	------
LOCK_TEST	41862		4		70		0		AAAKOGAAEAAAABGAAA
</pre>
<p>To simulate the occurrence of this error for testing/learning purposes, do as below in 2 sessions:</p>
<p>Session 1:</p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; conn scott/tiger;
Connected.

SQL&gt; create table lock_test (x number);
Table created.

SQL&gt; insert into lock_test values(100);
1 row created.

SQL&gt; commit;
Commit complete.

SQL&gt; update lock_test set x=500;
1 row updated.

-- Do not commit.
</pre>
<p>Session 2:</p>
<pre class="brush: sql; title: ; notranslate">

SQL&gt; conn scott/tiger;
Connected.
declare
y number;
begin
select x into y from lock_test for update nowait;
end;
/

ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified
ORA-06512: at line 4
</pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.dbapundits.com%2Fblog%2Fquery-script%2Fora-00054-resource-busy-and-acquire-with-nowait-specified%2F&amp;title=ORA-00054%20resource%20busy%20and%20acquire%20with%20NOWAIT%20specified" id="wpa2a_8"><img src="http://www.dbapundits.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbapundits.com/blog/query-script/ora-00054-resource-busy-and-acquire-with-nowait-specified/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Export-Import Using Unix Pipe</title>
		<link>http://www.dbapundits.com/blog/query-script/export-import-using-unix-pipe/</link>
		<comments>http://www.dbapundits.com/blog/query-script/export-import-using-unix-pipe/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 17:13:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Queries-Scripts]]></category>

		<guid isPermaLink="false">http://www.dbapundits.com/blog/?p=273</guid>
		<description><![CDATA[<p>In scenarios where there is no disk space to hold the export dumps or when migrating schemas across different versions of Oracle, this script helps to do the export/import over the network or on the same server using the Unix pipe.</p> <p>In this example, data is exported from the Oracle Database instance named PROD to [...]]]></description>
			<content:encoded><![CDATA[<p>In scenarios where there is no disk space to hold the export dumps or when migrating schemas across different versions of Oracle, this script helps to do the export/import over the network or on the same server using the Unix pipe.</p>
<p>In this example, data is exported from the Oracle Database instance named PROD to the instance named DEV. The export utility connects to the PROD database using the Net Service name, writes the export dump to a Unix pipe, which is read by the import process that loads the data to the DEV database.</p>
<p>Create a file exp_imp.sh:</p>
<pre class="brush: bash; title: ; notranslate">
#!/usr/bin/ksh

export ORACLE_SID=DEV
export ORACLE_HOME=/oracle/product/10204

# make a unix pipe
export PIPE=exp_imp_pipe
mknod exp_imp_pipe p

# Start the Export and run it in background  - Dont omit the '&amp;' at the end.
exp scott/tiger@PROD file=exp_imp_pipe log=exp_PROD.log buffer=1024000 consistent=y OWNER=HR,APP direct=y statistics=none compress=n  &amp;

# Start the Import
imp scott/tiger ignore=y full=y file=exp_imp_pipe log=imp_DEV.log commit=y buffer=1024000 RESUMABLE=Y &amp;
</pre>
<p>For big schemas, CONSISTENT=Y may not work as the export may fail with an ORA-01555 snapshot too old error, so you may need to revist the parameter. RESUMABLE=Y at the import side can help to make the session &#8216;resumable&#8217; in case the import hits any space issues on the target database. The export/import logs can be monitored to view the progress of the activity.</p>
<p>In this example the user scott has enough privilege to exp/imp the data, make sure that you connect to the databases as a privileged user and needless to say, it is recommended to run the job in nohup mode to avoid issues in case connectivity to the server gets disturbed.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.dbapundits.com%2Fblog%2Fquery-script%2Fexport-import-using-unix-pipe%2F&amp;title=Export-Import%20Using%20Unix%20Pipe" id="wpa2a_10"><img src="http://www.dbapundits.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbapundits.com/blog/query-script/export-import-using-unix-pipe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database/Tablespace Free space in MB</title>
		<link>http://www.dbapundits.com/blog/query-script/database-tablespace-free-space/</link>
		<comments>http://www.dbapundits.com/blog/query-script/database-tablespace-free-space/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 06:00:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Queries-Scripts]]></category>

		<guid isPermaLink="false">http://www.dbapundits.com/blog/?p=202</guid>
		<description><![CDATA[<p>One of the most used script used by the DBA in day-to-day work, this script gives the space usage of each tablespace in the database.</p> set heading on set pagesize 500 set lines 400 column tablespace format a30 heading &#34;Tablespace&#34; column avail format 9,999,999,999,999 heading &#34;MB Avail.&#34; column used format 9,999,999,999,999 heading &#34;MB Used&#34; column [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most used script used by the DBA in day-to-day work, this script gives the space usage of each tablespace in the database.</p>
<pre class="brush: sql; title: ; notranslate">

set heading on
set pagesize 500
set lines 400

column tablespace       format a30                heading &quot;Tablespace&quot;
column avail            format 9,999,999,999,999  heading &quot;MB Avail.&quot;
column used             format 9,999,999,999,999  heading &quot;MB Used&quot;
column free             format 9,999,999,999,999  heading &quot;MB Free&quot;
column pct              format 999                heading &quot;Pct&quot;

compute sum of avail used free on report
break on report
select  a.tablespace_name &quot;Tablespace&quot;,
        a.avail,
        a.avail-b.free used,
        b.free,
        round(nvl((a.avail-b.free)/a.avail*100,0))      &quot;Pct&quot;
from
(select tablespace_name, round(sum(bytes)/1048576)     avail
        from    sys.dba_data_files
        group by tablespace_name
        UNION
        select  tablespace_name,round(sum(bytes_free+bytes_used)/1048576)
        from v$temp_space_header
        group by tablespace_name)       a,
(select tablespace_name, round(sum(bytes)/1048576)     free
        from    sys.dba_free_space
        group by tablespace_name
        UNION
        select  tablespace_name,round(sum(bytes_free)/1048576)
        from v$temp_space_header
        group by tablespace_name)       b
where  a.tablespace_name = b.tablespace_name (+);
</pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.dbapundits.com%2Fblog%2Fquery-script%2Fdatabase-tablespace-free-space%2F&amp;title=Database%2FTablespace%20Free%20space%20in%20MB" id="wpa2a_12"><img src="http://www.dbapundits.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbapundits.com/blog/query-script/database-tablespace-free-space/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hourly/Daily Archive generation</title>
		<link>http://www.dbapundits.com/blog/query-script/hourly-daily-archive-generation/</link>
		<comments>http://www.dbapundits.com/blog/query-script/hourly-daily-archive-generation/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 05:56:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Queries-Scripts]]></category>

		<guid isPermaLink="false">http://www.dbapundits.com/blog/?p=198</guid>
		<description><![CDATA[<p>The below query comes handy to understand the archivelog generation of an Oracle database on an hourly /daily basis, per thread &#8211; in case of RAC databases.</p> <p>Archivelog generation on a daily basis:</p> set pages 1000 select trunc(COMPLETION_TIME,'DD') Day, thread#, round(sum(BLOCKS*BLOCK_SIZE)/1048576) MB,count(*) Archives_Generated from v$archived_log group by trunc(COMPLETION_TIME,'DD'),thread# order by 1; <p>Archive log generation on [...]]]></description>
			<content:encoded><![CDATA[<p>The below query comes handy to understand the archivelog generation of an Oracle database on an hourly /daily basis, per thread &#8211; in case of RAC databases.</p>
<p>Archivelog generation on a daily basis:</p>
<pre class="brush: sql; title: ; notranslate">
set pages 1000
select trunc(COMPLETION_TIME,'DD') Day, thread#, round(sum(BLOCKS*BLOCK_SIZE)/1048576) MB,count(*) Archives_Generated from v$archived_log
group by trunc(COMPLETION_TIME,'DD'),thread# order by 1;
</pre>
<p>Archive log generation on an hourly basis:</p>
<pre class="brush: sql; title: ; notranslate">
set pages 1000
select trunc(COMPLETION_TIME,'HH') Hour,thread# , round(sum(BLOCKS*BLOCK_SIZE)/1048576) MB,count(*) Archives from v$archived_log
group by trunc(COMPLETION_TIME,'HH'),thread#  order by 1 ;
</pre>
<p>Sample output:</p>
<pre class="brush: plain; title: ; notranslate">

HOUR                   THREAD#         MB   ARCHIVES
------------------- ---------- ---------- ----------
2009 08 20 12:00:00          1      31268        339
2009 08 20 13:00:00          1       4994         55
2009 08 20 14:00:00          1       4412         48
2009 08 20 15:00:00          1       4805         52
2009 08 20 16:00:00          1       3364         37
2009 08 20 17:00:00          1         22          1
2009 08 20 21:00:00          1          9          1
</pre>
<p>Also, the following script is useful to find the archivelog switches on an hourly basis that happened in the past one week, I got this from http://kubilaykara.blogspot.com/2008/02/redo-log-generation.html and is quite an useful one.</p>
<pre class="brush: sql; title: ; notranslate">
SELECT to_date(first_time) DAY,
to_char(sum(decode(to_char(first_time,'HH24'),'00',1,0)),'99') &quot;00&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'01',1,0)),'99') &quot;01&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'02',1,0)),'99') &quot;02&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'03',1,0)),'99') &quot;03&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'04',1,0)),'99') &quot;04&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'05',1,0)),'99') &quot;05&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'06',1,0)),'99') &quot;06&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'07',1,0)),'99') &quot;07&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'08',1,0)),'99') &quot;08&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'09',1,0)),'99') &quot;09&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'10',1,0)),'99') &quot;10&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'11',1,0)),'99') &quot;11&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'12',1,0)),'99') &quot;12&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'13',1,0)),'99') &quot;13&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'14',1,0)),'99') &quot;14&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'15',1,0)),'99') &quot;15&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'16',1,0)),'99') &quot;16&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'17',1,0)),'99') &quot;17&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'18',1,0)),'99') &quot;18&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'19',1,0)),'99') &quot;19&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'20',1,0)),'99') &quot;20&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'21',1,0)),'99') &quot;21&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'22',1,0)),'99') &quot;22&quot;,
to_char(sum(decode(to_char(first_time,'HH24'),'23',1,0)),'99') &quot;23&quot;
from
v$log_history
where to_date(first_time) &gt; sysdate -8
GROUP by
to_char(first_time,'YYYY-MON-DD'), to_date(first_time)
order by to_date(first_time)
/
</pre>
<p>Sample output:</p>
<pre class="brush: plain; title: ; notranslate">
DAY 00 01 02 03 04 05 06 09 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
--------- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- -
01-NOV-09 0 1 1 0 1 0 1 0 1 4 1 1 0 1 0 1 1 0 1 1 1 1 1 1
02-NOV-09 0 1 1 1 1 0 1 1 1 1 0 1 1 7 1 1 1 1 1 2 1 1 1 1
03-NOV-09 1 2 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 1 1 2
04-NOV-09 1 1 8 1 7 2 1 1 1 2 1 1 2 1 2 1 2 1 2 1 2 1 2 2
05-NOV-09 2 1 2 1 2 2 1 2 1 2 2 1 2 2 1 2 2 2 1 2 2 2 2 2
06-NOV-09 2 1 2 2 2 2 1 2 2 2 2 2 1 2 2 1 0 2 0 0 0 0 1 0
09-NOV-09 0 0 1 7 0 0 1 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0
</pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.dbapundits.com%2Fblog%2Fquery-script%2Fhourly-daily-archive-generation%2F&amp;title=Hourly%2FDaily%20Archive%20generation" id="wpa2a_14"><img src="http://www.dbapundits.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbapundits.com/blog/query-script/hourly-daily-archive-generation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

