-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexplain.php
59 lines (50 loc) · 1.82 KB
/
explain.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
<?php
#
# explain UI
#
require_once('init.php');
$explain = (isset($conf['explain'])) ? $conf['explain'] : true;
$anon = (isset($conf['anon'])) ? $conf['anon'] : false;
if (!$explain || $anon) {
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
return;
}
$checksum = mysql_real_escape_string($_GET['checksum']);
$hours = mysql_real_escape_string($_GET['hours']);
$q = "SELECT * FROM {$host_conf['db_query_review_history_table']} WHERE checksum = '{$checksum}'";
$result = mysql_query($q);
$row = mysql_fetch_assoc($result);
$query = $row['sample'];
$explain = "EXPLAIN ".preg_replace('|^\s*\/\*.*\*\/|', '', $query);
mysql_connect($conf['db_live_host'],$conf['db_live_user'],$conf['db_live_password']) or die(sprintf("Unable to connect to MySQL server: %s", mysql_error()));
mysql_select_db($host_conf['db_database_live']) or die("Unable to select database");
$is_select_query = (stripos($explain, 'SELECT') !== false);
if ($is_select_query) {
$result = mysql_query($explain);
}
$rows = array();
$tables = array();
while ($is_select_query && $row = mysql_fetch_assoc($result)) {
$row['possible_keys'] = implode('<br />', explode(',', $row['possible_keys']));
$rows[] = $row;
$table_alias = $row['table'];
$create_result = mysql_query("SHOW CREATE TABLE {$table_alias}");
if (!$create_result) {
# try to figure out the alias
$pattern = "/(?i)([\w]+?)(?=\s+(?:AS\s+)?\b{$table_alias}\b)/";
list($columns, $rest) = preg_split("/(?i)from/", $query);
preg_match_all($pattern, $rest, $matches);
$table = $matches[0][0];
$create_result = mysql_query("SHOW CREATE TABLE {$table}");
if (!$create_result) {
$table = '';
}
} else {
$table = $table_alias;
}
if ($create_result) {
$tables[$table] = mysql_fetch_assoc($create_result);
}
}
require("explain.tpl");