Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use DBD::Oracle qw(:ora_session_modes);
  5.  
  6. ### Variaveis e conexao ao Oracle.
  7. my $oracle_hostname = $ARGV[0];
  8. my $oracle_database = $ARGV[1];
  9. my $oracle_port = $ARGV[2];
  10. my $oracle_username = 'SYS';
  11. my $oracle_password = 'Nerv2013';
  12. my $oracle_dbh = DBI->connect("dbi:Oracle:host=$oracle_hostname;sid=$oracle_database;port=$oracle_port", $oracle_username, $oracle_password, {RaiseError => 1, AutoCommit => 0, ora_session_mode => ORA_SYSDBA});
  13. $oracle_dbh->{LongReadLen} = 20*1024*1024; # 20MB
  14.  
  15. open(LOG, '>C:\temp\OracleTuning.log') || die ("Could not open file!");
  16.  
  17. ### Remover o Tuning Task se ele ja existe.
  18. my $task_exist = 0;
  19. my $oracle_sql_01 = "SELECT TASK_ID FROM DBA_ADVISOR_TASKS WHERE OWNER = 'SYS' AND TASK_NAME = 'Portilho Tuning Task'";
  20. my $oracle_sth_01 = $oracle_dbh->prepare($oracle_sql_01);
  21. $oracle_sth_01->execute();
  22. while (my $oracle_ref_01 = $oracle_sth_01->fetchrow_hashref())
  23. {
  24. $task_exist++;
  25. }
  26. $oracle_sth_01->finish();
  27. if ($task_exist > 0)
  28. {
  29. my $oracle_sql_02 = "BEGIN DBMS_SQLTUNE.DROP_TUNING_TASK('Portilho Tuning Task'); END;";
  30. my $oracle_sth_02 = $oracle_dbh->prepare($oracle_sql_02);
  31. $oracle_sth_02->execute();
  32. $oracle_sth_02->finish();
  33. }
  34.  
  35. ### Executar
  36. my @Order = ('ELAPSED_TIME', 'CPU_TIME', 'DISK_READS', 'BUFFER_GETS', 'DIRECT_WRITES', 'SORTS');
  37. foreach (@Order)
  38. {
  39. my $Order = $_;
  40. print "\nAnalisando os TOP 100 SQLs ordenados por $Order...\n\n";
  41. print LOG "\nAnalisando os TOP 100 SQLs ordenados por $Order...\n\n";
  42. ### Recuperar os Top SQL.
  43. my $oracle_sql_03 = "SELECT SQL_ID, SQL_TEXT FROM V\$SQL WHERE PARSING_SCHEMA_NAME IS NOT NULL AND ROWNUM < 101 ORDER BY $Order";
  44. my $oracle_sth_03 = $oracle_dbh->prepare($oracle_sql_03);
  45. $oracle_sth_03->execute();
  46. while (my $oracle_ref_03 = $oracle_sth_03->fetchrow_hashref())
  47. {
  48. my $sql_id = $oracle_ref_03->{SQL_ID};
  49. my $sql_text = $oracle_ref_03->{SQL_TEXT};
  50.  
  51. ### Verificar se o SQL ainda existe.
  52. my $sql_exist = 0;
  53. my $oracle_sql_04 = "SELECT SQL_TEXT FROM V\$SQL WHERE SQL_ID= '$sql_id'";
  54. my $oracle_sth_04 = $oracle_dbh->prepare($oracle_sql_04);
  55. $oracle_sth_04->execute();
  56. while (my $oracle_ref_04 = $oracle_sth_04->fetchrow_hashref())
  57. {
  58. $sql_exist++;
  59. }
  60. $oracle_sth_04->finish();
  61.  
  62. ### Executar o Tuning Task
  63. if ($sql_exist > 0)
  64. {
  65. my $oracle_sql_05 = "DECLARE RET_VAL VARCHAR2(4000); BEGIN RET_VAL := DBMS_SQLTUNE.CREATE_TUNING_TASK(SQL_ID => '$sql_id', SCOPE => DBMS_SQLTUNE.SCOPE_COMPREHENSIVE, TIME_LIMIT => 60, TASK_NAME => 'Portilho Tuning Task', DESCRIPTION => 'Portilho Tuning Task'); END;";
  66. my $oracle_sth_05 = $oracle_dbh->prepare($oracle_sql_05);
  67. $oracle_sth_05->execute();
  68. $oracle_sth_05->finish();
  69.  
  70. my $oracle_sql_06 = "BEGIN DBMS_SQLTUNE.EXECUTE_TUNING_TASK('Portilho Tuning Task'); END;";
  71. my $oracle_sth_06 = $oracle_dbh->prepare($oracle_sql_06);
  72. $oracle_sth_06->execute();
  73. $oracle_sth_06->finish();
  74.  
  75. ### Exibição da recomendação.
  76. my $oracle_sql_07 = "SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK('Portilho Tuning Task') RECOMMENTATION FROM DUAL";
  77. my $oracle_sth_07 = $oracle_dbh->prepare($oracle_sql_07);
  78. $oracle_sth_07->execute();
  79. while (my $oracle_ref_07 = $oracle_sth_07->fetchrow_hashref())
  80. {
  81. my $recommendation = $oracle_ref_07->{RECOMMENTATION};
  82. print "$recommendation\n\n";
  83. print LOG "$recommendation\n\n";
  84. }
  85.  
  86. ### Execução da recomendação.
  87. my $oracle_sql_08 = "SELECT DBMS_SQLTUNE.SCRIPT_TUNING_TASK('Portilho Tuning Task') RECOMMENTATION FROM DUAL";
  88. my $oracle_sth_08 = $oracle_dbh->prepare($oracle_sql_08);
  89. $oracle_sth_08->execute();
  90. while (my $oracle_ref_08 = $oracle_sth_08->fetchrow_hashref())
  91. {
  92. my $recommendation = $oracle_ref_08->{RECOMMENTATION};
  93. if ($recommendation !~ m/There are no recommended actions for this task under the given filters./)
  94. {
  95. my @CompleteRecommendation = split /\n/, $recommendation;
  96. foreach (@CompleteRecommendation)
  97. {
  98. my $RecommendationLine = $_;
  99. unless ($RecommendationLine =~ /;/gm) {next;}
  100.  
  101. if ($RecommendationLine =~ /^create index /) {$RecommendationLine =~ s/;//;}
  102. if ($RecommendationLine =~ /^execute /) {$RecommendationLine =~ s/execute //; $RecommendationLine = "BEGIN $RecommendationLine END;";}
  103. my $oracle_sql_09 = "$RecommendationLine";
  104. my $oracle_sth_09 = $oracle_dbh->prepare($oracle_sql_09);
  105. print "Recommendation to implement: $RecommendationLine\n\n";
  106. print LOG "Recommendation to implement:: $RecommendationLine\n\n";
  107.  
  108. print "Do you wish to implement it? (Y/N)";
  109. while ()
  110. {
  111. my $option = $_;
  112. if ($option eq 'Y')
  113. {
  114. $oracle_sth_09->execute();
  115. $oracle_sth_09->finish();
  116. print "Recommendation IMPLEMENTED.\n\n";
  117. last;
  118. }
  119. else
  120. {
  121. print "Recommendation NOT IMPLEMENTED.\n\n";
  122. last;
  123. }
  124. }
  125. }
  126. }
  127. print "\n\n\n\n\n";
  128. }
  129. $oracle_sth_08->finish();
  130.  
  131. my $oracle_sql_10 = "BEGIN DBMS_SQLTUNE.DROP_TUNING_TASK('Portilho Tuning Task'); END;";
  132. my $oracle_sth_10 = $oracle_dbh->prepare($oracle_sql_10);
  133. $oracle_sth_10->execute();
  134. $oracle_sth_10->finish();
  135. }
  136. }
  137. $oracle_sth_03->finish();
  138. }
  139. $oracle_dbh->disconnect;
  140. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement