Author: spadkins
Date: Mon Sep  4 11:55:47 2006
New Revision: 6847

Modified:
   p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm

Log:
clean MySQL explain

Modified: p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm       (original)
+++ p5ee/trunk/App-Repository/lib/App/Repository/MySQL.pm       Mon Sep  4 
11:55:47 2006
@@ -416,19 +416,34 @@
             # do nothing
         }
         if ($sql =~ /^select/i) {
-            my ($rows);
+            my ($rows, $posskeys, $key, $keylen);
             eval {
                 $rows = $dbh->selectall_arrayref("explain $sql");
             };
             print "EXPLAIN_SQL: $DBI::errstr\n";
             if ($rows) {
-                print 
"+----+-------------+-------+-------+---------------------------------------+--------------------+---------+-------------+-------+\n";
-                print "| id | select_type | table | type  | possible_keys      
                   | key                | key_len | ref         |  rows | 
Extra\n";
-                print 
"+----+-------------+-------+-------+---------------------------------------+--------------------+---------+-------------+-------+\n";
+                print 
"+----+-------------+----------------------+-------+----------------------+---------+----------+\n";
+                print "| id | select_type | table                | type  | key 
                 | key_len |     rows |\n";
+                print 
"+----+-------------+----------------------+-------+----------------------+---------+----------+\n";
                 foreach my $row (@$rows) {
-                    printf("|%3s | %-12s| %-6s| %-6s| %-38s| %-19s|%8d | 
%-12s|%6d | %s\n", @$row);
+                    $key = $row->[5];
+                    $keylen = length($key);
+                    if ($keylen > 21) {
+                       $key = substr($key,0,12) . ".." . 
substr($key,$keylen-7,7);
+                    }
+                    printf("|%3s | %-12s| %-21s| %-6s| %-21s|%8d |%9d | %s\n", 
@{$row}[0,1,2,3], $key, @{$row}[6,8]);
                 }
-                print 
"+----+-------------+-------+-------+---------------------------------------+--------------------+---------+-------------+-------+\n";
+                print 
"+----+----------------------------------------------------------------------------------------+\n";
+                print "| id | possible_keys/ref/extra\n";
+                print 
"+----+----------------------------------------------------------------------------------------+\n";
+                foreach my $row (@$rows) {
+                    $key = $row->[5];
+                    $posskeys = $row->[4];
+                    $posskeys =~ s/\b($key)\b/[$key]/;
+                    printf("|%3s | posskeys: %s\n", $row->[0], $posskeys);
+                    printf("|%3s | ref:      %s; extra: %s\n", @{$row}[0,7,9]);
+                }
+                print 
"+---------------------------------------------------------------------------------------------+\n";
             }
         }
         else {

Reply via email to