{"id":215,"date":"2017-07-11T15:39:04","date_gmt":"2017-07-11T13:39:04","guid":{"rendered":"https:\/\/blog.kurschies.de\/?p=215"},"modified":"2017-07-13T15:11:15","modified_gmt":"2017-07-13T13:11:15","slug":"how-to-use-sql-tuning-advisor-with-sqlplus","status":"publish","type":"post","link":"https:\/\/blog.kurschies.de\/index.php\/2017\/07\/11\/how-to-use-sql-tuning-advisor-with-sqlplus\/","title":{"rendered":"how to use SQL tuning advisor with SQL*Plus"},"content":{"rendered":"<p>because I&#8217;ve worked on a lot of environments where I only have ssh access, i cannot use graphical tools like Toad or SQL Developer&#8230; so I have to find a suitable way to tune sql statements with sql*plus&#8230; I&#8217;m just describing how to use the sql tuning advisor, how to find the correct sql_id will be another artikel.<\/p>\n<p>TAKE CARE: you need the oracle diagnostic &#038; tuning pack license!<\/p>\n<p>ok, not the technical part&#8230; there are 3 steps: create a tuning task, where you define what oracle should do, execute the tuning task and show the result of the tuning task. Lets start with #1<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefine sql_id=abcdefghijkl\r\ndefine begin_snap=1234\r\ndefine end_snap=1235\r\n\r\nDECLARE\r\n  l_sql_tune_task_id  VARCHAR2(100);\r\nBEGIN\r\n  l_sql_tune_task_id := DBMS_SQLTUNE.create_tuning_task (                   \r\n                          begin_snap  =&gt; &amp;begin_snap,\r\n                          end_snap    =&gt; &amp;end_snap,\r\n                          sql_id      =&gt; '&amp;sql_id',\r\n                          scope       =&gt; DBMS_SQLTUNE.scope_comprehensive,\r\n                          time_limit  =&gt; 7200, -- max runtime in seconds... so this means 2 hours\r\n                          task_name   =&gt; '&amp;sql_id._AWR_tuning_task');\r\n  DBMS_OUTPUT.put_line('l_sql_tune_task_id: ' || l_sql_tune_task_id);\r\nEND;\r\n\/\r\n<\/pre>\n<p>ok, now you have to execute the tuning task<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nEXEC DBMS_SQLTUNE.execute_tuning_task(task_name =&gt; '&amp;sql_id._AWR_tuning_task');\r\n<\/pre>\n<p>and when the task has finished, you can view the report with this sql statement:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nSET LONG 10000;\r\nSET PAGESIZE 1000\r\nSET LINESIZE 200\r\ncolumn RECOMMENDATIONS format a200\r\n-- optionally you can spool the output\r\n-- spool sql_advisor_&amp;sql_id._AWR_tuning_task.txt\r\nSELECT DBMS_SQLTUNE.report_tuning_task('&amp;sql_id._AWR_tuning_task') AS recommendations FROM dual;\r\n-- spool off\r\n<\/pre>\n<p>if you want to accept a sql profile, the syntax to do so is withing the report, but are always the same&#8230;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nexec dbms_sqltune.accept_sql_profile(task_name =&gt; '&amp;sql_id._AWR_tuning_task', replace =&gt; TRUE);\r\n<\/pre>\n<p>if you want to undo (=remove) the sql profile, you just have to execute this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nexec DBMS_SQLTUNE.drop_sql_profile (name =&gt; '&amp;sql_id._AWR_tuning_task');\r\n<\/pre>\n<p>you can drop the tuning task including the report (but without the profile) with this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nexec dbms_sqltune.drop_tuning_task(task_name =&gt; '&amp;sql_id._AWR_tuning_task');\r\n<\/pre>\n<p>if you want to see the which sql tuning tasks exist, you can query the related dba view&#8230;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nselect task_name from DBA_ADVISOR_TASKS where advisor_name='SQL Tuning Advisor';\r\n<\/pre>\n<p>small hint: The automatic sql tuning advisor is running every night (if you haven&#8217;t disabled it). Internally it&#8217;s just a regular sql tuning task called &#8216;SYS_AUTO_SQL_TUNING_TASK&#8217;&#8230; you can get the report as described above (you just have to replace the task name) and see the recommendations of all automatically analysed sql statements within your AWR retention time&#8230; pretty cool to get an overview of slow sql. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>because I&#8217;ve worked on a lot of environments where I only have ssh access, i cannot use graphical tools like Toad or SQL Developer&#8230; so I have to find a suitable way to tune sql statements with sql*plus&#8230; I&#8217;m just &hellip; <a href=\"https:\/\/blog.kurschies.de\/index.php\/2017\/07\/11\/how-to-use-sql-tuning-advisor-with-sqlplus\/\">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":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8m6YC-3t","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":5,"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":225,"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/posts\/215\/revisions\/225"}],"wp:attachment":[{"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kurschies.de\/index.php\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}