éçºç°å¢ã§èªåã§SQLã«Explainããããã³ã³ãã¼ãã³ã
CakePHP 1.2.1ã使ã£ã¦ã¾ãã
CakePHPã¯config/core.phpã®debugå¤ã2ã«ããã¨ãç»é¢ã«ã¢ã¯ã»ã¹ããéã«çºè¡ãããSQLæã表示ããã¾ããããã«å ãã¦ãçºè¡ããSQLæã«Explainãããã¦ãã®çµæã追å 表示ããã³ã³ãã¼ãã³ããä½ãã¾ããã
åä½ãã§ãã¯ã¯MySQLã§ãã¾ããããexplainã使ããPostgresãªããã§ãåãã¨æãã¾ããOracleã¨ãexplain planã¨ããããªãããããªãã£ã½ãã®ã§ããã¶ãåãã¾ããã
Explainã®èª¬æãªã©ã¯ä¸è¨ãåç
§(from opparaさんブログ)
http://dev.mysql.com/doc/refman/5.1/ja/explain.html
http://blog.livedoor.jp/nipotan/archives/3743275.html
http://dev.seesaa.net/article/238633.html
追è¨(2009/3/10)
ä¸å
·åãããã¤ããã£ãã®ã§ãã¼ã¸ã§ã³ã¢ãããã¾ãããã½ã¼ã¹ã³ã¼ããªã©ã¯ä¸è¨ã®è¨äºãåç
§ãã ããã
開発環境で自動でSQLにExplainをかけるコンポーネント バージョンアップ(1.0)
ã½ã¼ã¹ã³ã¼ãã¯ここãããã¦ã³ãã¼ãããããä¸è¨ã®ã½ã¼ã¹ã³ã¼ããã³ãã¼&ãã¼ã¹ããã¦ãã ãããï¼ããã°ã«è¼ãã¦ããã½ã¼ã¹ã³ã¼ãã¯å¤ãã®ã§ãZIPãã¡ã¤ã«ããã¦ã³ãã¼ããã¦ãã ããï¼
app/controllers/components/explain_sql.phpã¨ããååã§ä¸è¨ãä¿åãã¦ãã ããã
<?php class ExplainSqlComponent extends Object{ var $_controller; function startup(& $controller) { $this->_controller = $controller; } function showExplainSQL(){ if(Configure::read() < 2){ return ; } if( empty($this->_controller->modelNames) ){ return ; } $html_out = null; $modelname = $this->_controller->modelNames[0]; if( !empty($this->_controller->$modelname->getDataSource()->_queriesLog[0]) ){ $count = 0; $html_out .= '<table>'; foreach($this->_controller->$modelname->getDataSource()->_queriesLog as $key => $value){ //ãããã°æã®DESCRIBEã®SQLæã¯æãã if( !preg_match( '/^DESCRIBE/', $value['query'] ) ){ $result = $this->_controller->$modelname->query("Explain ". $value['query']); $result[0][0]['query'] = $value['query']; //é£æ³é åãã¼ããã¼ãã«ã®ã¿ã¤ãã«ã¨ãã¦è¡¨ç¤º if($count === 0){ $html_out .= '<tr>'; foreach( $result[0][0] as $titlekey => $titleval ){ $html_out .= '<th>'; $html_out .= $titlekey; $html_out .= '</th>'; } $html_out .= '</tr>'; $count++; } //å 容表示 $html_out .= '<tr>'; foreach( $result[0][0] as $titlekey => $titleval ){ $html_out .= '<td style = "text-align: left">'; $html_out .= $titleval." "; $html_out .= '</td>'; } $html_out .= '</tr>'; } } $html_out .= '</table>'; $this->_controller->output .= $html_out; } } } ?>
ããã§ãExplainããããç»é¢ã®ã³ã³ããã¼ã©ã§ã³ã³ãã¼ãã³ãæå®ãã¦ãafterFilterã§å¼ã³åºãã¦ãã ãããä»åã¯ãã¹ã¦ã®ç»é¢ã«å¼·å¶çã«Explainããã¾ãããã«app_controller.phpã«è¨è¼ãã¾ã
app/app_controller.php
<?php class AppController extends Controller { var $components = array('ExplainSql'); function afterFilter(){ parent::afterFilter(); $this->ExplainSql->showExplainSQL(); } } ?>
ãããããã¨ãä¸è¨ã®ç»é¢ã®ããã«ãExplainã®çµæã追å ããã¾ãã
ããã§éçºç»é¢ã§ããã«ã¤ã³ããã¯ã¹ã®æ§åããã§ãã¯ã§ããï¼ï¼
_queriesLogã«ã¯SQLå®è¡æéãªã©ãè¨é²ããã¦ããã®ã§ãããã使ã£ã¦nç§ä»¥ä¸ããã£ãSQLæã®ã¿Explainã¨ããç°¡åã«ã§ãããï¼
追è¨(2009/5/16)
ä¸è¨ããã°ã§ãCakePHP1.1ã§SQL Explain ã³ã³ãã¼ãã³ããåããè¨äºãæ¸ãã¦ããã¾ãããããã¨ããããã¾ã > coelacanthãã
眠るシーラカンスと新米プログラマー「CakePHPのexplainコンポーネントを1.1で動かしてみた」