3 V4.94 23 Jan 2007 (c) 2000-2007 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://adodb.sourceforge.net
11 Library for basic performance monitoring and tuning
15 // security - hide paths
16 if (!defined('ADODB_DIR')) die();
18 class perf_oci8
extends ADODB_perf
{
20 var $tablesSQL = "select segment_name as \"tablename\", sum(bytes)/1024 as \"size_in_k\",tablespace_name as \"tablespace\",count(*) \"extents\" from sys.user_extents
21 group by segment_name,tablespace_name";
24 var $createTableSQL = "CREATE TABLE adodb_logsql (
25 created date NOT NULL,
26 sql0 varchar(250) NOT NULL,
27 sql1 varchar(4000) NOT NULL,
30 timer decimal(16,6) NOT NULL
33 var $settings = array(
35 'data cache hit ratio' => array('RATIOH',
36 "select round((1-(phy.value / (cur.value + con.value)))*100,2)
37 from v\$sysstat cur, v\$sysstat con, v\$sysstat phy
38 where cur.name = 'db block gets' and
39 con.name = 'consistent gets' and
40 phy.name = 'physical reads'",
43 'sql cache hit ratio' => array( 'RATIOH',
44 'select round(100*(sum(pins)-sum(reloads))/sum(pins),2) from v$librarycache',
45 'increase <i>shared_pool_size</i> if too ratio low'),
47 'datadict cache hit ratio' => array('RATIOH',
49 round((1 - (sum(getmisses) / (sum(gets) +
50 sum(getmisses))))*100,2)
52 'increase <i>shared_pool_size</i> if too ratio low'),
54 'memory sort ratio' => array('RATIOH',
55 "SELECT ROUND((100 * b.VALUE) /DECODE ((a.VALUE + b.VALUE),
56 0,1,(a.VALUE + b.VALUE)),2)
59 WHERE a.name = 'sorts (disk)'
60 AND b.name = 'sorts (memory)'",
61 "% of memory sorts compared to disk sorts - should be over 95%"),
64 'data reads' => array('IO',
65 "select value from v\$sysstat where name='physical reads'"),
67 'data writes' => array('IO',
68 "select value from v\$sysstat where name='physical writes'"),
71 'data cache buffers' => array( 'DATAC',
72 "select a.value/b.value from v\$parameter a, v\$parameter b
73 where a.name = 'db_cache_size' and b.name= 'db_block_size'",
74 'Number of cache buffers. Tune <i>db_cache_size</i> if the <i>data cache hit ratio</i> is too low.'),
75 'data cache blocksize' => array('DATAC',
76 "select value from v\$parameter where name='db_block_size'",
79 'data cache size' => array('DATAC',
80 "select value from v\$parameter where name = 'db_cache_size'",
82 'shared pool size' => array('DATAC',
83 "select value from v\$parameter where name = 'shared_pool_size'",
84 'shared_pool_size, which holds shared sql, stored procedures, dict cache and similar shared structs' ),
85 'java pool size' => array('DATAJ',
86 "select value from v\$parameter where name = 'java_pool_size'",
88 'large pool buffer size' => array('CACHE',
89 "select value from v\$parameter where name='large_pool_size'",
90 'this pool is for large mem allocations (not because it is larger than shared pool), for MTS sessions, parallel queries, io buffers (large_pool_size) ' ),
92 'pga buffer size' => array('CACHE',
93 "select value from v\$parameter where name='pga_aggregate_target'",
94 'program global area is private memory for sorting, and hash and bitmap merges - since oracle 9i (pga_aggregate_target)' ),
98 'current connections' => array('SESS',
99 'select count(*) from sys.v_$session where username is not null',
101 'max connections' => array( 'SESS',
102 "select value from v\$parameter where name='sessions'",
105 'Memory Utilization',
106 'data cache utilization ratio' => array('RATIOU',
107 "select round((1-bytes/sgasize)*100, 2)
108 from (select sum(bytes) sgasize from sys.v_\$sgastat) s, sys.v_\$sgastat f
109 where name = 'free memory' and pool = 'shared pool'",
110 'Percentage of data cache actually in use - should be over 85%'),
112 'shared pool utilization ratio' => array('RATIOU',
113 'select round((sga.bytes/case when p.value=0 then sga.bytes else to_number(p.value) end)*100,2)
114 from v$sgastat sga, v$parameter p
115 where sga.name = \'free memory\' and sga.pool = \'shared pool\'
116 and p.name = \'shared_pool_size\'',
117 'Percentage of shared pool actually used - too low is bad, too high is worse'),
119 'large pool utilization ratio' => array('RATIOU',
120 "select round((1-bytes/sgasize)*100, 2)
121 from (select sum(bytes) sgasize from sys.v_\$sgastat) s, sys.v_\$sgastat f
122 where name = 'free memory' and pool = 'large pool'",
123 'Percentage of large_pool actually in use - too low is bad, too high is worse'),
124 'sort buffer size' => array('CACHE',
125 "select value from v\$parameter where name='sort_area_size'",
126 'max in-mem sort_area_size (per query), uses memory in pga' ),
128 'pga usage at peak' => array('RATIOU',
129 '=PGA','Mb utilization at peak transactions (requires Oracle 9i+)'),
131 'rollback segments' => array('ROLLBACK',
132 "select count(*) from sys.v_\$rollstat",
135 'peak transactions' => array('ROLLBACK',
136 "select max_utilization tx_hwm
137 from sys.v_\$resource_limit
138 where resource_name = 'transactions'",
139 'Taken from high-water-mark'),
140 'max transactions' => array('ROLLBACK',
141 "select value from v\$parameter where name = 'transactions'",
142 'max transactions / rollback segments < 3.5 (or transactions_per_rollback_segment)'),
144 'cursor sharing' => array('CURSOR',
145 "select value from v\$parameter where name = 'cursor_sharing'",
146 'Cursor reuse strategy. Recommended is FORCE (8i+) or SIMILAR (9i+). See <a href=http://www.praetoriate.com/oracle_tips_cursor_sharing.htm>cursor_sharing</a>.'),
148 'cursor reuse' => array('CURSOR',
149 "select count(*) from (select sql_text_wo_constants, count(*)
151 group by sql_text_wo_constants
152 having count(*) > 100)",'These are sql statements that should be using bind variables'),*/
153 'index cache cost' => array('COST',
154 "select value from v\$parameter where name = 'optimizer_index_caching'",
156 'random page cost' => array('COST',
157 "select value from v\$parameter where name = 'optimizer_index_cost_adj'",
165 function perf_oci8(&$conn)
167 $savelog = $conn->LogSQL(false);
168 $this->version
= $conn->ServerInfo();
169 $conn->LogSQL($savelog);
170 $this->conn
=& $conn;
173 function WarnPageCost($val)
175 if ($val == 100) $s = '<font color=red><b>Too High</b>. </font>';
178 return $s.'Recommended is 20-50 for TP, and 50 for data warehouses. Default is 100. See <a href=http://www.dba-oracle.com/oracle_tips_cost_adj.htm>optimizer_index_cost_adj</a>. ';
181 function WarnIndexCost($val)
183 if ($val == 0) $s = '<font color=red><b>Too Low</b>. </font>';
186 return $s.'Percentage of indexed data blocks expected in the cache.
187 Recommended is 20 (fast disk array) to 50 (slower hard disks). Default is 0.
188 See <a href=http://www.dba-oracle.com/oracle_tips_cbo_part1.htm>optimizer_index_caching</a>.';
193 if ($this->version
['version'] < 9) return 'Oracle 9i or later required';
195 $rs = $this->conn
->Execute("select a.mb,a.targ as pga_size_pct,a.pct from
196 (select round(pga_target_for_estimate/1024.0/1024.0,0) Mb,
197 pga_target_factor targ,estd_pga_cache_hit_percentage pct,rownum as r
198 from v\$pga_target_advice) a left join
199 (select round(pga_target_for_estimate/1024.0/1024.0,0) Mb,
200 pga_target_factor targ,estd_pga_cache_hit_percentage pct,rownum as r
201 from v\$pga_target_advice) b on
204 if (!$rs) return "Only in 9i or later";
206 if ($rs->EOF
) return "PGA could be too big";
208 return reset($rs->fields
);
211 function Explain($sql,$partial=false)
213 $savelog = $this->conn
->LogSQL(false);
214 $rs =& $this->conn
->SelectLimit("select ID FROM PLAN_TABLE");
216 echo "<p><b>Missing PLAN_TABLE</b></p>
218 CREATE TABLE PLAN_TABLE (
219 STATEMENT_ID VARCHAR2(30),
221 REMARKS VARCHAR2(80),
222 OPERATION VARCHAR2(30),
223 OPTIONS VARCHAR2(30),
224 OBJECT_NODE VARCHAR2(128),
225 OBJECT_OWNER VARCHAR2(30),
226 OBJECT_NAME VARCHAR2(30),
227 OBJECT_INSTANCE NUMBER(38),
228 OBJECT_TYPE VARCHAR2(30),
229 OPTIMIZER VARCHAR2(255),
230 SEARCH_COLUMNS NUMBER,
232 PARENT_ID NUMBER(38),
235 CARDINALITY NUMBER(38),
237 OTHER_TAG VARCHAR2(255),
238 PARTITION_START VARCHAR2(255),
239 PARTITION_STOP VARCHAR2(255),
240 PARTITION_ID NUMBER(38),
242 DISTRIBUTION VARCHAR2(30)
249 // $this->conn->debug=1;
252 $sqlq = $this->conn
->qstr($sql.'%');
253 $arr = $this->conn
->GetArray("select distinct distinct sql1 from adodb_logsql where sql1 like $sqlq");
255 foreach($arr as $row) {
257 if (crc32($sql) == $partial) break;
262 $s = "<p><b>Explain</b>: ".htmlspecialchars($sql)."</p>";
264 $this->conn
->BeginTrans();
265 $id = "ADODB ".microtime();
267 $rs =& $this->conn
->Execute("EXPLAIN PLAN SET STATEMENT_ID='$id' FOR $sql");
268 $m = $this->conn
->ErrorMsg();
270 $this->conn
->RollbackTrans();
271 $this->conn
->LogSQL($savelog);
275 $rs =& $this->conn
->Execute("
277 '<pre>'||lpad('--', (level-1)*2,'-') || trim(operation) || ' ' || trim(options)||'</pre>' as Operation,
278 object_name,COST,CARDINALITY,bytes
280 START WITH id = 0 and STATEMENT_ID='$id'
281 CONNECT BY prior id=parent_id and statement_id='$id'");
283 $s .= rs2html($rs,false,false,false,false);
284 $this->conn
->RollbackTrans();
285 $this->conn
->LogSQL($savelog);
286 $s .= $this->Tracer($sql,$partial);
291 function CheckMemory()
293 if ($this->version
['version'] < 9) return 'Oracle 9i or later required';
295 $rs =& $this->conn
->Execute("
296 select a.size_for_estimate as cache_mb_estimate,
297 case when a.size_factor=1 then
299 when a.estd_physical_read_factor-b.estd_physical_read_factor > 0 and a.estd_physical_read_factor<1 then
301 else ' ' end as currsize,
302 a.estd_physical_read_factor-b.estd_physical_read_factor as best_when_0
303 from (select size_for_estimate,size_factor,estd_physical_read_factor,rownum r from v\$db_cache_advice) a ,
304 (select size_for_estimate,size_factor,estd_physical_read_factor,rownum r from v\$db_cache_advice) b where a.r = b.r-1");
305 if (!$rs) return false;
308 The v$db_cache_advice utility show the marginal changes in physical data block reads for different sizes of db_cache_size
310 $s = "<h3>Data Cache Estimate</h3>";
312 $s .= "<p>Cache that is 50% of current size is still too big</p>";
314 $s .= "Ideal size of Data Cache is when \"best_when_0\" changes from a positive number and becomes zero.";
315 $s .= rs2html($rs,false,false,false,false);
321 Generate html for suspicious/expensive sql
323 function tohtml(&$rs,$type)
325 $o1 = $rs->FetchField(0);
326 $o2 = $rs->FetchField(1);
327 $o3 = $rs->FetchField(2);
328 if ($rs->EOF
) return '<p>None found</p>';
331 $s = "\n\n<table border=1 bgcolor=white><tr><td><b>".$o1->name
.'</b></td><td><b>'.$o2->name
.'</b></td><td><b>'.$o3->name
.'</b></td></tr>';
333 if ($check != $rs->fields
[0].'::'.$rs->fields
[1]) {
335 $carr = explode('::',$check);
336 $prefix = "<a href=\"?$type=1&sql=".rawurlencode($sql).'&x#explain">';
338 if (strlen($prefix)>2000) {
343 $s .= "\n<tr><td align=right>".$carr[0].'</td><td align=right>'.$carr[1].'</td><td>'.$prefix.$sql.$suffix.'</td></tr>';
345 $sql = $rs->fields
[2];
346 $check = $rs->fields
[0].'::'.$rs->fields
[1];
348 $sql .= $rs->fields
[2];
349 if (substr($sql,strlen($sql)-1) == "\0") $sql = substr($sql,0,strlen($sql)-1);
354 $carr = explode('::',$check);
355 $prefix = "<a target=".rand()." href=\"?&hidem=1&$type=1&sql=".rawurlencode($sql).'&x#explain">';
357 if (strlen($prefix)>2000) {
361 $s .= "\n<tr><td align=right>".$carr[0].'</td><td align=right>'.$carr[1].'</td><td>'.$prefix.$sql.$suffix.'</td></tr>';
363 return $s."</table>\n\n";
366 // code thanks to Ixora.
367 // http://www.ixora.com.au/scripts/query_opt.htm
368 // requires oracle 8.1.7 or later
369 function SuspiciousSQL($numsql=10)
373 substr(to_char(s.pct, '99.00'), 2) || '%' load,
374 s.executions executes,
383 rank() over (order by buffer_gets desc) ranking
390 100 * ratio_to_report(buffer_gets) over () pct
394 command_type != 47 and module != 'T.O.A.D.'
397 buffer_gets > 50 * executions
401 s.ranking <= $numsql and
402 p.address = s.address
404 1 desc, s.address, p.piece";
406 global $ADODB_CACHE_MODE;
407 if (isset($_GET['expsixora']) && isset($_GET['sql'])) {
408 $partial = empty($_GET['part']);
409 echo "<a name=explain></a>".$this->Explain($_GET['sql'],$partial)."\n";
412 if (isset($_GET['sql'])) return $this->_SuspiciousSQL($numsql);
415 $s .= $this->_SuspiciousSQL($numsql);
418 $save = $ADODB_CACHE_MODE;
419 $ADODB_CACHE_MODE = ADODB_FETCH_NUM
;
420 if ($this->conn
->fetchMode
!== false) $savem = $this->conn
->SetFetchMode(false);
422 $savelog = $this->conn
->LogSQL(false);
423 $rs =& $this->conn
->SelectLimit($sql);
424 $this->conn
->LogSQL($savelog);
426 if (isset($savem)) $this->conn
->SetFetchMode($savem);
427 $ADODB_CACHE_MODE = $save;
429 $s .= "\n<h3>Ixora Suspicious SQL</h3>";
430 $s .= $this->tohtml($rs,'expsixora');
436 // code thanks to Ixora.
437 // http://www.ixora.com.au/scripts/query_opt.htm
438 // requires oracle 8.1.7 or later
439 function ExpensiveSQL($numsql = 10)
443 substr(to_char(s.pct, '99.00'), 2) || '%' load,
444 s.executions executes,
453 rank() over (order by disk_reads desc) ranking
460 100 * ratio_to_report(disk_reads) over () pct
464 command_type != 47 and module != 'T.O.A.D.'
467 disk_reads > 50 * executions
471 s.ranking <= $numsql and
472 p.address = s.address
474 1 desc, s.address, p.piece
476 global $ADODB_CACHE_MODE;
477 if (isset($_GET['expeixora']) && isset($_GET['sql'])) {
478 $partial = empty($_GET['part']);
479 echo "<a name=explain></a>".$this->Explain($_GET['sql'],$partial)."\n";
481 if (isset($_GET['sql'])) {
482 $var = $this->_ExpensiveSQL($numsql);
487 $s .= $this->_ExpensiveSQL($numsql);
489 $save = $ADODB_CACHE_MODE;
490 $ADODB_CACHE_MODE = ADODB_FETCH_NUM
;
491 if ($this->conn
->fetchMode
!== false) $savem = $this->conn
->SetFetchMode(false);
493 $savelog = $this->conn
->LogSQL(false);
494 $rs =& $this->conn
->Execute($sql);
495 $this->conn
->LogSQL($savelog);
497 if (isset($savem)) $this->conn
->SetFetchMode($savem);
498 $ADODB_CACHE_MODE = $save;
501 $s .= "\n<h3>Ixora Expensive SQL</h3>";
502 $s .= $this->tohtml($rs,'expeixora');