Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: extract affected rows from mysql query plan (#5812)

p0ny21cc37bdfa1c
diff --git a/backend/plugin/advisor/mysql/advisor_insert_row_limit.go b/backend/plugin/advisor/mysql/advisor_insert_row_limit.go
index 192395274a..d43cf89e90 100644
--- a/backend/plugin/advisor/mysql/advisor_insert_row_limit.go
+++ b/backend/plugin/advisor/mysql/advisor_insert_row_limit.go
@@ -145,6 +145,21 @@ func getInsertRows(res []any) (int64, error) {
if !ok {
return 0, errors.Errorf("expected []any but got %t", res[2])
}
+ if len(rowList) < 1 {
+ return 0, errors.Errorf("not found any data")
+ }
+
+ // MySQL EXPLAIN statement result has 12 columns.
+ // the column 9 is the data 'rows'.
+ // the first not-NULL value of column 9 is the affected rows count.
+ //
+ // mysql> explain delete from td;
+ // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
+ // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+ // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
+ // | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
+ // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
+ //
// mysql> explain insert into td select * from td;
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
// | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
@@ -152,31 +167,32 @@ func getInsertRows(res []any) (int64, error) {
// | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
// | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- if len(rowList) < 2 {
- return 0, errors.Errorf("not found any data")
- }
- // We need the row 2.
- rowTwo, ok := rowList[1].([]any)
- if !ok {
- return 0, errors.Errorf("expected []any but got %t", rowList[0])
- }
- // MySQL EXPLAIN statement result has 12 columns.
- if len(rowTwo) != 12 {
- return 0, errors.Errorf("expected 12 but got %d", len(rowTwo))
- }
- // the column 9 is the data 'rows'.
- switch rows := rowTwo[9].(type) {
- case int:
- return int64(rows), nil
- case int64:
- return rows, nil
- case string:
- v, err := strconv.ParseInt(rows, 10, 64)
- if err != nil {
- return 0, errors.Errorf("expected int or int64 but got string(%s)", rows)
+
+ for _, rowAny := range rowList {
+ row, ok := rowAny.([]any)
+ if !ok {
+ return 0, errors.Errorf("expected []any but got %t", row)
+ }
+ if len(row) != 12 {
+ return 0, errors.Errorf("expected 12 but got %d", len(row))
+ }
+ switch col := row[9].(type) {
+ case int:
+ return int64(col), nil
+ case int32:
+ return int64(col), nil
+ case int64:
+ return col, nil
+ case string:
+ v, err := strconv.ParseInt(col, 10, 64)
+ if err != nil {
+ return 0, errors.Errorf("expected int or int64 but got string(%s)", col)
+ }
+ return v, nil
+ default:
+ continue
}
- return v, nil
- default:
- return 0, errors.Errorf("expected int or in64 but got %t", rowTwo[9])
}
+
+ return 0, errors.Errorf("failed to extract rows from query plan")
}
diff --git a/backend/plugin/advisor/mysql/advisor_statement_affected_row_limit.go b/backend/plugin/advisor/mysql/advisor_statement_affected_row_limit.go
index 34ca158dd9..41fe21cab7 100644
--- a/backend/plugin/advisor/mysql/advisor_statement_affected_row_limit.go
+++ b/backend/plugin/advisor/mysql/advisor_statement_affected_row_limit.go
@@ -137,11 +137,10 @@ func getRows(res []any) (int64, error) {
if len(rowList) < 1 {
return 0, errors.Errorf("not found any data")
}
- rowOne, ok := rowList[0].([]any)
- if !ok {
- return 0, errors.Errorf("expected []any but got %t", rowList[0])
- }
+
// MySQL EXPLAIN statement result has 12 columns.
+ // the column 9 is the data 'rows'.
+ // the first not-NULL value of column 9 is the affected rows count.
//
// mysql> explain delete from td;
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
@@ -149,22 +148,40 @@ func getRows(res []any) (int64, error) {
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
// | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- if len(rowOne) != 12 {
- return 0, errors.Errorf("expected 12 but got %d", len(rowOne))
- }
- // the column 9 is the data 'rows'.
- switch rows := rowOne[9].(type) {
- case int:
- return int64(rows), nil
- case int64:
- return rows, nil
- case string:
- v, err := strconv.ParseInt(rows, 10, 64)
- if err != nil {
- return 0, errors.Errorf("expected int or int64 but got string(%s)", rows)
+ //
+ // mysql> explain insert into td select * from td;
+ // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
+ // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+ // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
+ // | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+ // | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
+ // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
+
+ for _, rowAny := range rowList {
+ row, ok := rowAny.([]any)
+ if !ok {
+ return 0, errors.Errorf("expected []any but got %t", row)
+ }
+ if len(row) != 12 {
+ return 0, errors.Errorf("expected 12 but got %d", len(row))
+ }
+ switch col := row[9].(type) {
+ case int:
+ return int64(col), nil
+ case int32:
+ return int64(col), nil
+ case int64:
+ return col, nil
+ case string:
+ v, err := strconv.ParseInt(col, 10, 64)
+ if err != nil {
+ return 0, errors.Errorf("expected int or int64 but got string(%s)", col)
+ }
+ return v, nil
+ default:
+ continue
}
- return v, nil
- default:
- return 0, errors.Errorf("expected int or int64 but got %t", rowOne[9])
}
+
+ return 0, errors.Errorf("failed to extract rows from query plan")
}
diff --git a/backend/runner/taskcheck/statement_affected_rows_report_executor.go b/backend/runner/taskcheck/statement_affected_rows_report_executor.go
index dc44926e25..06590cea36 100644
--- a/backend/runner/taskcheck/statement_affected_rows_report_executor.go
+++ b/backend/runner/taskcheck/statement_affected_rows_report_executor.go
@@ -320,7 +320,7 @@ func getInsertAffectedRowsForMysql(ctx context.Context, sqlDB *sql.DB, node *tid
if err != nil {
return 0, err
}
- rowCount, err := getInsertAffectedRowsCountForMysql(res)
+ rowCount, err := getAffectedRowsCountForMysql(res)
if err != nil {
return 0, errors.Wrapf(err, "failed to get insert affected rows count for mysql, res %+v", res)
}
@@ -332,14 +332,14 @@ func getUpdateOrDeleteAffectedRowsForMysql(ctx context.Context, sqlDB *sql.DB, n
if err != nil {
return 0, err
}
- rowCount, err := getUpdateOrDeleteAffectedRowsCountForMysql(res)
+ rowCount, err := getAffectedRowsCountForMysql(res)
if err != nil {
return 0, errors.Wrapf(err, "failed to get update or delete affected rows count for mysql, res %+v", res)
}
return rowCount, nil
}
-func getUpdateOrDeleteAffectedRowsCountForMysql(res []any) (int64, error) {
+func getAffectedRowsCountForMysql(res []any) (int64, error) {
// the res struct is []any{columnName, columnTable, rowDataList}
if len(res) != 3 {
return 0, errors.Errorf("expected 3 but got %d", len(res))
@@ -351,11 +351,10 @@ func getUpdateOrDeleteAffectedRowsCountForMysql(res []any) (int64, error) {
if len(rowList) < 1 {
return 0, errors.Errorf("not found any data")
}
- rowOne, ok := rowList[0].([]any)
- if !ok {
- return 0, errors.Errorf("expected []any but got %t", rowList[0])
- }
+
// MySQL EXPLAIN statement result has 12 columns.
+ // the column 9 is the data 'rows'.
+ // the first not-NULL value of column 9 is the affected rows count.
//
// mysql> explain delete from td;
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
@@ -363,35 +362,7 @@ func getUpdateOrDeleteAffectedRowsCountForMysql(res []any) (int64, error) {
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
// | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- if len(rowOne) != 12 {
- return 0, errors.Errorf("expected 12 but got %d", len(rowOne))
- }
- // the column 9 is the data 'rows'.
- switch rows := rowOne[9].(type) {
- case int:
- return int64(rows), nil
- case int64:
- return rows, nil
- case string:
- v, err := strconv.ParseInt(rows, 10, 64)
- if err != nil {
- return 0, errors.Errorf("expected int or int64 but got string(%s)", rows)
- }
- return v, nil
- default:
- return 0, errors.Errorf("expected int or int64 but got %t", rowOne[9])
- }
-}
-
-func getInsertAffectedRowsCountForMysql(res []any) (int64, error) {
- // the res struct is []any{columnName, columnTable, rowDataList}
- if len(res) != 3 {
- return 0, errors.Errorf("expected 3 but got %d", len(res))
- }
- rowList, ok := res[2].([]any)
- if !ok {
- return 0, errors.Errorf("expected []any but got %t", res[2])
- }
+ //
// mysql> explain insert into td select * from td;
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
// | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
@@ -399,33 +370,34 @@ func getInsertAffectedRowsCountForMysql(res []any) (int64, error) {
// | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
// | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
// +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- if len(rowList) < 2 {
- return 0, errors.Errorf("not found any data")
- }
- // We need the row 2.
- rowTwo, ok := rowList[1].([]any)
- if !ok {
- return 0, errors.Errorf("expected []any but got %t", rowList[0])
- }
- // MySQL EXPLAIN statement result has 12 columns.
- if len(rowTwo) != 12 {
- return 0, errors.Errorf("expected 12 but got %d", len(rowTwo))
- }
- // the column 9 is the data 'rows'.
- switch rows := rowTwo[9].(type) {
- case int:
- return int64(rows), nil
- case int64:
- return rows, nil
- case string:
- v, err := strconv.ParseInt(rows, 10, 64)
- if err != nil {
- return 0, errors.Errorf("expected int or int64 but got string(%s)", rows)
+
+ for _, rowAny := range rowList {
+ row, ok := rowAny.([]any)
+ if !ok {
+ return 0, errors.Errorf("expected []any but got %t", row)
+ }
+ if len(row) != 12 {
+ return 0, errors.Errorf("expected 12 but got %d", len(row))
+ }
+ switch col := row[9].(type) {
+ case int:
+ return int64(col), nil
+ case int32:
+ return int64(col), nil
+ case int64:
+ return col, nil
+ case string:
+ v, err := strconv.ParseInt(col, 10, 64)
+ if err != nil {
+ return 0, errors.Errorf("expected int or int64 but got string(%s)", col)
+ }
+ return v, nil
+ default:
+ continue
}
- return v, nil
- default:
- return 0, errors.Errorf("expected int or in64 but got %t", rowTwo[9])
}
+
+ return 0, errors.Errorf("failed to extract rows from query plan")
}
// Query runs the EXPLAIN or SELECT statements for advisors.
diff --git a/backend/runner/taskcheck/statement_affected_rows_report_executor_test.go b/backend/runner/taskcheck/statement_affected_rows_report_executor_test.go
new file mode 100644
index 0000000000..7c5e3c2350
--- /dev/null
+++ b/backend/runner/taskcheck/statement_affected_rows_report_executor_test.go
@@ -0,0 +1,66 @@
+package taskcheck
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestGetAffectedRowsCountForMysql(t *testing.T) {
+ tests := []struct {
+ name string
+ want int64
+ res []any
+ }{
+ {
+ name: "delete simple",
+ want: 123,
+ res: []any{
+ []string{
+ "id",
+ "select_type",
+ "table",
+ "partitions",
+ "type",
+ "possible_keys",
+ "key",
+ "key_len",
+ "ref",
+ "rows",
+ "filtered",
+ "Extra",
+ },
+ []string{
+ "UNSIGNED BIGINT",
+ "VARCHAR",
+ "VARCHAR",
+ "MEDIUMTEXT",
+ "VARCHAR",
+ "VARCHAR",
+ "VARCHAR",
+ "VARCHAR",
+ "VARCHAR",
+ "UNSIGNED BIGINT",
+ "DOUBLE",
+ "VARCHAR",
+ },
+ []any{
+ []any{1, "SIMPLE", "<subquery2>", nil, "ALL", nil, nil, nil, nil, nil, 100.0, "Using temporary"},
+ []any{1, "DELETE", "ha", nil, "ALL", nil, nil, nil, nil, 123, 100.0, "Using where"},
+ []any{2, "MATERIALIZED", "haha", nil, "ALL", nil, nil, nil, nil, 5423, 100.0, "Using where"},
+ []any{2, "MATERIALIZED", "hahaha", nil, "ALL", nil, nil, nil, nil, 54321, 100.0, "Using where"},
+ []any{2, "MATERIALIZED", "hahahaha", nil, "ALL", nil, nil, nil, nil, 856, 100.0, "Range checked for each record (index map: 0x2)"},
+ },
+ },
+ },
+ }
+
+ a := require.New(t)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := getAffectedRowsCountForMysql(tc.res)
+ a.NoError(err)
+ a.Equal(tc.want, got)
+ })
+ }
+}