提交 552ccf9a 编写于 作者: martianzhang's avatar martianzhang

fix explain result with multi rows error

上级 442f4147
...@@ -66,9 +66,9 @@ var ExplainType = map[string]int{ ...@@ -66,9 +66,9 @@ var ExplainType = map[string]int{
type ExplainInfo struct { type ExplainInfo struct {
SQL string SQL string
ExplainFormat int ExplainFormat int
ExplainRows []*ExplainRow ExplainRows []ExplainRow
ExplainJSON *ExplainJSON ExplainJSON *ExplainJSON
Warnings []*ExplainWarning Warnings []ExplainWarning
QueryCost float64 QueryCost float64
} }
...@@ -419,12 +419,12 @@ func findTablesInJSON(explainJSON string, depth int) { ...@@ -419,12 +419,12 @@ func findTablesInJSON(explainJSON string, depth int) {
} }
// FormatJSONIntoTraditional 将JSON形式转换为TRADITIONAL形式,方便前端展现 // FormatJSONIntoTraditional 将JSON形式转换为TRADITIONAL形式,方便前端展现
func FormatJSONIntoTraditional(explainJSON string) []*ExplainRow { func FormatJSONIntoTraditional(explainJSON string) []ExplainRow {
// 查找JSON中的所有ExplainJSONTable // 查找JSON中的所有ExplainJSONTable
explainJSONTables = []*ExplainJSONTable{} explainJSONTables = []*ExplainJSONTable{}
findTablesInJSON(explainJSON, 0) findTablesInJSON(explainJSON, 0)
var explainRows []*ExplainRow var explainRows []ExplainRow
id := -1 id := -1
for _, table := range explainJSONTables { for _, table := range explainJSONTables {
keyLen := table.KeyLength keyLen := table.KeyLength
...@@ -435,7 +435,7 @@ func FormatJSONIntoTraditional(explainJSON string) []*ExplainRow { ...@@ -435,7 +435,7 @@ func FormatJSONIntoTraditional(explainJSON string) []*ExplainRow {
if filtered > 100.00 { if filtered > 100.00 {
filtered = 100.00 filtered = 100.00
} }
explainRows = append(explainRows, &ExplainRow{ explainRows = append(explainRows, ExplainRow{
ID: id + 1, ID: id + 1,
SelectType: "", SelectType: "",
TableName: table.TableName, TableName: table.TableName,
...@@ -456,7 +456,7 @@ func FormatJSONIntoTraditional(explainJSON string) []*ExplainRow { ...@@ -456,7 +456,7 @@ func FormatJSONIntoTraditional(explainJSON string) []*ExplainRow {
// ConvertExplainJSON2Row 将 JSON 格式转成 ROW 格式,为方便统一做优化建议 // ConvertExplainJSON2Row 将 JSON 格式转成 ROW 格式,为方便统一做优化建议
// 但是会损失一些 JSON 特有的分析结果 // 但是会损失一些 JSON 特有的分析结果
func ConvertExplainJSON2Row(explainJSON *ExplainJSON) []*ExplainRow { func ConvertExplainJSON2Row(explainJSON *ExplainJSON) []ExplainRow {
buf, err := json.Marshal(explainJSON) buf, err := json.Marshal(explainJSON)
if err != nil { if err != nil {
return nil return nil
...@@ -751,7 +751,7 @@ func ParseExplainText(content string) (exp *ExplainInfo, err error) { ...@@ -751,7 +751,7 @@ func ParseExplainText(content string) (exp *ExplainInfo, err error) {
} }
// 解析文本形式传统形式Explain信息 // 解析文本形式传统形式Explain信息
func parseTraditionalExplainText(content string) (explainRows []*ExplainRow, err error) { func parseTraditionalExplainText(content string) (explainRows []ExplainRow, err error) {
LS := regexp.MustCompile(`^\+`) // 华丽的分隔线:) LS := regexp.MustCompile(`^\+`) // 华丽的分隔线:)
// 格式正确性检查 // 格式正确性检查
...@@ -827,7 +827,7 @@ func parseTraditionalExplainText(content string) (explainRows []*ExplainRow, err ...@@ -827,7 +827,7 @@ func parseTraditionalExplainText(content string) (explainRows []*ExplainRow, err
} }
// 拼接结构体 // 拼接结构体
explainRows = append(explainRows, &ExplainRow{ explainRows = append(explainRows, ExplainRow{
ID: id, ID: id,
SelectType: colsMap["select_type"], SelectType: colsMap["select_type"],
TableName: colsMap["table"], TableName: colsMap["table"],
...@@ -847,9 +847,9 @@ func parseTraditionalExplainText(content string) (explainRows []*ExplainRow, err ...@@ -847,9 +847,9 @@ func parseTraditionalExplainText(content string) (explainRows []*ExplainRow, err
} }
// 解析文本形式竖排版 Explain信息 // 解析文本形式竖排版 Explain信息
func parseVerticalExplainText(content string) (explainRows []*ExplainRow, err error) { func parseVerticalExplainText(content string) (explainRows []ExplainRow, err error) {
var lines []string var lines []string
explainRow := &ExplainRow{ explainRow := ExplainRow{
Partitions: "NULL", Partitions: "NULL",
Filtered: 0.00, Filtered: 0.00,
} }
...@@ -961,7 +961,7 @@ func ParseExplainResult(res QueryResult, formatType int) (exp *ExplainInfo, err ...@@ -961,7 +961,7 @@ func ParseExplainResult(res QueryResult, formatType int) (exp *ExplainInfo, err
// Different MySQL version has different columns define // Different MySQL version has different columns define
var selectType, table, partitions, accessType, possibleKeys, key, keyLen, ref, extra []byte var selectType, table, partitions, accessType, possibleKeys, key, keyLen, ref, extra []byte
expRow := &ExplainRow{} expRow := ExplainRow{}
explainFields := make([]interface{}, 0) explainFields := make([]interface{}, 0)
fields := map[string]interface{}{ fields := map[string]interface{}{
"id": &expRow.ID, "id": &expRow.ID,
...@@ -990,7 +990,7 @@ func ParseExplainResult(res QueryResult, formatType int) (exp *ExplainInfo, err ...@@ -990,7 +990,7 @@ func ParseExplainResult(res QueryResult, formatType int) (exp *ExplainInfo, err
} }
// 补全 ExplainRows // 补全 ExplainRows
var explainRows []*ExplainRow var explainRows []ExplainRow
for res.Rows.Next() { for res.Rows.Next() {
err := res.Rows.Scan(explainFields...) err := res.Rows.Scan(explainFields...)
if err != nil { if err != nil {
...@@ -1020,8 +1020,8 @@ func ParseExplainResult(res QueryResult, formatType int) (exp *ExplainInfo, err ...@@ -1020,8 +1020,8 @@ func ParseExplainResult(res QueryResult, formatType int) (exp *ExplainInfo, err
// check explain warning info // check explain warning info
if common.Config.ShowWarnings { if common.Config.ShowWarnings {
for res.Warning.Next() { for res.Warning.Next() {
var expWarning *ExplainWarning var expWarning ExplainWarning
err = res.Warning.Scan(expWarning.Level, expWarning.Code, expWarning.Message) err = res.Warning.Scan(&expWarning.Level, &expWarning.Code, &expWarning.Message)
if err != nil { if err != nil {
break break
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册