2008年8月22日星期五

Re: [fw-db] DB Profiler

Plugin:

<?php
class SJCRH_Controller_Plugin_DbProfiler extends Zend_Controller_Plugin_Abstract {

  private $_profiler = null;
  
  public function __construct() {}
  
  public function preDispatch(Zend_Controller_Request_Abstract $request) {
    $config          = Zend_Registry::get('globalConfig');
    $db              = Zend_Registry::get('db');
    $this->_profiler = $db->getProfiler()->setEnabled($config->db->profiler);
  }
 
  public function dispatchLoopShutDown() {
    if ($this->_profiler) {
      $totalTime    = $this->_profiler->getTotalElapsedSecs();
      $queryCount   = $this->_profiler->getTotalNumQueries();
      $longestTime  = 0;
      $longestQuery = null;
      $returnString = '';

      if ($this->_profiler->getQueryProfiles()) {
          foreach ($this->_profiler->getQueryProfiles() as $query) {
            $returnString .= "Current query: {$query->getQuery()}<br/>";
            if ($query->getElapsedSecs() > $longestTime) {
              $longestTime  = $query->getElapsedSecs();
              $longestQuery = $query->getQuery();
            }
          }
        $returnString .= "<code>\n";
        $returnString .= 'Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "<br/>\n";
        $returnString .= 'Average query length: ' . $totalTime / $queryCount . ' seconds' . "<br/>\n";
        $returnString .= 'Queries per second: ' . $queryCount / $totalTime . "<br/>\n";
        $returnString .= 'Longest query length: ' . $longestTime . "<br/>\n";
        $returnString .= "Longest query: \n" . $longestQuery . "<br/>\n";
        $returnString .= "</code>\n";
       
        $this->getResponse()->append('DbProfiler', $returnString);
      }
    }
  }
}


// Bootstrap file
  $frontController->registerPlugin(new SJCRH_Controller_Plugin_DbProfiler());

In the plugin you can turn the profiler on/off

On Fri, Aug 22, 2008 at 9:04 AM, Jeremy Brown <jbrown@spearone.com> wrote:
$db->getProfiler()->setEnabled( true );

// run your query in between the code calls above and below

$profiler = $db->getProfiler();
$lastQuery = $profiler->getLastQueryProfile();
echo $lastQuery->getQuery();


----
Jeremy Brown
Senior Web Developer
Spear One
972.661.6038
www.spearone.com


-----Original Message-----
From: Cory D. Wiles [mailto:kwylez@gmail.com]
Sent: Friday, August 22, 2008 8:25 AM
To: Емил Иванов / Emil Ivanov
Cc: monk.e.boy; fw-db@lists.zend.com
Subject: Re: [fw-db] DB Profiler

You should be able to see the entire query with db profiler so that u
can copy and paste it into your Mysql editor.  I have it setup as a
plugin so that I csm turn it off and on globally

Sent from my iPhone

On Aug 22, 2008, at 6:14 AM, "Емил Иванов / Emil Ivanov"
<emil.vladev@gmail.com> wrote:

> I believe you cannot see the parameters. They are transmitted
> separately from the query. You can try using the mysql query log
> http://dev.mysql.com/doc/refman/5.0/en/query-log.html
>
> Regards,
> Emil Ivanov
>
> 2008/8/22 monk.e.boy <john.glazebrook@guava.com>:
>>
>> How do I extract the actual SQL that was passed to the DB? I want
>> to see how
>> my variables have been inserted (I also want to copy the SQL and
>> run it in
>> my MySQLyog)
>>
>> thanks
>>
>> monk.e.boy
>>
>> http://teethgrinder.co.uk/open-flash-chart/  <-- open source charts
>> --
>> View this message in context: http://www.nabble.com/DB-Profiler-tp19104036p19104036.html
>> Sent from the Zend DB mailing list archive at Nabble.com.
>>
>>
>
>
>
> --
> My place to share my ideas:
> http://vladev.blogspot.com
>




--
Cory Wiles
kwylez@gmail.com
http://www.corywiles.com
http://www.400mtogo.com

没有评论: