3 V4.20 22 Feb 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence. See License.txt.
7 Set tabs to 4 for best viewing.
9 Latest version is available at http://php.weblogs.com/
11 Library for basic performance monitoring and tuning
15 class perf_mysql
extends adodb_perf
{
17 var $tablesSQL = 'show table status';
19 var $createTableSQL = "CREATE TABLE adodb_logsql (
20 created datetime NOT NULL,
21 sql0 varchar(250) NOT NULL,
25 timer decimal(16,6) NOT NULL
28 var $settings = array(
30 'MyISAM cache hit ratio' => array('RATIO',
33 'InnoDB cache hit ratio' => array('RATIO',
36 'data cache hit ratio' => array('HIDE', # only if called
39 'sql cache hit ratio' => array('RATIO',
43 'data reads' => array('IO',
45 'Number of selects (Key_reads is not accurate)'),
46 'data writes' => array('IO',
48 'Number of inserts/updates/deletes * coef (Key_writes is not accurate)'),
51 'MyISAM data cache size' => array('DATAC',
52 array("show variables", 'key_buffer_size'),
54 'BDB data cache size' => array('DATAC',
55 array("show variables", 'bdb_cache_size'),
57 'InnoDB data cache size' => array('DATAC',
58 array("show variables", 'innodb_buffer_pool_size'),
61 'read buffer size' => array('CACHE',
62 array("show variables", 'read_buffer_size'),
64 'sort buffer size' => array('CACHE',
65 array("show variables", 'sort_buffer_size'),
66 'Size of sort buffer (per session)' ),
67 'table cache' => array('CACHE',
68 array("show variables", 'table_cache'),
69 'Number of tables to keep open'),
71 'current connections' => array('SESS',
72 array('show status','Threads_connected'),
74 'max connections' => array( 'SESS',
75 array("show variables",'max_connections'),
81 function perf_mysql(&$conn)
86 function Explain($sql,$partial=false)
89 if (strtoupper(substr(trim($sql),0,6)) !== 'SELECT') return '<p>Unable to EXPLAIN non-select statement</p>';
90 $save = $this->conn
->LogSQL(false);
92 $sqlq = $this->conn
->qstr($sql.'%');
93 $arr = $this->conn
->GetArray("select distinct sql1 from adodb_logsql where sql1 like $sqlq");
95 foreach($arr as $row) {
97 if (crc32($sql) == $partial) break;
101 $sql = str_replace('?',"''",$sql);
104 $sqlq = $this->conn
->qstr($sql.'%');
105 $sql = $this->conn
->GetOne("select sql1 from adodb_logsql where sql1 like $sqlq");
108 $s = '<p><b>Explain</b>: '.htmlspecialchars($sql).'</p>';
109 $rs = $this->conn
->Execute('EXPLAIN '.$sql);
110 $s .= rs2html($rs,false,false,false,false);
111 $this->conn
->LogSQL($save);
112 $s .= $this->Tracer($sql);
118 if (!$this->tablesSQL
) return false;
120 $rs = $this->conn
->Execute($this->tablesSQL
);
121 if (!$rs) return false;
123 $html = rs2html($rs,false,false,false,false);
129 global $ADODB_FETCH_MODE;
130 $save = $ADODB_FETCH_MODE;
131 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
132 $rs = $this->conn
->Execute('show status');
133 $ADODB_FETCH_MODE = $save;
138 switch($rs->fields
[0]) {
140 $val = $rs->fields
[1];
154 global $ADODB_FETCH_MODE;
155 $save = $ADODB_FETCH_MODE;
156 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
157 $rs = $this->conn
->Execute('show status');
158 $ADODB_FETCH_MODE = $save;
163 switch($rs->fields
[0]) {
165 $val +
= $rs->fields
[1]; break;
167 $val +
= $rs->fields
[1]; break;
169 $val +
= $rs->fields
[1]/2;
181 function FindDBHitRatio()
183 // first find out type of table
184 //$this->conn->debug=1;
185 $rs = $this->conn
->Execute('show table status');
187 $type = strtoupper($rs->fields
[1]);
192 return $this->DBParameter('MyISAM cache hit ratio').' (MyISAM)';
194 return $this->DBParameter('InnoDB cache hit ratio').' (InnoDB)';
196 return $type.' not supported';
201 function GetQHitRatio()
203 //Total number of queries = Qcache_inserts + Qcache_hits + Qcache_not_cached
204 $hits = $this->_DBParameter(array("show status","Qcache_hits"));
205 $total = $this->_DBParameter(array("show status","Qcache_inserts"));
206 $total +
= $this->_DBParameter(array("show status","Qcache_not_cached"));
209 if ($total) return ($hits*100)/$total;
214 Use session variable to store Hit percentage, because MySQL
215 does not remember last value of SHOW INNODB STATUS hit ratio
217 # 1st query to SHOW INNODB STATUS
218 0.00 reads/s, 0.00 creates/s, 0.00 writes/s
219 Buffer pool hit rate 1000 / 1000
221 # 2nd query to SHOW INNODB STATUS
222 0.00 reads/s, 0.00 creates/s, 0.00 writes/s
223 No buffer pool activity since the last printout
225 function GetInnoDBHitRatio()
227 global $HTTP_SESSION_VARS;
229 $rs = $this->conn
->Execute('show innodb status');
230 if (!$rs ||
$rs->EOF
) return 0;
231 $stat = $rs->fields
[0];
233 $at = strpos($stat,'Buffer pool hit rate');
234 $stat = substr($stat,$at,200);
235 if (preg_match('!Buffer pool hit rate\s*([0-9]*) / ([0-9]*)!',$stat,$arr)) {
236 $val = 100*$arr[1]/$arr[2];
237 $HTTP_SESSION_VARS['INNODB_HIT_PCT'] = $val;
240 if (isset($HTTP_SESSION_VARS['INNODB_HIT_PCT'])) return $HTTP_SESSION_VARS['INNODB_HIT_PCT'];
246 function GetKeyHitRatio()
248 $hits = $this->_DBParameter(array("show status","Key_read_requests"));
249 $reqs = $this->_DBParameter(array("show status","Key_reads"));
250 if ($reqs == 0) return 0;
252 return ($hits/($reqs+
$hits))*100;