{"id":1105,"date":"2011-03-26T08:35:27","date_gmt":"2011-03-26T13:35:27","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=1105"},"modified":"2011-07-06T09:42:13","modified_gmt":"2011-07-06T14:42:13","slug":"which-service-is-executing-a-given-sql-statement","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2011\/03\/26\/which-service-is-executing-a-given-sql-statement\/","title":{"rendered":"Which service is executing a given SQL statement"},"content":{"rendered":"<p>We have a shared PLSQL code base between several pieces of application services.  Some of them use all of it, some only a few pieces.  We also use database services for <a href=http:\/\/appcrawler.com\/wordpress\/2010\/10\/18\/generating-chargeback-costs-using-awr target=_blank>accounting reasons.<\/a><\/p>\n<p>I needed to determine which database service was executing a given SQL statement while we plan a migration that may impact those that execute that SQL.  I ended up using what is below.  It isn&#8217;t perfect, but it will tell you the count of times a given service was found executing that SQL as recorded in DBA_HIST_ACTIVE_SESS_HISTORY.  It is definitely useful for a high level &#8220;landscape&#8221; for planning.<\/p>\n<pre lang=\"python\" line=\"1\">\r\n#!\/home\/oracle\/local\/bin\/python\r\n\r\nimport cx_Oracle, sys\r\n\r\nconn = cx_Oracle.connect(mode = cx_Oracle.SYSDBA)\r\ncursor = conn.cursor()\r\n\r\ncursor.execute(\"select min(snap_id),max(snap_id) from dba_hist_snapshot\")\r\n\r\nfor row in cursor:\r\n  min_snap = row[0]\r\n  max_snap = row[1]\r\n\r\nwhile min_snap <= max_snap:\r\n  cursor.execute(\"\"\"\r\n                  select count(*) cnt,service_hash\r\n                    from dba_hist_active_sess_history\r\n                    where snap_id = :1 \r\n                      and sql_id = 'grjrw3a6z22k1'\r\n                    group by service_hash\r\n                 \"\"\",[min_snap])\r\n  for row in cursor:\r\n    print row[1],row[0]\r\n    sys.stdout.flush()\r\n  min_snap = min_snap + 1\r\n<\/pre>\n<p>We could also format the totals in python, but we just let them print out and then totaled them using awk.<\/p>\n<pre lang=\"awk\" line=\"1\">\r\n09:32:10 oracle@wcdb01pxwe ~ >awk '{s[$1]+=$2} END {for (i in s) {print i,s[i]}}' l.txt\r\n2217777499 9044\r\n3540941183 2013\r\n3493663839 1\r\n09:32:14 oracle@wcdb01pxwe ~ >\r\n<\/pre>\n<p>You can then take the actual service_hash printed in the first column of the awk output and get its name with the following...<\/p>\n<pre lang=\"sql\">\r\nselect name from dba_services where name_hash = 2217777499;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We have a shared PLSQL code base between several pieces of application services. Some of them use all of it, some only a few pieces. We also use database services for accounting reasons. I needed to determine which database service&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2011\/03\/26\/which-service-is-executing-a-given-sql-statement\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,24,22,26],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1105"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1105"}],"version-history":[{"count":8,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1105\/revisions"}],"predecessor-version":[{"id":1283,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1105\/revisions\/1283"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1105"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}