-
Notifications
You must be signed in to change notification settings - Fork 1
/
ti-explain.ts
271 lines (247 loc) · 10.6 KB
/
ti-explain.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
function clearExplain() {
document.getElementById("simpletable").innerHTML = '';
document.getElementById("content").innerHTML = '';
document.getElementById("tree").innerHTML = '<pre id="treepre"></pre>';
( < HTMLTextAreaElement > document.getElementById("explaintext")).value = '';
}
function loadExample() {
fetch("testdata/explain_0004.txt")
.then(r => {
r.text()
.then(r => {
( < HTMLTextAreaElement > document.getElementById("explaintext")).value = r
})
})
}
function lineToArray(line, columnstarts) {
let cols = [];
for (let i = 0; i < columnstarts.length - 1; i++) {
let colStart = columnstarts[i] + 2;
let colEnd = columnstarts[i + 1] - 1;
cols.push(line.substring(colStart, colEnd));
}
return cols;
}
function lineToObj(line, explaininfo) {
let colData = {};
let cols = lineToArray(line, explaininfo["columnstarts"]);
for (let i = 0; i < cols.length; i++) {
colData[explaininfo["columnnames"][i]] = cols[i];
}
return colData;
}
function formatInfo(info) {
return info.replace(/: ?\{/g, ":{\n")
.replace(/, /g, ",\n")
.trim();
}
function formatStruct(info) {
let i = 0;
let r = "";
for (let c of info) {
if (![" ", "}", ")"].includes(c)) {
r += c;
}
if (["{", "("].includes(c)) {
i++;
r += "\n" + " ".repeat(i * 2);
} else if (["}", ")"].includes(c)) {
i--;
r += "\n" + " ".repeat(i * 2) + c;
} else if (c == ",") {
r += "\n" + " ".repeat(i * 2);
} else if (c == ":") {
r += " ";
}
}
return r;
}
function explainExplain(explaininfo) {
let outputDiv = document.getElementById("content");
let treeDiv = document.getElementById("tree");
treeDiv.innerHTML = '<pre id="treepre"></pre>';
let treePre = document.getElementById("treepre");
let simpleTbl = document.getElementById("simpletable");
outputDiv.innerHTML = '';
treeDiv.insertBefore(document.createTextNode("Explain Tree"), treePre);
let str = document.createElement("tr");
explaininfo["columnnames"].forEach(col => {
let td = document.createElement("td");
td.appendChild(document.createTextNode(col));
str.appendChild(td);
});
simpleTbl.appendChild(str);
explaininfo["rows"].forEach((row) => {
treePre.appendChild(document.createTextNode(row["id"] + "\n"));
outputDiv.appendChild(document.createElement("hr"));
outputDiv.appendChild(document.createTextNode("Going to try and explain this row:"));
let t = document.createElement("table");
t.className = "explainrow";
let rt = document.createElement("tr");
let rd = document.createElement("tr");
for (let [col, val] of Object.entries(row)) {
let c = document.createElement("td");
c.appendChild(document.createTextNode(col));
rt.appendChild(c);
c = document.createElement("td");
let pre = document.createElement("pre");
if (["execution info", "operator info"].includes(col)) {
pre.appendChild(document.createTextNode(formatStruct(val)));
} else if (col == "id") {
pre.appendChild(document.createTextNode( < string > val))
} else {
pre.appendChild(document.createTextNode(formatInfo(val)));
}
c.appendChild(pre);
rd.appendChild(c);
}
t.appendChild(rt);
simpleTbl.appendChild(rd.cloneNode(true))
rd.firstChild.firstChild.textContent = rd.firstChild.firstChild.textContent.trim()
t.appendChild(rd);
outputDiv.appendChild(t);
if (row["task"].trim() == "cop[tikv]") {
outputDiv.appendChild(document.createTextNode("Using coprocessor for TiKV row based storage."));
outputDiv.appendChild(document.createElement("br"));
let link = document.createElement("a");
link.appendChild(document.createTextNode("For more info: TiDB Docs: TiKV Coprocessor"));
link.href = "https://docs.pingcap.com/tidb/stable/tikv-overview#tikv-coprocessor";
outputDiv.appendChild(link);
outputDiv.appendChild(document.createElement("br"));
}
if (row["task"].trim() == "cop[tiflash]") {
outputDiv.appendChild(document.createTextNode("Using distributed coprocessor for TiFlash column based storage."));
outputDiv.appendChild(document.createElement("br"));
let link = document.createElement("a");
link.appendChild(document.createTextNode("For more info: TiFlash Overview"));
link.href = "https://docs.pingcap.com/tidb/stable/tiflash-overview";
outputDiv.appendChild(link);
outputDiv.appendChild(document.createElement("br"));
}
if (row["task"].trim() == "mpp[tiflash]") {
outputDiv.appendChild(document.createTextNode("Using distributed MPP for TiFlash column based storage."));
outputDiv.appendChild(document.createElement("br"));
let link = document.createElement("a");
link.appendChild(document.createTextNode("For more info: TiFlash Overview"));
link.href = "https://docs.pingcap.com/tidb/stable/tiflash-overview";
outputDiv.appendChild(link);
outputDiv.appendChild(document.createElement("br"));
link = document.createElement("a");
link.appendChild(document.createTextNode("For more info: TiFlash MPP mode"));
link.href = "https://docs.pingcap.com/tidb/stable/use-tiflash#use-the-mpp-mode";
outputDiv.appendChild(link);
outputDiv.appendChild(document.createElement("br"));
}
if (row["operator info"].match(/stats:pseudo/)) {
outputDiv.appendChild(document.createTextNode("Using pseudo statistics, consider running ANALYZE TABLE."));
outputDiv.appendChild(document.createElement("br"));
}
// https://docs.pingcap.com/tidb/dev/explain-overview#operator-overview
// https://docs.pingcap.com/tidb/dev/choose-index#operators-for-accessing-tables
let operator = row["id"].match(/([A-Z].*)_/)[1];
let operatorAdv;
let operatorURL;
switch (operator) {
case "TableDual":
operatorAdv = "Virtual single row table.";
break;
case "TableFullScan":
operatorAdv = "Full table scan, consider adding an index.";
break;
case "TableRangeScan":
operatorAdv = "Scans a range of the table.";
break;
case "TableReader":
operatorAdv = "Aggregates the data obtained by the underlying operators like TableFullScan or TableRangeScan in TiKV.";
break;
case "TableRowIDScan":
operatorAdv = "Scans the table data based on the RowID. Usually follows an index read operation to retrieve the matching data rows.";
break;
case "IndexFullScan":
operatorAdv = "Scans the full index, rather than the table data.";
break;
case "IndexRangeScan":
operatorAdv = "Scans a range of the index.";
break;
case "HashJoin":
operatorURL = "https://docs.pingcap.com/tidb/dev/explain-joins#hash-join";
break;
case "ExchangeSender":
operatorURL = "https://docs.pingcap.com/tidb/dev/explain-mpp#exchange-operators";
break;
case "HashAgg":
operatorURL = "https://docs.pingcap.com/tidb/dev/explain-aggregation#hash-aggregation";
break;
case "Projection":
operatorAdv = "Maps expression value(s) to select list.";
break;
case "StreamAgg":
operatorURL = "https://docs.pingcap.com/tidb/dev/explain-aggregation#stream-aggregation";
break;
default:
console.log("No advise available for '" + operator + "' operator");
}
if (operatorAdv) {
outputDiv.appendChild(document.createTextNode("Info for the \"" + operator + "\" operator: " + operatorAdv));
outputDiv.appendChild(document.createElement("br"));
}
if (operatorURL) {
let link = document.createElement("a");
link.appendChild(document.createTextNode("More info on the " + operator + " operator can be found here."));
link.href = operatorURL;
outputDiv.appendChild(link);
}
});
}
function checkExplain() {
let explaininfo = {
"columnstarts": [],
"columnnames": [],
"rows": [],
};
let hasColumnInfo = false;
let hasColumnNames = false;
let rowNumber = 0;
let expText = ( < HTMLTextAreaElement > document.getElementById("explaintext")).value;
let expLines = expText.split(/\r?\n/);
if (expLines[0].match("\t?id *\ttask")) {
expLines.forEach((expLine) => {
if (!hasColumnInfo) { // header
hasColumnInfo = true;
explaininfo.columnnames = expLine.split("\t").map(x => x.trim());
explaininfo.columnnames.shift()
} else {
let coldata = expLine.split("\t")
coldata.shift()
explaininfo.rows[rowNumber] = {}
for (const [index, element] of coldata.entries()) {
explaininfo.rows[rowNumber][explaininfo.columnnames[index]] = element;
}
rowNumber++;
}
});
} else { // MySQL Client Output
expLines.forEach((expLine) => {
if ((expLine[0] == "+") && !hasColumnInfo) {
hasColumnInfo = true;
for (let i = 0; i < expLine.length; i++) {
if (expLine[i] == "+") {
explaininfo["columnstarts"].push(i);
}
}
} else if ((expLine[0] == "|") && hasColumnInfo) {
if (!hasColumnNames) {
hasColumnNames = true;
explaininfo["columnnames"] = lineToArray(expLine, explaininfo["columnstarts"]).map(
x => x.trim()
);
} else {
explaininfo["rows"][rowNumber] = lineToObj(expLine, explaininfo);
rowNumber++;
}
}
});
}
console.log(explaininfo);
explainExplain(explaininfo);
}