{"id":22,"date":"2008-10-09T17:44:21","date_gmt":"2008-10-09T15:44:21","guid":{"rendered":"http:\/\/www.ludovicocaldara.net\/dba\/?p=22"},"modified":"2020-08-18T17:02:38","modified_gmt":"2020-08-18T15:02:38","slug":"system-triggers-statsuser_log-and-side-effects","status":"publish","type":"post","link":"https:\/\/www.ludovicocaldara.net\/dba\/system-triggers-statsuser_log-and-side-effects\/","title":{"rendered":"System triggers, stats$user_log and side effects"},"content":{"rendered":"<p>Sometimes people get advice from internet: both Metalink or well-known consulting sites.<br \/>\nIf people need a fix or a feature, they use to trust advices.<\/p>\n<p>Last week I heard a collegue about a 10g RAC database with performance problems and, since I never lay on my chair, I probed both AWR and ADDM . I suddenly recognized heavy enqueues and physical reads<br \/>\nover a segment named STATS$USER_LOG. &#8220;Strange&#8221;, I said, &#8220;I cannot remember this name in neither perfstat or catalog segments&#8221;.<br \/>\nThen I searched the Internet and the Metalink and found the same thing in BOTH metalink.oracle.com and www.dba-oracle.com: a trick to trace logon and logoffs into a table using system triggers.<\/p>\n<p>Look at this code:<\/p>\n<p><code>create or replace trigger<br \/>\nlogon_audit_trigger<br \/>\nAFTER LOGON ON DATABASE<br \/>\nBEGIN<br \/>\ninsert into stats$user_log values(<br \/>\nuser,<br \/>\nsys_context('USERENV','SESSIONID'),<br \/>\nsys_context('USERENV','HOST'),<br \/>\nsysdate,<br \/>\nto_char(sysdate, 'hh24:mi:ss'),<br \/>\n[...]<br \/>\n);<br \/>\nCOMMIT;<br \/>\nEND;<br \/>\n\/<\/code><\/p>\n<p>Cool, every single access is kept into stats$user_log.<\/p>\n<p>Let&#8217;s see the logoff trigger:<\/p>\n<p><code>create or replace trigger<br \/>\nlogoff_audit_trigger<br \/>\nBEFORE LOGOFF ON DATABASE<br \/>\nBEGIN<br \/>\n-- ***************************************************<br \/>\n-- Update the last action accessed<br \/>\n-- ***************************************************<br \/>\nupdate stats$user_log [...]<br \/>\n--***************************************************<br \/>\n-- Update the last program accessed<br \/>\n-- ***************************************************<br \/>\nupdate stats$user_log [...]<br \/>\n-- ***************************************************<br \/>\n[ ... many, many updates ...]<br \/>\n-- ***************************************************<br \/>\nupdate stats$user_log [...]<br \/>\n-- ***************************************************<br \/>\n-- Compute the elapsed minutes<br \/>\n-- ***************************************************<br \/>\nupdate stats$user_log set elapsed_minutes =<br \/>\nround((logoff_day - logon_day)*1440)<br \/>\nwhere<br \/>\nsys_context('USERENV','SESSIONID') = session_id;<br \/>\nCOMMIT;<br \/>\nEND;<br \/>\n\/<br \/>\n<\/code><br \/>\nThat&#8217;s all. It inserts a row when someone logons. It updates MANY rows when someone logoffs.<br \/>\nThere is no match between the record inserted and the records updated (but the session_id).<br \/>\nNeither indexes or constraints.<\/p>\n<p>What&#8217;s the matter?<\/p>\n<p>What happens if we have many logons?<\/p>\n<p><code>SQL&gt; select num_rows from dba_tables where table_name='STATS$USER_LOG';<\/code><\/p>\n<p>NUM_ROWS<br \/>\n&#8212;&#8212;&#8212;-<br \/>\n3053931<\/p>\n<p>What happens if the execution plan does a full scan?<\/p>\n<p>SQL&gt; explain plan for update stats$user_log [&#8230;]<\/p>\n<p>Explained.<\/p>\n<p><code>SQL&gt; @?\/rdbms\/admin\/utlxpls<br \/>\n---------------------------------------------<br \/>\n| Id  | Operation          | Name           |<br \/>\n---------------------------------------------<br \/>\n|   0 | UPDATE STATEMENT   |                |<br \/>\n|   1 |  UPDATE            | STATS$USER_LOG |<br \/>\n|   2 |   TABLE ACCESS FULL| STATS$USER_LOG |<br \/>\n---------------------------------------------<\/code><\/p>\n<p>How many reads should it take?<\/p>\n<p><code>SQL&gt; select bytes\/1024\/1024 Mb from dba_Segments where segment_name='STATS$USER_LOG';<\/code><\/p>\n<p>MB<br \/>\n&#8212;&#8212;&#8212;-<br \/>\n237<\/p>\n<p>The database performace will decrease constantly and very slowly&#8230;..<br \/>\nRemember: never trust a solution if it involves a change on the system.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes people get advice from internet: both Metalink or well-known consulting sites. If people need a fix or a feature, they use to trust advices. Last week I heard a collegue about a 10g RAC database with performance problems and, &hellip; <a href=\"https:\/\/www.ludovicocaldara.net\/dba\/system-triggers-statsuser_log-and-side-effects\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[326,3,6],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-oracle","category-oracledb","category-perf"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":1,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":23,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/22\/revisions\/23"}],"wp:attachment":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}