-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathslowquery.php
218 lines (194 loc) · 7.1 KB
/
slowquery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
session_start();
if($_GET['action'] == "logout"){
unset($_SESSION['transmit_dbname']);
//exit('<script>top.location.href="slowquery.php"</script>');
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>慢查询日志</title>
<link rel="stylesheet" href="./css/simple-line-icons/css/simple-line-icons.css">
<link rel="stylesheet" href="./css/font-awesome/css/fontawesome-all.min.css">
<link rel="stylesheet" href="./css/styles.css">
<script language="javascript">
function TestBlack(TagName){
var obj = document.getElementById(TagName);
if(obj.style.display=="block"){
obj.style.display = "none";
}else{
obj.style.display = "block";
}
}
</script>
<script>
function ss(){
var slt=document.getElementById("select");
if(slt.value==""){
alert("请选择数据库!!!");
return false;
}
return true;
}
</script>
</head>
<body>
<div class="card">
<div class="card-header bg-light">
<h1><a href="slowquery.php?action=logout">MySQL 慢查询分析</a></h1>
</div>
<div class="card-body">
<div class="table-responsive">
<form action="" method="post" name="sql_statement" id="form1" onsubmit=" return ss()">
<div>
<tr>
<td><select id="select" name="dbname">
<option value="">选择你的数据库</option>
<?php
require 'config.php';
$result = mysqli_query($con,"SELECT dbname FROM dbinfo order by dbname ASC");
while($row = mysqli_fetch_array($result)){
if(isset($_POST['dbname']) || isset($_GET['dbname'])){
if($_POST['dbname'] == $row[0] || $_GET['dbname'] == $row[0]){
echo "<option selected='selected' value=\"".$row[0]."\">".$row[0]."</option>"."<br>";
} else {
echo "<option value=\"".$row[0]."\">".$row[0]."</option>"."<br>";
}
} else{ echo "<option value=\"".$row[0]."\">".$row[0]."</option>"."<br>";}
}
?>
</select><td>
</tr>
<input name="submit" type="submit" class="STYLE3" value="搜索" />
</label>
</div>
</form>
<?php
if(isset($_POST['submit'])){
$dbname=$_POST['dbname'];
session_start();
$_SESSION['transmit_dbname']=$dbname;
require 'show.html';
} else {
session_start();
$dbname=$_SESSION['transmit_dbname'];
if(!empty($dbname)){
require 'show.html';
} else {
require 'top.html';
}
}
?>
<table class="table table-hover">
<thead>
<tr>
<th>抽象语句</th>
<th>数据库</th>
<th>用户名</th>
<th>最近时间</th>
<th>次数</th>
<th>平均时间</th>
<th>最小时间</th>
<th>最大时间</th>
</tr>
</thead>
<tbody>
<?php
session_start();
$select_dbname=$_SESSION['transmit_dbname'];
require 'config.php';
$perNumber=5; //每页显示的记录数
$page=$_GET['page']; //获得当前的页面值
$count=mysqli_query($con,"select count(*) from mysql_slow_query_review r where r.fingerprint not like '%commit%'"); //获得记录总数
$rs=mysqli_fetch_array($count);
$totalNumber=$rs[0];
#echo count($rs);
#echo $totalNumber;
$totalPage=ceil($totalNumber/$perNumber); //计算出总页数
#echo $totalPage;
if (empty($page)) {
$page=1;
} //如果没有值,则赋值1
$startCount=($page-1)*$perNumber; //分页开始,根据此方法计算出开始的记录
#echo $select_dbname;
if(!empty($select_dbname)){
$sql = "SELECT r.checksum,r.fingerprint,h.db_max,h.user_max,r.last_seen,SUM(h.ts_cnt) AS ts_cnt,
ROUND(MIN(h.Query_time_min),3) AS Query_time_min,ROUND(MAX(h.Query_time_max),3) AS Query_time_max,
ROUND(SUM(h.Query_time_sum)/SUM(h.ts_cnt),3) AS Query_time_avg,r.sample
FROM mysql_slow_query_review AS r JOIN mysql_slow_query_review_history AS h
ON r.checksum=h.checksum
WHERE h.db_max = '${select_dbname}'
AND r.last_seen >= SUBDATE(NOW(),INTERVAL 31 DAY) and r.fingerprint not like '%commit%'
GROUP BY r.checksum
ORDER BY r.last_seen DESC,ts_cnt DESC LIMIT $startCount,$perNumber";
} else {
$sql = "SELECT r.checksum,r.fingerprint,h.db_max,h.user_max,r.last_seen,SUM(h.ts_cnt) AS ts_cnt,
ROUND(MIN(h.Query_time_min),3) AS Query_time_min,ROUND(MAX(h.Query_time_max),3) AS Query_time_max,
ROUND(SUM(h.Query_time_sum)/SUM(h.ts_cnt),3) AS Query_time_avg,r.sample
FROM mysql_slow_query_review AS r JOIN mysql_slow_query_review_history AS h
ON r.checksum=h.checksum
WHERE r.last_seen >= SUBDATE(NOW(),INTERVAL 31 DAY) and r.fingerprint not like '%commit%'
GROUP BY r.checksum
ORDER BY r.last_seen DESC,ts_cnt DESC LIMIT $startCount,$perNumber";
}
$result = mysqli_query($con,$sql);
echo "<br> 慢查询日志agent采集阀值是每10分钟/次,SQL执行时间(单位:秒)</br>";
require 'SqlFormatter.php';
#echo sizeof($result);
#echo count($result);
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td width='100px' onclick=\"TestBlack('${row['0']}')\">✚ " .substr("{$row['1']}",0,50)
."<div id='${row['0']}' style='display:none;'><a href='slowquery_explain.php?checksum={$row['0']}' target='_blank'>" .SqlFormatter::format($row['1']) ."</br></div></a></td>";
echo "<td>{$row['2']}</td>";
echo "<td>{$row['3']}</td>";
echo "<td>{$row['4']}</td>";
echo "<td>{$row['5']}</td>";
echo "<td>{$row['8']}</td>";
echo "<td>{$row['6']}</td>";
echo "<td>{$row['7']}</td>";
echo "</tr>";
}
//end while
echo "</tbody>";
echo "</table>";
echo "</div>";
echo "</div>";
echo "</div>";
$maxPageCount=10;
$buffCount=2;
$startPage=1;
if ($page< $buffCount){
$startPage=1;
#echo $page;
}else if($page>=$buffCount and $page<$totalPage-$maxPageCount){
$startPage=$page-$buffCount+1;
}else{
$startPage=$totalPage-$maxPageCount+1;
if($startPage <=0){
$startPage=1;
}
}
#$endPage=$startPage+$maxPageCount-1;
$endPage=$totalPage;
$htmlstr="";
$htmlstr.="<table class='bordered' border='1' align='center'><tr>";
if ($page > 1){
$htmlstr.="<td> <a href='slowquery.php?dbname=$dbname&page=" . "1" . "'>第一页</a> </td>";
$htmlstr.="<td> <a href='slowquery.php?dbname=$dbname&page=" . ($page-1) . "'>上一页</a> </td>";
}
$htmlstr.="<td> 第${page}页/共${totalPage}页 </td>";
for ($i=$startPage;$i<=$endPage; $i++){
$htmlstr.="<td> <a href='slowquery.php?dbname=$dbname&page=" . $i . "'>" . $i . "</a> </td>";
}
if ($page<$totalPage){
$htmlstr.="<td> <a href='slowquery.php?dbname=$dbname&page=" . ($page+1) . "'>下一页</a> </td>";
$htmlstr.="<td> <a href='slowquery.php?dbname=$dbname&page=" . $totalPage . "'>最后页</a> </td>";
}
$htmlstr.="</tr></table>";
echo $htmlstr;
?>