éçºç°å¢ã§èªåã§SQLã«Explainããããã³ã³ãã¼ãã³ã ãã¼ã¸ã§ã³ã¢ãã(1.0)
CakePHP 1.2.1ã使ã£ã¦ã¾ãã
æ¨æ¥æ¸ããAuto Explain Componentの記事ã§ãããåå¿ã¯ã»ã¨ãã©ãªãã¨æã£ã¦ãã½ã¼ã¹ã³ã¼ãã¨ãé©å½ã«æ¸ããç¶æ ã§å ¬éãã¦ãã¾ãã¾ãããåå¿ããã£ã¦ããããã£ãã®ã§ãããããã¤ãä¸å ·åãçºè¦ããã®ã§ããããªããã¼ã¸ã§ã³ã¢ããã§ãã
追è¨
ãã¿ã¾ããããã®è¨äºã¯MySQLã®ã¿å¯¾å¿ã¨ãªãã¾ããPostgreSQLããå©ç¨ã®æ¹ã¯ãä¸è¨ã®è¨äºãåç
§ãã ããã
開発環境で自動でSQLにExplainをかけるコンポーネント バージョンアップ(1.1) PostgreSQL対応
ãã°ã¨ãã¦ã¯ãã¢ãã«ãnewã使ã£ã¦åå¥ã«å¼ã³åºãã¦ãå ´åããinsert, update , deleteãªã©ãçºè¡ãããå ´åã¯ãã¾ãåãã¾ããã§ãããä»åã¯ãã®ãã°ã®ä¿®æ£ã¨ã追å æ©è½ã¨ãã¦ãããç§æ°ä»¥ä¸ããã£ãSelectæã®ã¿Explainããããã«ãã¾ãããããã©ã«ãã¯0ç§ä»¥ä¸ã®ã¯ã¨ãªãªã®ã§å ¨ã¦ã®SelectæãExplain対象ã§ãã
表示å
容ã¯ä¸è¨ã®ç»åã®ããã«ä½ãå¤åã¯ããã¾ããã
åºæ¬çã«ã¯componentãã¡ã¤ã«ãå ¥ãæ¿ããã ãã§OKã§ãã
ã³ã³ãã¼ãã³ããReadmeãªã©ã®ãã¡ã¤ã«ã¯このZIPをダウンロードããããä¸è¨ã®ã³ã¼ããã³ãã¼&ãã¼ã¹ããã¦ãã ããã
app/controllers/components/explain_sql.php
<?php /** * ExplainSqlComponent - Auto execute SQL Explain and set results in the debug mode. * * Copyright (c) 2009 Yasushi Ichikawa * * Use this compnent in afterFilter or afterRender. * var $components = array('ExplainSql'); * $this->ExplainSql->showExplainSQL( $slowQueryThreshold = 0 ); * * @author Yasushi Ichikawa * @version 1.0 * */ class ExplainSqlComponent extends Object{ /** * * @var controller object */ var $_controller; /** * set conroller object */ function startup(& $controller) { $this->_controller = $controller; } /** * Get all SQL query and execute SQL Explain of them without DESCRIBE query. * * if set the $slowQueryThreshold, * execute SQL Explain only slow query which are spent over $slowQueryThreshold seconds. * * @param integer $slowQueryThreshold * @access public */ function showExplainSQL( $slowQueryThreshold = 0 ){ $explain_results = array(); $count = 1; if(Configure::read() < 2 ){ return ; } if (!class_exists('ConnectionManager')) { return ; } $dbConfigs = ConnectionManager::sourceList(); foreach ( $dbConfigs as $configName ) { $db =& ConnectionManager::getDataSource( $configName ); if( empty($db->_queriesLog[0]) ){ continue; } foreach( $db->_queriesLog as $key => $value ){ if( preg_match( '/^SELECT /i', $value['query'] ) ãããããããããããããããããã&& $value['took'] >= $slowQueryThreshold ){ $reesults = null; $results = $db->query( "Explain ". $value['query'] ); $results[0][0]['query'] = $value['query']; $results[0][0]['id'] = $count; $explain_results[] = $results[0][0]; $count++; } } } if( !empty( $explain_results[0] ) ){ $this->_outputHtml( $explain_results ); } return; } /** * set SQL Explain results on the controller->output. * * @param array $explain_results */ function _outputHtml( $explain_results ){ $html_out = '<table>'; $html_out .= '<tr>'; //set table column name foreach( $explain_results[0] as $titlekey => $titleval ){ $html_out .= '<th>'; $html_out .= $titlekey; $html_out .= '</th>'; } $html_out .= '</tr>'; //set results foreach($explain_results as $recordnum => $val_arr){ $html_out .= '<tr>'; foreach( $val_arr as $key => $value ){ $html_out .= '<td style = "text-align: left">'; $html_out .= $value." "; $html_out .= '</td>'; } $html_out .= '</tr>'; } $html_out .= '</table>'; $this->_controller->output .= $html_out; } } ?>
使ãæ¹ã¯åã¨åãããã«ãafterRenderã¨ãafterFileterã«å
¥ãã¦ãã ãããä»åã®è¿½å æ©è½ã§ãããæå®ç§æ°ä»¥ä¸ããã£ãã¯ã¨ãªã®ã¿Explainããããã«ã第ä¸å¼æ°ã«æ°å¤ãã»ããããããã«ãã¾ããããããããªãã¦ãåãã¾ãï¼å
¨ã¦ã®SelectãExplain対象ï¼ã
ä»åã®ä¾ã¯ãå
¨ã¦ã®ç»é¢ã«è¡¨ç¤ºãããããã«ãapp/app_controller.phpã«è¨è¼ããä¾ã§ãã
<?php class AppController extends Controller { var $components = array('ExplainSql'); function afterFilter(){ parent::afterFilter(); $this->ExplainSql->showExplainSQL( $slowQueryThreshold = 0 ); } } ?>
åã®ãã¼ã¸ã§ã³ã ã¨ãçºè¡ãããã¯ã¨ãªã®åå¾ããããããé©å½ãªã¢ãã«ã使ã£ã¦åå¾ãã¦ããã§ãããä»åã®ãã¼ã¸ã§ã³ããçºè¡ãããã¯ã¨ãªã®åå¾æ¹æ³ãå¤ãã¾ãããããã«config/database.phpã§$dafault以å¤ã®DBã使ã£ã¦ãå ´åã§ãåãããã«ãªã£ã¦ãã¨æãã¾ãï¼Selectã ãã¹ã¬ã¼ãDBãè¦ã¦ããå ´åã¨ãï¼ã
ä¸è¨ã®mcurryさんã®ã½ã¼ã¹ã³ã¼ããåèã«ãã¾ããã
http://github.com/mcurry/cakephp/tree/master/plugins/sql_log