<?php
/*
* write by [email protected] 20100617
* version 1.0
* QQ 38347999
*/
define('API_KEY', 'YOUR_KEY');
define('dbhost','ip');
define('dbuser','user');
define('dbpw','pwd');
define('dbname','database');
require './class/DB.class.php';
require './class/explain.class.php';
//接受sql语句
$insql=trim($_POST["insql"]);
$insql=str_replace("\\","","$insql");
$thesubmit=$_POST["submit"];
?>
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="Css/Basic.css"></head>
<title>校园网(www.xiaoyuan.com)SQL性能分析工具--</title>
</head>
<body bgcolor="white" >
<div id="body" align="left">
<table width="500" border="0" cellpadding="0" cellspacing="0">
<tr><td>
<form action="" method="POST">
<textarea name="insql" rows="4" cols="120"><?php echo $insql ?>
</textarea>
<div align="center"><input type="submit" value="Do" name="submit" /> <input type="submit" value="Result" name="submit" /> </div>
<?php echo $insql ?>
</form></td></tr></table>
<?php
if ($_POST["submit"]=="Result")
{
$db=new DB();
$db->connect(dbhost, dbuser, dbpw, dbname);
$resql=$insql." limit 30";
$result=$db->query("$resql");
$showtable="<table width=960 border=1 cellpadding=3 cellspacing=0><tr>";
$Fields = mysql_num_fields($result);
for($i=0;$i<$Fields;$i++) {
$showtable=$showtable.'<td >'.mysql_field_name($result,$i).'</td>';
}
$showtable=$showtable.'</tr>';
$rowid=0;
while($row=mysql_fetch_row($result)) {
$showtable= $showtable."<tr>";
$rowid=$rowid+1;
for($i=0;$i<count($row);$i++) {
$showtable=$showtable."<td valign=top>".$row[$i]."</td>";
}
$showtable=$showtable.'</tr>';
}
$showtable=$showtable.'</table>';
if ($rowid==30){
echo "总结果超过30,这里只显示30条!</font></tr>";
}
else
{
echo "<br>执行结果.$rowid.条";
}
echo $showtable;
}
?>
<?php
if ($insql<>"") {
$db=new DB();
$db->connect(dbhost, dbuser, dbpw, dbname);
$sql="explain extended $insql";
$result=$db->query($sql);
$showtable="<table width=960 border=1 cellpadding=3 cellspacing=0><tr>";
$Fields = mysql_num_fields($result);
for($i=0;$i<$Fields;$i++) {
$showtable=$showtable.'<td >'.mysql_field_name($result,$i).'</td>';
}
$showtable=$showtable.'</tr>';
while($row=mysql_fetch_row($result)) {
$showtable= $showtable."<tr>";
for($i=0;$i<count($row);$i++) {
if ($i==3 and $row[$i]=="ALL")
$showtable=$showtable."<td valign=top><font color=red>".$row[$i]."</font></td>";
elseif($i==8 and $row[$i]>3000)
$showtable=$showtable."<td valign=top><font color=red>".$row[$i]."</font></td>";
else
$showtable=$showtable."<td valign=top>".$row[$i]."</td>";
}
$showtable=$showtable.'</tr>';
}
$showtable=$showtable.'</table>';
$sql2="show warnings";
$result2=$db->query($sql2);
// $row2=mysql_fetch_object($result2);
$rs= mysql_fetch_array($result2);
echo "<table width=960 border=1 cellpadding=3 cellspacing=0><tr>";
echo "<td>Mysql详细语句<td>";
echo $rs[2];
echo "</table>";
// var_dump($rs);
// echo print_r()
// var_dump
//echo "<tr>".$row2."</tr>";
echo "语句执行分析";
echo $showtable;
//显示profile
echo "语句执行时间";
$sql3="set profiling=1";
$result3=$db->query($sql3);
//$result4=$db->query($sql);
$result4=mysql_query($insql);
$sql5="show profiles";
$result5=mysql_query($sql5);
$showtable="<table width=960 border=1 cellpadding=3 cellspacing=0><tr>";
$Fields = mysql_num_fields($result5);
for($i=0;$i<$Fields;$i++) {
$showtable=$showtable.'<td bordercolor=#FFFFFF>'.mysql_field_name($result5,$i).'</td>';
}
$showtable=$showtable.'</tr>';
while($row=mysql_fetch_array($result5)) {
$showtable= $showtable."<tr>";
for($i=0;$i<count($row);$i++) {
if ($i==1 and $row[$i]>0.3)
$showtable=$showtable."<td valign=top bordercolor=#FFFFFF><font color=red>".$row[$i]."</font></td>";
else
$showtable=$showtable."<td valign=top bordercolor=#FFFFFF>".$row[$i]."</td>";
}
}
$showtable=$showtable.'</tr></table>';
echo $showtable;
$sql6="show profile cpu limit 40";
$result6=mysql_query($sql6);
$showtable="<table width=960 border=1 cellpadding=3 cellspacing=0><tr>";
$Fields = mysql_num_fields($result6);
$showtable=$showtable.'<td >ID</td>';
for($i=0;$i<$Fields;$i++) {
$showtable=$showtable.'<td >'.mysql_field_name($result6,$i).'</td>';
}
$showtable=$showtable.'</tr>';
$rowid=0;
while($row=mysql_fetch_array($result6)) {
$rowid++;
$showtable= $showtable."<tr>";
for($i=0;$i<count($row);$i++) {
if($i==0)
$showtable=$showtable."<td valign=top>".$rowid."</td>";
$showtable=$showtable."<td valign=top>".$row[$i]."</td>";
}
$showtable=$showtable.'</tr>';
}
if ($rowid>35)
$showtable=$showtable."<tr><font color=red>总执行步骤数目超过$rowid,此SQL语句可能存在问题!</font></tr>";
else
$showtable=$showtable."<tr><font>总执行步骤数目:$rowid</font></tr>";
$showtable=$showtable.'</table>';
echo $showtable;
//显示设计到的表的索引
$showtable=new explainclass();
$table=$showtable->gettable($insql);
echo "涉及到的表的索引情况";
echo "<br>";
foreach($table as $value)//格式化数组
{
$sqlengine="show table status where Name='$value'";
$result8=mysql_query($sqlengine);
$row=mysql_fetch_array($result8);
echo "<b>$value</b>:$row[Engine]";
mysql_free_result($result8);
$sqltable="show index from $value";
$result7=mysql_query($sqltable);
$showtable="<table width=960 border=1 cellpadding=3 cellspacing=0><tr>";
$Fields = mysql_num_fields($result7);
$showtable=$showtable.'<td >ID</td>';
for($i=0;$i<$Fields;$i++) {
$showtable=$showtable.'<td width="160">'.mysql_field_name($result7,$i).'</td>';
}
$showtable=$showtable.'</tr>';
$rowid=0;
while($row=mysql_fetch_array($result7)) {
$rowid++;
$showtable= $showtable."<tr>";
for($i=0;$i<count($row);$i++) {
if($i==0)
$showtable=$showtable."<td valign=top>".$rowid."</td>";
$showtable=$showtable."<td valign=top>".$row[$i]."</td>";
}
$showtable=$showtable.'</tr>';
}
$showtable=$showtable.'</table>';
echo $showtable;
}
///end show index
$db->close();
}
?>
</div>
<div id="footer"><small><small>
</small></small></div>
</body></html>
|