PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/adodb/perf/perf-oci8.inc.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 702 lines | 566 code | 96 blank | 40 comment | 60 complexity | b151d632bc373d60b3b9813aa9753a13 MD5 | raw file
  1. <?php
  2. /*
  3. V5.18 3 Sep 2012 (c) 2000-2012 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.
  8. Latest version is available at http://adodb.sourceforge.net
  9. Library for basic performance monitoring and tuning
  10. */
  11. // security - hide paths
  12. if (!defined('ADODB_DIR')) die();
  13. class perf_oci8 extends ADODB_perf{
  14. var $noShowIxora = 15; // if the sql for suspicious sql is taking too long, then disable ixora
  15. var $tablesSQL = "select segment_name as \"tablename\", sum(bytes)/1024 as \"size_in_k\",tablespace_name as \"tablespace\",count(*) \"extents\" from sys.user_extents
  16. group by segment_name,tablespace_name";
  17. var $version;
  18. var $createTableSQL = "CREATE TABLE adodb_logsql (
  19. created date NOT NULL,
  20. sql0 varchar(250) NOT NULL,
  21. sql1 varchar(4000) NOT NULL,
  22. params varchar(4000),
  23. tracer varchar(4000),
  24. timer decimal(16,6) NOT NULL
  25. )";
  26. var $settings = array(
  27. 'Ratios',
  28. 'data cache hit ratio' => array('RATIOH',
  29. "select round((1-(phy.value / (cur.value + con.value)))*100,2)
  30. from v\$sysstat cur, v\$sysstat con, v\$sysstat phy
  31. where cur.name = 'db block gets' and
  32. con.name = 'consistent gets' and
  33. phy.name = 'physical reads'",
  34. '=WarnCacheRatio'),
  35. 'sql cache hit ratio' => array( 'RATIOH',
  36. 'select round(100*(sum(pins)-sum(reloads))/sum(pins),2) from v$librarycache',
  37. 'increase <i>shared_pool_size</i> if too ratio low'),
  38. 'datadict cache hit ratio' => array('RATIOH',
  39. "select
  40. round((1 - (sum(getmisses) / (sum(gets) +
  41. sum(getmisses))))*100,2)
  42. from v\$rowcache",
  43. 'increase <i>shared_pool_size</i> if too ratio low'),
  44. 'memory sort ratio' => array('RATIOH',
  45. "SELECT ROUND((100 * b.VALUE) /DECODE ((a.VALUE + b.VALUE),
  46. 0,1,(a.VALUE + b.VALUE)),2)
  47. FROM v\$sysstat a,
  48. v\$sysstat b
  49. WHERE a.name = 'sorts (disk)'
  50. AND b.name = 'sorts (memory)'",
  51. "% of memory sorts compared to disk sorts - should be over 95%"),
  52. 'IO',
  53. 'data reads' => array('IO',
  54. "select value from v\$sysstat where name='physical reads'"),
  55. 'data writes' => array('IO',
  56. "select value from v\$sysstat where name='physical writes'"),
  57. 'Data Cache',
  58. 'data cache buffers' => array( 'DATAC',
  59. "select a.value/b.value from v\$parameter a, v\$parameter b
  60. where a.name = 'db_cache_size' and b.name= 'db_block_size'",
  61. 'Number of cache buffers. Tune <i>db_cache_size</i> if the <i>data cache hit ratio</i> is too low.'),
  62. 'data cache blocksize' => array('DATAC',
  63. "select value from v\$parameter where name='db_block_size'",
  64. '' ),
  65. 'Memory Pools',
  66. 'Mem Max Target (11g+)' => array( 'DATAC',
  67. "select value from v\$parameter where name = 'memory_max_target'",
  68. 'The memory_max_size is the maximum value to which memory_target can be set.' ),
  69. 'Memory target (11g+)' => array( 'DATAC',
  70. "select value from v\$parameter where name = 'memory_target'",
  71. 'If memory_target is defined then SGA and PGA targets are consolidated into one memory_target.' ),
  72. 'SGA Max Size' => array( 'DATAC',
  73. "select nvl(value,0)/1024.0/1024 || 'M' from v\$parameter where name = 'sga_max_size'",
  74. 'The sga_max_size is the maximum value to which sga_target can be set.' ),
  75. 'SGA target' => array( 'DATAC',
  76. "select nvl(value,0)/1024.0/1024 || 'M' from v\$parameter where name = 'sga_target'",
  77. '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.' ),
  78. 'PGA aggr target' => array( 'DATAC',
  79. "select nvl(value,0)/1024.0/1024 || 'M' from v\$parameter where name = 'pga_aggregate_target'",
  80. '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.' ),
  81. 'data cache size' => array('DATAC',
  82. "select value from v\$parameter where name = 'db_cache_size'",
  83. 'db_cache_size' ),
  84. 'shared pool size' => array('DATAC',
  85. "select value from v\$parameter where name = 'shared_pool_size'",
  86. 'shared_pool_size, which holds shared sql, stored procedures, dict cache and similar shared structs' ),
  87. 'java pool size' => array('DATAJ',
  88. "select value from v\$parameter where name = 'java_pool_size'",
  89. 'java_pool_size' ),
  90. 'large pool buffer size' => array('CACHE',
  91. "select value from v\$parameter where name='large_pool_size'",
  92. '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) ' ),
  93. 'dynamic memory usage' => array('CACHE', "select '-' from dual", '=DynMemoryUsage'),
  94. 'Connections',
  95. 'current connections' => array('SESS',
  96. 'select count(*) from sys.v_$session where username is not null',
  97. ''),
  98. 'max connections' => array( 'SESS',
  99. "select value from v\$parameter where name='sessions'",
  100. ''),
  101. 'Memory Utilization',
  102. 'data cache utilization ratio' => array('RATIOU',
  103. "select round((1-bytes/sgasize)*100, 2)
  104. from (select sum(bytes) sgasize from sys.v_\$sgastat) s, sys.v_\$sgastat f
  105. where name = 'free memory' and pool = 'shared pool'",
  106. 'Percentage of data cache actually in use - should be over 85%'),
  107. 'shared pool utilization ratio' => array('RATIOU',
  108. 'select round((sga.bytes/case when p.value=0 then sga.bytes else to_number(p.value) end)*100,2)
  109. from v$sgastat sga, v$parameter p
  110. where sga.name = \'free memory\' and sga.pool = \'shared pool\'
  111. and p.name = \'shared_pool_size\'',
  112. 'Percentage of shared pool actually used - too low is bad, too high is worse'),
  113. 'large pool utilization ratio' => array('RATIOU',
  114. "select round((1-bytes/sgasize)*100, 2)
  115. from (select sum(bytes) sgasize from sys.v_\$sgastat) s, sys.v_\$sgastat f
  116. where name = 'free memory' and pool = 'large pool'",
  117. 'Percentage of large_pool actually in use - too low is bad, too high is worse'),
  118. 'sort buffer size' => array('CACHE',
  119. "select value from v\$parameter where name='sort_area_size'",
  120. 'max in-mem sort_area_size (per query), uses memory in pga' ),
  121. /*'pga usage at peak' => array('RATIOU',
  122. '=PGA','Mb utilization at peak transactions (requires Oracle 9i+)'),*/
  123. 'Transactions',
  124. 'rollback segments' => array('ROLLBACK',
  125. "select count(*) from sys.v_\$rollstat",
  126. ''),
  127. 'peak transactions' => array('ROLLBACK',
  128. "select max_utilization tx_hwm
  129. from sys.v_\$resource_limit
  130. where resource_name = 'transactions'",
  131. 'Taken from high-water-mark'),
  132. 'max transactions' => array('ROLLBACK',
  133. "select value from v\$parameter where name = 'transactions'",
  134. 'max transactions / rollback segments < 3.5 (or transactions_per_rollback_segment)'),
  135. 'Parameters',
  136. 'cursor sharing' => array('CURSOR',
  137. "select value from v\$parameter where name = 'cursor_sharing'",
  138. '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>.'),
  139. /*
  140. 'cursor reuse' => array('CURSOR',
  141. "select count(*) from (select sql_text_wo_constants, count(*)
  142. from t1
  143. group by sql_text_wo_constants
  144. having count(*) > 100)",'These are sql statements that should be using bind variables'),*/
  145. 'index cache cost' => array('COST',
  146. "select value from v\$parameter where name = 'optimizer_index_caching'",
  147. '=WarnIndexCost'),
  148. 'random page cost' => array('COST',
  149. "select value from v\$parameter where name = 'optimizer_index_cost_adj'",
  150. '=WarnPageCost'),
  151. 'Waits',
  152. 'Recent wait events' => array('WAITS','select \'Top 5 events\' from dual','=TopRecentWaits'),
  153. // 'Historical wait SQL' => array('WAITS','select \'Last 2 days\' from dual','=TopHistoricalWaits'), -- requires AWR license
  154. 'Backup',
  155. 'Achivelog Mode' => array('BACKUP', 'select log_mode from v$database', '=LogMode'),
  156. 'DBID' => array('BACKUP','select dbid from v$database','Primary key of database, used for recovery with an RMAN Recovery Catalog'),
  157. 'Archive Log Dest' => array('BACKUP', "SELECT NVL(v1.value,v2.value)
  158. FROM v\$parameter v1, v\$parameter v2 WHERE v1.name='log_archive_dest' AND v2.name='log_archive_dest_10'", ''),
  159. '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.'),
  160. 'Flashback Usage' => array('BACKUP', "select nvl('-','Flashback Area not used') from v\$parameter where name=lower('DB_RECOVERY_FILE_DEST')", '=FlashUsage', 'Flashback area usage.'),
  161. '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.'),
  162. 'Recent RMAN Jobs' => array('BACKUP', "select '-' from dual", "=RMAN"),
  163. // '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.'),
  164. 'Storage', 'Tablespaces' => array('TABLESPACE', "select '-' from dual", "=TableSpace"),
  165. false
  166. );
  167. function perf_oci8(&$conn)
  168. {
  169. global $gSQLBlockRows;
  170. $gSQLBlockRows = 1000;
  171. $savelog = $conn->LogSQL(false);
  172. $this->version = $conn->ServerInfo();
  173. $conn->LogSQL($savelog);
  174. $this->conn = $conn;
  175. }
  176. function LogMode()
  177. {
  178. $mode = $this->conn->GetOne("select log_mode from v\$database");
  179. if ($mode == 'ARCHIVELOG') return 'To turn off archivelog:<br>
  180. <pre><font size=-2>
  181. SQLPLUS> connect sys as sysdba;
  182. SQLPLUS> shutdown immediate;
  183. SQLPLUS> startup mount exclusive;
  184. SQLPLUS> alter database noarchivelog;
  185. SQLPLUS> alter database open;
  186. </font></pre>';
  187. return 'To turn on archivelog:<br>
  188. <pre><font size=-2>
  189. SQLPLUS> connect sys as sysdba;
  190. SQLPLUS> shutdown immediate;
  191. SQLPLUS> startup mount exclusive;
  192. SQLPLUS> alter database archivelog;
  193. SQLPLUS> archive log start;
  194. SQLPLUS> alter database open;
  195. </font></pre>';
  196. }
  197. function TopRecentWaits()
  198. {
  199. $rs = $this->conn->Execute("select * from (
  200. select event, round(100*time_waited/(select sum(time_waited) from v\$system_event where wait_class <> 'Idle'),1) \"% Wait\",
  201. total_waits,time_waited, average_wait,wait_class from v\$system_event where wait_class <> 'Idle' order by 2 desc
  202. ) where rownum <=5");
  203. $ret = rs2html($rs,false,false,false,false);
  204. return "&nbsp;<p>".$ret."&nbsp;</p>";
  205. }
  206. function TopHistoricalWaits()
  207. {
  208. $days = 2;
  209. $rs = $this->conn->Execute("select * from ( SELECT
  210. b.wait_class,B.NAME,
  211. round(sum(wait_time+TIME_WAITED)/1000000) waitsecs,
  212. parsing_schema_name,
  213. C.SQL_TEXT, a.sql_id
  214. FROM V\$ACTIVE_SESSION_HISTORY A
  215. join V\$EVENT_NAME B on A.EVENT# = B.EVENT#
  216. join V\$SQLAREA C on A.SQL_ID = C.SQL_ID
  217. WHERE A.SAMPLE_TIME BETWEEN sysdate-$days and sysdate
  218. and parsing_schema_name not in ('SYS','SYSMAN','DBSNMP','SYSTEM')
  219. GROUP BY b.wait_class,parsing_schema_name,C.SQL_TEXT, B.NAME,A.sql_id
  220. order by 3 desc) where rownum <=10");
  221. $ret = rs2html($rs,false,false,false,false);
  222. return "&nbsp;<p>".$ret."&nbsp;</p>";
  223. }
  224. function TableSpace()
  225. {
  226. $rs = $this->conn->Execute(
  227. "select tablespace_name,round(sum(bytes)/1024/1024) as Used_MB,round(sum(maxbytes)/1024/1024) as Max_MB, round(sum(bytes)/sum(maxbytes),4) * 100 as PCT
  228. from dba_data_files
  229. group by tablespace_name order by 2 desc");
  230. $ret = "<p><b>Tablespace</b>".rs2html($rs,false,false,false,false);
  231. $rs = $this->conn->Execute("select * from dba_data_files order by tablespace_name, 1");
  232. $ret .= "<p><b>Datafile</b>".rs2html($rs,false,false,false,false);
  233. return "&nbsp;<p>".$ret."&nbsp;</p>";
  234. }
  235. function RMAN()
  236. {
  237. $rs = $this->conn->Execute("select * from (select start_time, end_time, operation, status, mbytes_processed, output_device_type
  238. from V\$RMAN_STATUS order by start_time desc) where rownum <=10");
  239. $ret = rs2html($rs,false,false,false,false);
  240. return "&nbsp;<p>".$ret."&nbsp;</p>";
  241. }
  242. function DynMemoryUsage()
  243. {
  244. if (@$this->version['version'] >= 11) {
  245. $rs = $this->conn->Execute("select component, current_size/1024./1024 as \"CurrSize (M)\" from V\$MEMORY_DYNAMIC_COMPONENTS");
  246. } else
  247. $rs = $this->conn->Execute("select name, round(bytes/1024./1024,2) as \"CurrSize (M)\" from V\$sgainfo");
  248. $ret = rs2html($rs,false,false,false,false);
  249. return "&nbsp;<p>".$ret."&nbsp;</p>";
  250. }
  251. function FlashUsage()
  252. {
  253. $rs = $this->conn->Execute("select * from V\$FLASH_RECOVERY_AREA_USAGE");
  254. $ret = rs2html($rs,false,false,false,false);
  255. return "&nbsp;<p>".$ret."&nbsp;</p>";
  256. }
  257. function WarnPageCost($val)
  258. {
  259. if ($val == 100 && $this->version['version'] < 10) $s = '<font color=red><b>Too High</b>. </font>';
  260. else $s = '';
  261. 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>. ';
  262. }
  263. function WarnIndexCost($val)
  264. {
  265. if ($val == 0 && $this->version['version'] < 10) $s = '<font color=red><b>Too Low</b>. </font>';
  266. else $s = '';
  267. return $s.'Percentage of indexed data blocks expected in the cache.
  268. Recommended is 20 (fast disk array) to 30 (slower hard disks). Default is 0.
  269. See <a href=http://www.dba-oracle.com/oracle_tips_cbo_part1.htm>optimizer_index_caching</a>.';
  270. }
  271. function PGA()
  272. {
  273. //if ($this->version['version'] < 9) return 'Oracle 9i or later required';
  274. }
  275. function PGA_Advice()
  276. {
  277. $t = "<h3>PGA Advice Estimate</h3>";
  278. if ($this->version['version'] < 9) return $t.'Oracle 9i or later required';
  279. $rs = $this->conn->Execute('select a.MB,
  280. case when a.targ = 1 then \'<<= Current \'
  281. when a.targ < 1 or a.pct <= b.pct then null
  282. else
  283. \'- BETTER than Current by \'||round(a.pct/b.pct*100-100,2)||\'%\' end as "Percent Improved",
  284. a.targ as "PGA Size Factor",a.pct "% Perf"
  285. from
  286. (select round(pga_target_for_estimate/1024.0/1024.0,0) MB,
  287. pga_target_factor targ,estd_pga_cache_hit_percentage pct,rownum as r
  288. from v$pga_target_advice) a left join
  289. (select round(pga_target_for_estimate/1024.0/1024.0,0) MB,
  290. pga_target_factor targ,estd_pga_cache_hit_percentage pct,rownum as r
  291. from v$pga_target_advice) b on
  292. a.r = b.r+1 where
  293. b.pct < 100');
  294. if (!$rs) return $t."Only in 9i or later";
  295. // $rs->Close();
  296. if ($rs->EOF) return $t."PGA could be too big";
  297. return $t.rs2html($rs,false,false,true,false);
  298. }
  299. function Explain($sql,$partial=false)
  300. {
  301. $savelog = $this->conn->LogSQL(false);
  302. $rs = $this->conn->SelectLimit("select ID FROM PLAN_TABLE");
  303. if (!$rs) {
  304. echo "<p><b>Missing PLAN_TABLE</b></p>
  305. <pre>
  306. CREATE TABLE PLAN_TABLE (
  307. STATEMENT_ID VARCHAR2(30),
  308. TIMESTAMP DATE,
  309. REMARKS VARCHAR2(80),
  310. OPERATION VARCHAR2(30),
  311. OPTIONS VARCHAR2(30),
  312. OBJECT_NODE VARCHAR2(128),
  313. OBJECT_OWNER VARCHAR2(30),
  314. OBJECT_NAME VARCHAR2(30),
  315. OBJECT_INSTANCE NUMBER(38),
  316. OBJECT_TYPE VARCHAR2(30),
  317. OPTIMIZER VARCHAR2(255),
  318. SEARCH_COLUMNS NUMBER,
  319. ID NUMBER(38),
  320. PARENT_ID NUMBER(38),
  321. POSITION NUMBER(38),
  322. COST NUMBER(38),
  323. CARDINALITY NUMBER(38),
  324. BYTES NUMBER(38),
  325. OTHER_TAG VARCHAR2(255),
  326. PARTITION_START VARCHAR2(255),
  327. PARTITION_STOP VARCHAR2(255),
  328. PARTITION_ID NUMBER(38),
  329. OTHER LONG,
  330. DISTRIBUTION VARCHAR2(30)
  331. );
  332. </pre>";
  333. return false;
  334. }
  335. $rs->Close();
  336. // $this->conn->debug=1;
  337. if ($partial) {
  338. $sqlq = $this->conn->qstr($sql.'%');
  339. $arr = $this->conn->GetArray("select distinct sql1 from adodb_logsql where sql1 like $sqlq");
  340. if ($arr) {
  341. foreach($arr as $row) {
  342. $sql = reset($row);
  343. if (crc32($sql) == $partial) break;
  344. }
  345. }
  346. }
  347. $s = "<p><b>Explain</b>: ".htmlspecialchars($sql)."</p>";
  348. $this->conn->BeginTrans();
  349. $id = "ADODB ".microtime();
  350. $rs = $this->conn->Execute("EXPLAIN PLAN SET STATEMENT_ID='$id' FOR $sql");
  351. $m = $this->conn->ErrorMsg();
  352. if ($m) {
  353. $this->conn->RollbackTrans();
  354. $this->conn->LogSQL($savelog);
  355. $s .= "<p>$m</p>";
  356. return $s;
  357. }
  358. $rs = $this->conn->Execute("
  359. select
  360. '<pre>'||lpad('--', (level-1)*2,'-') || trim(operation) || ' ' || trim(options)||'</pre>' as Operation,
  361. object_name,COST,CARDINALITY,bytes
  362. FROM plan_table
  363. START WITH id = 0 and STATEMENT_ID='$id'
  364. CONNECT BY prior id=parent_id and statement_id='$id'");
  365. $s .= rs2html($rs,false,false,false,false);
  366. $this->conn->RollbackTrans();
  367. $this->conn->LogSQL($savelog);
  368. $s .= $this->Tracer($sql,$partial);
  369. return $s;
  370. }
  371. function CheckMemory()
  372. {
  373. if ($this->version['version'] < 9) return 'Oracle 9i or later required';
  374. $rs = $this->conn->Execute("
  375. select a.name Buffer_Pool, b.size_for_estimate as cache_mb_estimate,
  376. case when b.size_factor=1 then
  377. '&lt;&lt;= Current'
  378. when a.estd_physical_read_factor-b.estd_physical_read_factor > 0.001 and b.estd_physical_read_factor<1 then
  379. '- BETTER than current by ' || round((1-b.estd_physical_read_factor)/b.estd_physical_read_factor*100,2) || '%'
  380. else ' ' end as RATING,
  381. b.estd_physical_read_factor \"Phys. Reads Factor\",
  382. round((a.estd_physical_read_factor-b.estd_physical_read_factor)/b.estd_physical_read_factor*100,2) as \"% Improve\"
  383. from (select size_for_estimate,size_factor,estd_physical_read_factor,rownum r,name from v\$db_cache_advice order by name,1) a ,
  384. (select size_for_estimate,size_factor,estd_physical_read_factor,rownum r,name from v\$db_cache_advice order by name,1) b
  385. where a.r = b.r-1 and a.name = b.name
  386. ");
  387. if (!$rs) return false;
  388. /*
  389. The v$db_cache_advice utility show the marginal changes in physical data block reads for different sizes of db_cache_size
  390. */
  391. $s = "<h3>Data Cache Advice Estimate</h3>";
  392. if ($rs->EOF) {
  393. $s .= "<p>Cache that is 50% of current size is still too big</p>";
  394. } else {
  395. $s .= "Ideal size of Data Cache is when %BETTER gets close to zero.";
  396. $s .= rs2html($rs,false,false,false,false);
  397. }
  398. return $s.$this->PGA_Advice();
  399. }
  400. /*
  401. Generate html for suspicious/expensive sql
  402. */
  403. function tohtml(&$rs,$type)
  404. {
  405. $o1 = $rs->FetchField(0);
  406. $o2 = $rs->FetchField(1);
  407. $o3 = $rs->FetchField(2);
  408. if ($rs->EOF) return '<p>None found</p>';
  409. $check = '';
  410. $sql = '';
  411. $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>';
  412. while (!$rs->EOF) {
  413. if ($check != $rs->fields[0].'::'.$rs->fields[1]) {
  414. if ($check) {
  415. $carr = explode('::',$check);
  416. $prefix = "<a href=\"?$type=1&sql=".rawurlencode($sql).'&x#explain">';
  417. $suffix = '</a>';
  418. if (strlen($prefix)>2000) {
  419. $prefix = '';
  420. $suffix = '';
  421. }
  422. $s .= "\n<tr><td align=right>".$carr[0].'</td><td align=right>'.$carr[1].'</td><td>'.$prefix.$sql.$suffix.'</td></tr>';
  423. }
  424. $sql = $rs->fields[2];
  425. $check = $rs->fields[0].'::'.$rs->fields[1];
  426. } else
  427. $sql .= $rs->fields[2];
  428. if (substr($sql,strlen($sql)-1) == "\0") $sql = substr($sql,0,strlen($sql)-1);
  429. $rs->MoveNext();
  430. }
  431. $rs->Close();
  432. $carr = explode('::',$check);
  433. $prefix = "<a target=".rand()." href=\"?&hidem=1&$type=1&sql=".rawurlencode($sql).'&x#explain">';
  434. $suffix = '</a>';
  435. if (strlen($prefix)>2000) {
  436. $prefix = '';
  437. $suffix = '';
  438. }
  439. $s .= "\n<tr><td align=right>".$carr[0].'</td><td align=right>'.$carr[1].'</td><td>'.$prefix.$sql.$suffix.'</td></tr>';
  440. return $s."</table>\n\n";
  441. }
  442. // code thanks to Ixora.
  443. // http://www.ixora.com.au/scripts/query_opt.htm
  444. // requires oracle 8.1.7 or later
  445. function SuspiciousSQL($numsql=10)
  446. {
  447. $sql = "
  448. select
  449. substr(to_char(s.pct, '99.00'), 2) || '%' load,
  450. s.executions executes,
  451. p.sql_text
  452. from
  453. (
  454. select
  455. address,
  456. buffer_gets,
  457. executions,
  458. pct,
  459. rank() over (order by buffer_gets desc) ranking
  460. from
  461. (
  462. select
  463. address,
  464. buffer_gets,
  465. executions,
  466. 100 * ratio_to_report(buffer_gets) over () pct
  467. from
  468. sys.v_\$sql
  469. where
  470. command_type != 47 and module != 'T.O.A.D.'
  471. )
  472. where
  473. buffer_gets > 50 * executions
  474. ) s,
  475. sys.v_\$sqltext p
  476. where
  477. s.ranking <= $numsql and
  478. p.address = s.address
  479. order by
  480. 1 desc, s.address, p.piece";
  481. global $ADODB_CACHE_MODE;
  482. if (isset($_GET['expsixora']) && isset($_GET['sql'])) {
  483. $partial = empty($_GET['part']);
  484. echo "<a name=explain></a>".$this->Explain($_GET['sql'],$partial)."\n";
  485. }
  486. if (isset($_GET['sql'])) return $this->_SuspiciousSQL($numsql);
  487. $s = '';
  488. $timer = time();
  489. $s .= $this->_SuspiciousSQL($numsql);
  490. $timer = time() - $timer;
  491. if ($timer > $this->noShowIxora) return $s;
  492. $s .= '<p>';
  493. $save = $ADODB_CACHE_MODE;
  494. $ADODB_CACHE_MODE = ADODB_FETCH_NUM;
  495. if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
  496. $savelog = $this->conn->LogSQL(false);
  497. $rs = $this->conn->SelectLimit($sql);
  498. $this->conn->LogSQL($savelog);
  499. if (isset($savem)) $this->conn->SetFetchMode($savem);
  500. $ADODB_CACHE_MODE = $save;
  501. if ($rs) {
  502. $s .= "\n<h3>Ixora Suspicious SQL</h3>";
  503. $s .= $this->tohtml($rs,'expsixora');
  504. }
  505. return $s;
  506. }
  507. // code thanks to Ixora.
  508. // http://www.ixora.com.au/scripts/query_opt.htm
  509. // requires oracle 8.1.7 or later
  510. function ExpensiveSQL($numsql = 10)
  511. {
  512. $sql = "
  513. select
  514. substr(to_char(s.pct, '99.00'), 2) || '%' load,
  515. s.executions executes,
  516. p.sql_text
  517. from
  518. (
  519. select
  520. address,
  521. disk_reads,
  522. executions,
  523. pct,
  524. rank() over (order by disk_reads desc) ranking
  525. from
  526. (
  527. select
  528. address,
  529. disk_reads,
  530. executions,
  531. 100 * ratio_to_report(disk_reads) over () pct
  532. from
  533. sys.v_\$sql
  534. where
  535. command_type != 47 and module != 'T.O.A.D.'
  536. )
  537. where
  538. disk_reads > 50 * executions
  539. ) s,
  540. sys.v_\$sqltext p
  541. where
  542. s.ranking <= $numsql and
  543. p.address = s.address
  544. order by
  545. 1 desc, s.address, p.piece
  546. ";
  547. global $ADODB_CACHE_MODE;
  548. if (isset($_GET['expeixora']) && isset($_GET['sql'])) {
  549. $partial = empty($_GET['part']);
  550. echo "<a name=explain></a>".$this->Explain($_GET['sql'],$partial)."\n";
  551. }
  552. if (isset($_GET['sql'])) {
  553. $var = $this->_ExpensiveSQL($numsql);
  554. return $var;
  555. }
  556. $s = '';
  557. $timer = time();
  558. $s .= $this->_ExpensiveSQL($numsql);
  559. $timer = time() - $timer;
  560. if ($timer > $this->noShowIxora) return $s;
  561. $s .= '<p>';
  562. $save = $ADODB_CACHE_MODE;
  563. $ADODB_CACHE_MODE = ADODB_FETCH_NUM;
  564. if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
  565. $savelog = $this->conn->LogSQL(false);
  566. $rs = $this->conn->Execute($sql);
  567. $this->conn->LogSQL($savelog);
  568. if (isset($savem)) $this->conn->SetFetchMode($savem);
  569. $ADODB_CACHE_MODE = $save;
  570. if ($rs) {
  571. $s .= "\n<h3>Ixora Expensive SQL</h3>";
  572. $s .= $this->tohtml($rs,'expeixora');
  573. }
  574. return $s;
  575. }
  576. function clearsql()
  577. {
  578. $perf_table = adodb_perf::table();
  579. // using the naive "delete from $perf_table where created<".$this->conn->sysTimeStamp will cause the table to lock, possibly
  580. // for a long time
  581. $sql =
  582. "DECLARE cnt pls_integer;
  583. BEGIN
  584. cnt := 0;
  585. FOR rec IN (SELECT ROWID AS rr FROM $perf_table WHERE created<SYSDATE)
  586. LOOP
  587. cnt := cnt + 1;
  588. DELETE FROM $perf_table WHERE ROWID=rec.rr;
  589. IF cnt = 1000 THEN
  590. COMMIT;
  591. cnt := 0;
  592. END IF;
  593. END LOOP;
  594. commit;
  595. END;";
  596. $ok = $this->conn->Execute($sql);
  597. }
  598. }
  599. ?>