{"id":2125,"date":"2018-07-08T16:56:40","date_gmt":"2018-07-08T16:56:40","guid":{"rendered":"http:\/\/nenadnoveljic.com\/blog\/?p=2125"},"modified":"2018-07-08T17:00:01","modified_gmt":"2018-07-08T17:00:01","slug":"converting-hash_value-to-sql_id","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/","title":{"rendered":"Converting HASH_VALUE to SQL_ID"},"content":{"rendered":"<p>The V$SQL.SQL_ID is a 64-bit number in a base32 notation. And the V$SQL.HASH_VALUE stores its least significant 32-bits in the decimal notation, for example:<\/p>\n<pre><code>select hash_value from v$sql where sql_id='<span style=\"color:red\">7wckkp93ya8a2<\/span>' ;\r\n\r\nHASH_VALUE\r\n----------\r\n<span style=\"color:blue\">1206198594<\/span><\/code><\/pre>\n<p>This means that the most significant 32 bits will inevitably be lost when converting the V$SQL.HASH_VALUE back to the SQL_ID. Nevertheless, at least we can get its bottom 32 bits with the <a href=\"http:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/\">SQL for converting decimal integers to any base<\/a>:<\/p>\n<pre><code>define b = 32\r\ncolumn base&&b format a13\r\n\r\ndefine d = <span style=\"color:blue\">1206198594<\/span>\r\ncolumn base10 format 9999999999999999999\r\n\r\nselect &&d base10,\r\n    listagg(\r\n        substr(\r\n          case &&b when 32 then \r\n            '0123456789abcdfghjkmnpqrstuvwxyz'\r\n          else\r\n            '0123456789abcdefghjkmnpqrstuvwxyz'\r\n          end,\r\n          mod(\r\n            trunc(&&d\/power(&&b,level-1)),\r\n            &&b\r\n          ) + 1 ,\r\n          1\r\n        ) \r\n    ) within group (order by level desc) base&&b \r\n  from dual \r\n  connect by level <= ceil(log(&#038;&#038;b,&#038;&#038;d+1))\r\n;\r\n\r\n              BASE10 BASE32\r\n-------------------- -------------\r\n          <span style=\"color:blue\">1206198594<\/span> <span style=\"color:red\">13ya8a2<\/span><\/code><\/pre>\n<p>Now take a look at the SQL_ID in the ADS trace excerpt below:<\/p>\n<pre><code>kkoadsComputeSqlid: sql_id=<span style=\"color:brown\">9093421488918569282<\/span>: newText=SELECT \/* DS_SVC *\/  NVL(SUM(C1),0) FROM (SELECT \/*+ qb_name(\"innerQuery\") NO_INDEX_FFS( \"T1#1\")  *\/ 1 AS C1 FROM \"T1\" \"T1#1\", \"T2\" \"T2#0\" WHERE (\"T2#0\".\"N2\"=2) AND (\"T1#1\".\"N1\"=\"T2#0\".\"N1\") AND (\"T1#1\".\"N2\"=2) AND (\"T1#1\".\"N3\"=2)) innerQuery, startPos = 20, stopPos = 120, newTextLen = 244, sqlLen = 343<\/code><\/pre>\n<p>At first glance, <span style=\"color:brown\">9093421488918569282<\/span> looks entirely different from the V$SQL.SQL_ID and the V$SQL.HASH_VALUE, <span style=\"color:red\">7wckkp93ya8a2<\/span> and <span style=\"color:blue\">1206198594<\/span>, respectively. <\/p>\n<p>Jonathan Lewis, however, kindly <a href=\"https:\/\/www.freelists.org\/post\/oracle-l\/sql-id-in-ADS-trace,4\">pointed out<\/a> that the SQL_ID in the ADS trace is nothing else but the V$SQL.SQL_ID in the decimal notation. Consequently, we can use the same method to obtain the V$SQL.SQL_ID:<\/p>\n<pre><code>define d = <span style=\"color:brown\">9093421488918569282<\/span>\r\n\/\r\n\r\n              BASE10 BASE32\r\n-------------------- -------------\r\n <span style=\"color:brown\">9093421488918569282<\/span> <span style=\"color:red\">7wckkp93ya8a2<\/span><\/code><\/pre>\n<p>References:<\/p>\n<ul>\n<li><a href=\"https:\/\/blog.tanelpoder.com\/2009\/02\/22\/sql_id-is-just-a-fancy-representation-of-hash-value\/\">SQL_ID is just a fancy representation of hash value<\/a>, Tanel Poder<\/li>\n<li><a href=\"https:\/\/mauro-pagano.com\/2016\/11\/28\/something-new-about-sql-plan-directives-and-12-2\/\">Something new about SQL Plan Directives and 12.2<\/a>, Mauro Pagano<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Converting HASH_VALUE to SQL_ID with SQL <a href=\"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/\" class=\"more-link\">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":{"inline_featured_image":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2125","post","type-post","status-publish","format-standard","hentry","category-oracle"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Converting HASH_VALUE to SQL_ID - All-round Database Topics<\/title>\n<meta name=\"description\" content=\"Converting HASH_VALUE to SQL_ID with SQL\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Converting HASH_VALUE to SQL_ID - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"Converting HASH_VALUE to SQL_ID with SQL\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-08T16:56:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-07-08T17:00:01+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Converting HASH_VALUE to SQL_ID\",\"datePublished\":\"2018-07-08T16:56:40+00:00\",\"dateModified\":\"2018-07-08T17:00:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/\"},\"wordCount\":178,\"commentCount\":1,\"articleSection\":[\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/\",\"name\":\"Converting HASH_VALUE to SQL_ID - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2018-07-08T16:56:40+00:00\",\"dateModified\":\"2018-07-08T17:00:01+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"description\":\"Converting HASH_VALUE to SQL_ID with SQL\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/converting-hash_value-to-sql_id\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Converting HASH_VALUE to SQL_ID\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Converting HASH_VALUE to SQL_ID - All-round Database Topics","description":"Converting HASH_VALUE to SQL_ID with SQL","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/","og_locale":"en_US","og_type":"article","og_title":"Converting HASH_VALUE to SQL_ID - All-round Database Topics","og_description":"Converting HASH_VALUE to SQL_ID with SQL","og_url":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/","og_site_name":"All-round Database Topics","article_published_time":"2018-07-08T16:56:40+00:00","article_modified_time":"2018-07-08T17:00:01+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Converting HASH_VALUE to SQL_ID","datePublished":"2018-07-08T16:56:40+00:00","dateModified":"2018-07-08T17:00:01+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/"},"wordCount":178,"commentCount":1,"articleSection":["Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/","url":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/","name":"Converting HASH_VALUE to SQL_ID - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2018-07-08T16:56:40+00:00","dateModified":"2018-07-08T17:00:01+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"description":"Converting HASH_VALUE to SQL_ID with SQL","breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/converting-hash_value-to-sql_id\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Converting HASH_VALUE to SQL_ID"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=2125"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2125\/revisions"}],"predecessor-version":[{"id":2141,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2125\/revisions\/2141"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=2125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=2125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=2125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}