new approach to logging database access and upgraded adodb
[openemr.git] / library / adodb / perf / perf-oci8.inc.php
blobe0c55cc24e376d7198a2bba044937f65818b4655
1 <?php
2 /*
3 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). 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();
19 class perf_oci8 extends ADODB_perf{
21 var $noShowIxora = 15; // if the sql for suspicious sql is taking too long, then disable ixora
23 var $tablesSQL = "select segment_name as \"tablename\", sum(bytes)/1024 as \"size_in_k\",tablespace_name as \"tablespace\",count(*) \"extents\" from sys.user_extents
24 group by segment_name,tablespace_name";
26 var $version;
28 var $createTableSQL = "CREATE TABLE adodb_logsql (
29 created date NOT NULL,
30 sql0 varchar(250) NOT NULL,
31 sql1 varchar(4000) NOT NULL,
32 params varchar(4000),
33 tracer varchar(4000),
34 timer decimal(16,6) NOT NULL
35 )";
37 var $settings = array(
38 'Ratios',
39 'data cache hit ratio' => array('RATIOH',
40 "select round((1-(phy.value / (cur.value + con.value)))*100,2)
41 from v\$sysstat cur, v\$sysstat con, v\$sysstat phy
42 where cur.name = 'db block gets' and
43 con.name = 'consistent gets' and
44 phy.name = 'physical reads'",
45 '=WarnCacheRatio'),
47 'sql cache hit ratio' => array( 'RATIOH',
48 'select round(100*(sum(pins)-sum(reloads))/sum(pins),2) from v$librarycache',
49 'increase <i>shared_pool_size</i> if too ratio low'),
51 'datadict cache hit ratio' => array('RATIOH',
52 "select
53 round((1 - (sum(getmisses) / (sum(gets) +
54 sum(getmisses))))*100,2)
55 from v\$rowcache",
56 'increase <i>shared_pool_size</i> if too ratio low'),
58 'memory sort ratio' => array('RATIOH',
59 "SELECT ROUND((100 * b.VALUE) /DECODE ((a.VALUE + b.VALUE),
60 0,1,(a.VALUE + b.VALUE)),2)
61 FROM v\$sysstat a,
62 v\$sysstat b
63 WHERE a.name = 'sorts (disk)'
64 AND b.name = 'sorts (memory)'",
65 "% of memory sorts compared to disk sorts - should be over 95%"),
67 'IO',
68 'data reads' => array('IO',
69 "select value from v\$sysstat where name='physical reads'"),
71 'data writes' => array('IO',
72 "select value from v\$sysstat where name='physical writes'"),
74 'Data Cache',
76 'data cache buffers' => array( 'DATAC',
77 "select a.value/b.value from v\$parameter a, v\$parameter b
78 where a.name = 'db_cache_size' and b.name= 'db_block_size'",
79 'Number of cache buffers. Tune <i>db_cache_size</i> if the <i>data cache hit ratio</i> is too low.'),
80 'data cache blocksize' => array('DATAC',
81 "select value from v\$parameter where name='db_block_size'",
82 '' ),
84 'Memory Pools',
85 'Mem Max Target (11g+)' => array( 'DATAC',
86 "select value from v\$parameter where name = 'memory_max_target'",
87 'The memory_max_size is the maximum value to which memory_target can be set.' ),
88 'Memory target (11g+)' => array( 'DATAC',
89 "select value from v\$parameter where name = 'memory_target'",
90 'If memory_target is defined then SGA and PGA targets are consolidated into one memory_target.' ),
91 'SGA Max Size' => array( 'DATAC',
92 "select nvl(value,0)/1024.0/1024 || 'M' from v\$parameter where name = 'sga_max_size'",
93 'The sga_max_size is the maximum value to which sga_target can be set.' ),
94 'SGA target' => array( 'DATAC',
95 "select nvl(value,0)/1024.0/1024 || 'M' from v\$parameter where name = 'sga_target'",
96 'If sga_target is defined then data cache, shared, java and large pool size can be 0. This is because all these pools are consolidated into one sga_target.' ),
97 'PGA aggr target' => array( 'DATAC',
98 "select nvl(value,0)/1024.0/1024 || 'M' from v\$parameter where name = 'pga_aggregate_target'",
99 'If pga_aggregate_target is defined then this is the maximum memory that can be allocated for cursor operations such as sorts, group by, joins, merges. When in doubt, set it to 20% of sga_target.' ),
100 'data cache size' => array('DATAC',
101 "select value from v\$parameter where name = 'db_cache_size'",
102 'db_cache_size' ),
103 'shared pool size' => array('DATAC',
104 "select value from v\$parameter where name = 'shared_pool_size'",
105 'shared_pool_size, which holds shared sql, stored procedures, dict cache and similar shared structs' ),
106 'java pool size' => array('DATAJ',
107 "select value from v\$parameter where name = 'java_pool_size'",
108 'java_pool_size' ),
109 'large pool buffer size' => array('CACHE',
110 "select value from v\$parameter where name='large_pool_size'",
111 '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) ' ),
113 'dynamic memory usage' => array('CACHE', "select '-' from dual", '=DynMemoryUsage'),
115 'Connections',
116 'current connections' => array('SESS',
117 'select count(*) from sys.v_$session where username is not null',
118 ''),
119 'max connections' => array( 'SESS',
120 "select value from v\$parameter where name='sessions'",
121 ''),
123 'Memory Utilization',
124 'data cache utilization ratio' => array('RATIOU',
125 "select round((1-bytes/sgasize)*100, 2)
126 from (select sum(bytes) sgasize from sys.v_\$sgastat) s, sys.v_\$sgastat f
127 where name = 'free memory' and pool = 'shared pool'",
128 'Percentage of data cache actually in use - should be over 85%'),
130 'shared pool utilization ratio' => array('RATIOU',
131 'select round((sga.bytes/case when p.value=0 then sga.bytes else to_number(p.value) end)*100,2)
132 from v$sgastat sga, v$parameter p
133 where sga.name = \'free memory\' and sga.pool = \'shared pool\'
134 and p.name = \'shared_pool_size\'',
135 'Percentage of shared pool actually used - too low is bad, too high is worse'),
137 'large pool utilization ratio' => array('RATIOU',
138 "select round((1-bytes/sgasize)*100, 2)
139 from (select sum(bytes) sgasize from sys.v_\$sgastat) s, sys.v_\$sgastat f
140 where name = 'free memory' and pool = 'large pool'",
141 'Percentage of large_pool actually in use - too low is bad, too high is worse'),
142 'sort buffer size' => array('CACHE',
143 "select value from v\$parameter where name='sort_area_size'",
144 'max in-mem sort_area_size (per query), uses memory in pga' ),
146 /*'pga usage at peak' => array('RATIOU',
147 '=PGA','Mb utilization at peak transactions (requires Oracle 9i+)'),*/
148 'Transactions',
149 'rollback segments' => array('ROLLBACK',
150 "select count(*) from sys.v_\$rollstat",
151 ''),
153 'peak transactions' => array('ROLLBACK',
154 "select max_utilization tx_hwm
155 from sys.v_\$resource_limit
156 where resource_name = 'transactions'",
157 'Taken from high-water-mark'),
158 'max transactions' => array('ROLLBACK',
159 "select value from v\$parameter where name = 'transactions'",
160 'max transactions / rollback segments < 3.5 (or transactions_per_rollback_segment)'),
161 'Parameters',
162 'cursor sharing' => array('CURSOR',
163 "select value from v\$parameter where name = 'cursor_sharing'",
164 '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>.'),
166 'cursor reuse' => array('CURSOR',
167 "select count(*) from (select sql_text_wo_constants, count(*)
168 from t1
169 group by sql_text_wo_constants
170 having count(*) > 100)",'These are sql statements that should be using bind variables'),*/
171 'index cache cost' => array('COST',
172 "select value from v\$parameter where name = 'optimizer_index_caching'",
173 '=WarnIndexCost'),
174 'random page cost' => array('COST',
175 "select value from v\$parameter where name = 'optimizer_index_cost_adj'",
176 '=WarnPageCost'),
177 'Waits',
178 'Recent wait events' => array('WAITS','select \'Top 5 events\' from dual','=TopRecentWaits'),
179 // 'Historical wait SQL' => array('WAITS','select \'Last 2 days\' from dual','=TopHistoricalWaits'), -- requires AWR license
180 'Backup',
181 'Achivelog Mode' => array('BACKUP', 'select log_mode from v$database', '=LogMode'),
183 'DBID' => array('BACKUP','select dbid from v$database','Primary key of database, used for recovery with an RMAN Recovery Catalog'),
184 'Archive Log Dest' => array('BACKUP', "SELECT NVL(v1.value,v2.value)
185 FROM v\$parameter v1, v\$parameter v2 WHERE v1.name='log_archive_dest' AND v2.name='log_archive_dest_10'", ''),
187 'Flashback Area' => array('BACKUP', "select nvl(value,'Flashback Area not used') from v\$parameter where name=lower('DB_RECOVERY_FILE_DEST')", 'Flashback area is a folder where all backup data and logs can be stored and managed by Oracle. If Error: message displayed, then it is not in use.'),
189 'Flashback Usage' => array('BACKUP', "select nvl('-','Flashback Area not used') from v\$parameter where name=lower('DB_RECOVERY_FILE_DEST')", '=FlashUsage', 'Flashback area usage.'),
191 'Control File Keep Time' => array('BACKUP', "select value from v\$parameter where name='control_file_record_keep_time'",'No of days to keep RMAN info in control file. Recommended set to x2 or x3 times the frequency of your full backup.'),
192 'Recent RMAN Jobs' => array('BACKUP', "select '-' from dual", "=RMAN"),
194 // 'Control File Keep Time' => array('BACKUP', "select value from v\$parameter where name='control_file_record_keep_time'",'No of days to keep RMAN info in control file. I recommend it be set to x2 or x3 times the frequency of your full backup.'),
196 false
201 function perf_oci8(&$conn)
203 global $gSQLBlockRows;
205 $gSQLBlockRows = 1000;
206 $savelog = $conn->LogSQL(false);
207 $this->version = $conn->ServerInfo();
208 $conn->LogSQL($savelog);
209 $this->conn = $conn;
212 function LogMode()
214 $mode = $this->conn->GetOne("select log_mode from v\$database");
216 if ($mode == 'ARCHIVELOG') return 'To turn off archivelog:<br>
217 <pre><font size=-2>
218 SQLPLUS> connect sys as sysdba;
219 SQLPLUS> shutdown immediate;
221 SQLPLUS> startup mount exclusive;
222 SQLPLUS> alter database noarchivelog;
223 SQLPLUS> alter database open;
224 </font></pre>';
226 return 'To turn on archivelog:<br>
227 <pre><font size=-2>
228 SQLPLUS> connect sys as sysdba;
229 SQLPLUS> shutdown immediate;
231 SQLPLUS> startup mount exclusive;
232 SQLPLUS> alter database archivelog;
233 SQLPLUS> archive log start;
234 SQLPLUS> alter database open;
235 </font></pre>';
238 function TopRecentWaits()
241 $rs = $this->conn->Execute("select * from (
242 select event, round(100*time_waited/(select sum(time_waited) from v\$system_event where wait_class <> 'Idle'),1) \"% Wait\",
243 total_waits,time_waited, average_wait,wait_class from v\$system_event where wait_class <> 'Idle' order by 2 desc
244 ) where rownum <=5");
246 $ret = rs2html($rs,false,false,false,false);
247 return "&nbsp;<p>".$ret."&nbsp;</p>";
251 function TopHistoricalWaits()
253 $days = 2;
255 $rs = $this->conn->Execute("select * from ( SELECT
256 b.wait_class,B.NAME,
257 round(sum(wait_time+TIME_WAITED)/1000000) waitsecs,
258 parsing_schema_name,
259 C.SQL_TEXT, a.sql_id
260 FROM V\$ACTIVE_SESSION_HISTORY A
261 join V\$EVENT_NAME B on A.EVENT# = B.EVENT#
262 join V\$SQLAREA C on A.SQL_ID = C.SQL_ID
263 WHERE A.SAMPLE_TIME BETWEEN sysdate-$days and sysdate
264 and parsing_schema_name not in ('SYS','SYSMAN','DBSNMP','SYSTEM')
265 GROUP BY b.wait_class,parsing_schema_name,C.SQL_TEXT, B.NAME,A.sql_id
266 order by 3 desc) where rownum <=10");
268 $ret = rs2html($rs,false,false,false,false);
269 return "&nbsp;<p>".$ret."&nbsp;</p>";
273 function RMAN()
275 $rs = $this->conn->Execute("select * from (select start_time, end_time, operation, status, mbytes_processed, output_device_type
276 from V\$RMAN_STATUS order by start_time desc) where rownum <=10");
278 $ret = rs2html($rs,false,false,false,false);
279 return "&nbsp;<p>".$ret."&nbsp;</p>";
282 function DynMemoryUsage()
284 if (@$this->version['version'] >= 11) {
285 $rs = $this->conn->Execute("select component, current_size/1024./1024 as \"CurrSize (M)\" from V\$MEMORY_DYNAMIC_COMPONENTS");
287 } else
288 $rs = $this->conn->Execute("select name, round(bytes/1024./1024,2) as \"CurrSize (M)\" from V\$sgainfo");
291 $ret = rs2html($rs,false,false,false,false);
292 return "&nbsp;<p>".$ret."&nbsp;</p>";
295 function FlashUsage()
297 $rs = $this->conn->Execute("select * from V\$FLASH_RECOVERY_AREA_USAGE");
298 $ret = rs2html($rs,false,false,false,false);
299 return "&nbsp;<p>".$ret."&nbsp;</p>";
302 function WarnPageCost($val)
304 if ($val == 100 && $this->version['version'] < 10) $s = '<font color=red><b>Too High</b>. </font>';
305 else $s = '';
307 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>. ';
310 function WarnIndexCost($val)
312 if ($val == 0 && $this->version['version'] < 10) $s = '<font color=red><b>Too Low</b>. </font>';
313 else $s = '';
315 return $s.'Percentage of indexed data blocks expected in the cache.
316 Recommended is 20 (fast disk array) to 30 (slower hard disks). Default is 0.
317 See <a href=http://www.dba-oracle.com/oracle_tips_cbo_part1.htm>optimizer_index_caching</a>.';
320 function PGA()
323 //if ($this->version['version'] < 9) return 'Oracle 9i or later required';
325 function PGA_Advice()
327 $t = "<h3>PGA Advice Estimate</h3>";
328 if ($this->version['version'] < 9) return $t.'Oracle 9i or later required';
330 $rs = $this->conn->Execute('select a.MB,
331 case when a.targ = 1 then \'<<= Current \'
332 when a.targ < 1 or a.pct <= b.pct then null
333 else
334 \'- BETTER than Current by \'||round(a.pct/b.pct*100-100,2)||\'%\' end as "Percent Improved",
335 a.targ as "PGA Size Factor",a.pct "% Perf"
336 from
337 (select round(pga_target_for_estimate/1024.0/1024.0,0) MB,
338 pga_target_factor targ,estd_pga_cache_hit_percentage pct,rownum as r
339 from v$pga_target_advice) a left join
340 (select round(pga_target_for_estimate/1024.0/1024.0,0) MB,
341 pga_target_factor targ,estd_pga_cache_hit_percentage pct,rownum as r
342 from v$pga_target_advice) b on
343 a.r = b.r+1 where
344 b.pct < 100');
345 if (!$rs) return $t."Only in 9i or later";
346 // $rs->Close();
347 if ($rs->EOF) return $t."PGA could be too big";
349 return $t.rs2html($rs,false,false,true,false);
352 function Explain($sql,$partial=false)
354 $savelog = $this->conn->LogSQL(false);
355 $rs = $this->conn->SelectLimit("select ID FROM PLAN_TABLE");
356 if (!$rs) {
357 echo "<p><b>Missing PLAN_TABLE</b></p>
358 <pre>
359 CREATE TABLE PLAN_TABLE (
360 STATEMENT_ID VARCHAR2(30),
361 TIMESTAMP DATE,
362 REMARKS VARCHAR2(80),
363 OPERATION VARCHAR2(30),
364 OPTIONS VARCHAR2(30),
365 OBJECT_NODE VARCHAR2(128),
366 OBJECT_OWNER VARCHAR2(30),
367 OBJECT_NAME VARCHAR2(30),
368 OBJECT_INSTANCE NUMBER(38),
369 OBJECT_TYPE VARCHAR2(30),
370 OPTIMIZER VARCHAR2(255),
371 SEARCH_COLUMNS NUMBER,
372 ID NUMBER(38),
373 PARENT_ID NUMBER(38),
374 POSITION NUMBER(38),
375 COST NUMBER(38),
376 CARDINALITY NUMBER(38),
377 BYTES NUMBER(38),
378 OTHER_TAG VARCHAR2(255),
379 PARTITION_START VARCHAR2(255),
380 PARTITION_STOP VARCHAR2(255),
381 PARTITION_ID NUMBER(38),
382 OTHER LONG,
383 DISTRIBUTION VARCHAR2(30)
385 </pre>";
386 return false;
389 $rs->Close();
390 // $this->conn->debug=1;
392 if ($partial) {
393 $sqlq = $this->conn->qstr($sql.'%');
394 $arr = $this->conn->GetArray("select distinct sql1 from adodb_logsql where sql1 like $sqlq");
395 if ($arr) {
396 foreach($arr as $row) {
397 $sql = reset($row);
398 if (crc32($sql) == $partial) break;
403 $s = "<p><b>Explain</b>: ".htmlspecialchars($sql)."</p>";
405 $this->conn->BeginTrans();
406 $id = "ADODB ".microtime();
408 $rs = $this->conn->Execute("EXPLAIN PLAN SET STATEMENT_ID='$id' FOR $sql");
409 $m = $this->conn->ErrorMsg();
410 if ($m) {
411 $this->conn->RollbackTrans();
412 $this->conn->LogSQL($savelog);
413 $s .= "<p>$m</p>";
414 return $s;
416 $rs = $this->conn->Execute("
417 select
418 '<pre>'||lpad('--', (level-1)*2,'-') || trim(operation) || ' ' || trim(options)||'</pre>' as Operation,
419 object_name,COST,CARDINALITY,bytes
420 FROM plan_table
421 START WITH id = 0 and STATEMENT_ID='$id'
422 CONNECT BY prior id=parent_id and statement_id='$id'");
424 $s .= rs2html($rs,false,false,false,false);
425 $this->conn->RollbackTrans();
426 $this->conn->LogSQL($savelog);
427 $s .= $this->Tracer($sql,$partial);
428 return $s;
432 function CheckMemory()
434 if ($this->version['version'] < 9) return 'Oracle 9i or later required';
436 $rs = $this->conn->Execute("
437 select a.name Buffer_Pool, b.size_for_estimate as cache_mb_estimate,
438 case when b.size_factor=1 then
439 '&lt;&lt;= Current'
440 when a.estd_physical_read_factor-b.estd_physical_read_factor > 0.001 and b.estd_physical_read_factor<1 then
441 '- BETTER than current by ' || round((1-b.estd_physical_read_factor)/b.estd_physical_read_factor*100,2) || '%'
442 else ' ' end as RATING,
443 b.estd_physical_read_factor \"Phys. Reads Factor\",
444 round((a.estd_physical_read_factor-b.estd_physical_read_factor)/b.estd_physical_read_factor*100,2) as \"% Improve\"
445 from (select size_for_estimate,size_factor,estd_physical_read_factor,rownum r,name from v\$db_cache_advice order by name,1) a ,
446 (select size_for_estimate,size_factor,estd_physical_read_factor,rownum r,name from v\$db_cache_advice order by name,1) b
447 where a.r = b.r-1 and a.name = b.name
449 if (!$rs) return false;
452 The v$db_cache_advice utility show the marginal changes in physical data block reads for different sizes of db_cache_size
454 $s = "<h3>Data Cache Advice Estimate</h3>";
455 if ($rs->EOF) {
456 $s .= "<p>Cache that is 50% of current size is still too big</p>";
457 } else {
458 $s .= "Ideal size of Data Cache is when %BETTER gets close to zero.";
459 $s .= rs2html($rs,false,false,false,false);
461 return $s.$this->PGA_Advice();
465 Generate html for suspicious/expensive sql
467 function tohtml(&$rs,$type)
469 $o1 = $rs->FetchField(0);
470 $o2 = $rs->FetchField(1);
471 $o3 = $rs->FetchField(2);
472 if ($rs->EOF) return '<p>None found</p>';
473 $check = '';
474 $sql = '';
475 $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>';
476 while (!$rs->EOF) {
477 if ($check != $rs->fields[0].'::'.$rs->fields[1]) {
478 if ($check) {
479 $carr = explode('::',$check);
480 $prefix = "<a href=\"?$type=1&sql=".rawurlencode($sql).'&x#explain">';
481 $suffix = '</a>';
482 if (strlen($prefix)>2000) {
483 $prefix = '';
484 $suffix = '';
487 $s .= "\n<tr><td align=right>".$carr[0].'</td><td align=right>'.$carr[1].'</td><td>'.$prefix.$sql.$suffix.'</td></tr>';
489 $sql = $rs->fields[2];
490 $check = $rs->fields[0].'::'.$rs->fields[1];
491 } else
492 $sql .= $rs->fields[2];
493 if (substr($sql,strlen($sql)-1) == "\0") $sql = substr($sql,0,strlen($sql)-1);
494 $rs->MoveNext();
496 $rs->Close();
498 $carr = explode('::',$check);
499 $prefix = "<a target=".rand()." href=\"?&hidem=1&$type=1&sql=".rawurlencode($sql).'&x#explain">';
500 $suffix = '</a>';
501 if (strlen($prefix)>2000) {
502 $prefix = '';
503 $suffix = '';
505 $s .= "\n<tr><td align=right>".$carr[0].'</td><td align=right>'.$carr[1].'</td><td>'.$prefix.$sql.$suffix.'</td></tr>';
507 return $s."</table>\n\n";
510 // code thanks to Ixora.
511 // http://www.ixora.com.au/scripts/query_opt.htm
512 // requires oracle 8.1.7 or later
513 function SuspiciousSQL($numsql=10)
515 $sql = "
516 select
517 substr(to_char(s.pct, '99.00'), 2) || '%' load,
518 s.executions executes,
519 p.sql_text
520 from
522 select
523 address,
524 buffer_gets,
525 executions,
526 pct,
527 rank() over (order by buffer_gets desc) ranking
528 from
530 select
531 address,
532 buffer_gets,
533 executions,
534 100 * ratio_to_report(buffer_gets) over () pct
535 from
536 sys.v_\$sql
537 where
538 command_type != 47 and module != 'T.O.A.D.'
540 where
541 buffer_gets > 50 * executions
542 ) s,
543 sys.v_\$sqltext p
544 where
545 s.ranking <= $numsql and
546 p.address = s.address
547 order by
548 1 desc, s.address, p.piece";
550 global $ADODB_CACHE_MODE;
551 if (isset($_GET['expsixora']) && isset($_GET['sql'])) {
552 $partial = empty($_GET['part']);
553 echo "<a name=explain></a>".$this->Explain($_GET['sql'],$partial)."\n";
556 if (isset($_GET['sql'])) return $this->_SuspiciousSQL($numsql);
558 $s = '';
559 $timer = time();
560 $s .= $this->_SuspiciousSQL($numsql);
561 $timer = time() - $timer;
563 if ($timer > $this->noShowIxora) return $s;
564 $s .= '<p>';
566 $save = $ADODB_CACHE_MODE;
567 $ADODB_CACHE_MODE = ADODB_FETCH_NUM;
568 if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
570 $savelog = $this->conn->LogSQL(false);
571 $rs = $this->conn->SelectLimit($sql);
572 $this->conn->LogSQL($savelog);
574 if (isset($savem)) $this->conn->SetFetchMode($savem);
575 $ADODB_CACHE_MODE = $save;
576 if ($rs) {
577 $s .= "\n<h3>Ixora Suspicious SQL</h3>";
578 $s .= $this->tohtml($rs,'expsixora');
581 return $s;
584 // code thanks to Ixora.
585 // http://www.ixora.com.au/scripts/query_opt.htm
586 // requires oracle 8.1.7 or later
587 function ExpensiveSQL($numsql = 10)
589 $sql = "
590 select
591 substr(to_char(s.pct, '99.00'), 2) || '%' load,
592 s.executions executes,
593 p.sql_text
594 from
596 select
597 address,
598 disk_reads,
599 executions,
600 pct,
601 rank() over (order by disk_reads desc) ranking
602 from
604 select
605 address,
606 disk_reads,
607 executions,
608 100 * ratio_to_report(disk_reads) over () pct
609 from
610 sys.v_\$sql
611 where
612 command_type != 47 and module != 'T.O.A.D.'
614 where
615 disk_reads > 50 * executions
616 ) s,
617 sys.v_\$sqltext p
618 where
619 s.ranking <= $numsql and
620 p.address = s.address
621 order by
622 1 desc, s.address, p.piece
624 global $ADODB_CACHE_MODE;
625 if (isset($_GET['expeixora']) && isset($_GET['sql'])) {
626 $partial = empty($_GET['part']);
627 echo "<a name=explain></a>".$this->Explain($_GET['sql'],$partial)."\n";
629 if (isset($_GET['sql'])) {
630 $var = $this->_ExpensiveSQL($numsql);
631 return $var;
634 $s = '';
635 $timer = time();
636 $s .= $this->_ExpensiveSQL($numsql);
637 $timer = time() - $timer;
638 if ($timer > $this->noShowIxora) return $s;
640 $s .= '<p>';
641 $save = $ADODB_CACHE_MODE;
642 $ADODB_CACHE_MODE = ADODB_FETCH_NUM;
643 if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
645 $savelog = $this->conn->LogSQL(false);
646 $rs = $this->conn->Execute($sql);
647 $this->conn->LogSQL($savelog);
649 if (isset($savem)) $this->conn->SetFetchMode($savem);
650 $ADODB_CACHE_MODE = $save;
652 if ($rs) {
653 $s .= "\n<h3>Ixora Expensive SQL</h3>";
654 $s .= $this->tohtml($rs,'expeixora');
657 return $s;
660 function clearsql()
662 $perf_table = adodb_perf::table();
663 // using the naive "delete from $perf_table where created<".$this->conn->sysTimeStamp will cause the table to lock, possibly
664 // for a long time
665 $sql =
666 "DECLARE cnt pls_integer;
667 BEGIN
668 cnt := 0;
669 FOR rec IN (SELECT ROWID AS rr FROM $perf_table WHERE created<SYSDATE)
670 LOOP
671 cnt := cnt + 1;
672 DELETE FROM $perf_table WHERE ROWID=rec.rr;
673 IF cnt = 1000 THEN
674 COMMIT;
675 cnt := 0;
676 END IF;
677 END LOOP;
678 commit;
679 END;";
681 $ok = $this->conn->Execute($sql);