--- optimize_mythdb.pl	2015-07-08 03:01:25.000000000 +0100
+++ jp_optimize_and_defrag_mythdb.pl	2015-07-08 19:53:43.269373742 +0100
@@ -2,6 +2,7 @@
 #
 # Connects to the mythtv database and repairs/optimizes the tables that it
 # finds.  Suggested use is to cron it to run once per day.
+# Timing additions by JP following http://www.perlmonks.org/?node_id=795716
 #
 # @url       $URL$
 # @date      $Date$
@@ -13,6 +14,10 @@
 # Includes
     use DBI;
     use MythTV;
+    use Time::HiRes qw( time );  # If required
+    
+# Setup timing stuff
+    my @t;
 
 # Connect to mythbackend
     my $Myth = new MythTV({'connect' => 0});
@@ -22,6 +27,8 @@
 
 # Repair and optimize each table
     foreach $table ($dbh->tables) {
+        $t[0]= time();
+        print "Working on: $table\n";
         unless ($dbh->do("REPAIR TABLE $table")) {
             print "Skipped:  $table\n";
             next;
@@ -32,18 +39,43 @@
         if ($dbh->do("ANALYZE TABLE $table")) {
             print "Analyzed: $table\n";
         }
+    $t[1]= time();
+    my $diff = formatted_time_diff(@t);
+    print "Processing took $diff seconds\n";
     }
 
-# Defragement seek table
+# Defrag some big tables; see Ticket #12451    
+
+# Defragment seek table
+    $t[0]= time();
+    print "Defragging: recordedseek\n";
     if ($dbh->do("ALTER TABLE `recordedseek` ORDER BY chanid, starttime, type")) {
         print "Defragmented: recordedseek\n";
+        $t[1]= time();
+        my $diff = formatted_time_diff(@t);
+        print "Processing took $diff seconds\n";        
     }
-# Defragement program table
+# Defragment program table
+    $t[0]= time();
+    print "Defragging: program\n";
     if ($dbh->do("ALTER TABLE `program` ORDER BY starttime, chanid")) {
         print "Defragmented: program\n";
+        $t[1]= time();
+        my $diff = formatted_time_diff(@t);
+        print "Processing took $diff seconds\n";
     }
-# Defragement video seek table
+# Defragment video seek table
+    $t[0]= time();
+    print "Defragging: filemarkup\n";
     if ($dbh->do("ALTER TABLE `filemarkup` ORDER BY filename")) {
-        print "Defragmented: filemarkup\n";
+       print "Defragmented: filemarkup\n";
+       $t[1]= time();
+       my $diff = formatted_time_diff(@t);
+       print "Processing took $diff seconds\n";
     }
 
+# Format calculated time interval
+
+    sub formatted_time_diff {
+       return sprintf("%.2f", $_[1]-$_[0])
+    }
