Skip to content

Commit

Permalink
Merge pull request #2859 from actiontech/2587
Browse files Browse the repository at this point in the history
rule DMLCheckScanRowsDesc cancel checking of type of sql explain
  • Loading branch information
BugsGuru authored Jan 7, 2025
2 parents 966b5a4 + b98ed81 commit 796cbd0
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
6 changes: 3 additions & 3 deletions sqle/driver/mysql/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6494,7 +6494,7 @@ func TestDMLCheckScanRows(t *testing.T) {
inspect3 := NewMockInspect(e)
handler.ExpectQuery(regexp.QuoteMeta("select * from exist_tb_2 where v1 = 'a'")).
WillReturnRows(sqlmock.NewRows([]string{"rows", "type"}).AddRow("100000000000", "const"))
runSingleRuleInspectCase(rule, t, "", inspect3, "select * from exist_tb_2 where v1 = 'a'", newTestResult())
runSingleRuleInspectCase(rule, t, "", inspect3, "select * from exist_tb_2 where v1 = 'a'", newTestResult().addResult(rulepkg.DMLCheckScanRows))

inspect4 := NewMockInspect(e)
handler.ExpectQuery(regexp.QuoteMeta("select * from exist_tb_2 where v1 in (select v2 from exist_tb_1)")).
Expand All @@ -6519,7 +6519,7 @@ func TestDMLCheckScanRows(t *testing.T) {
inspect8 := NewMockInspect(e)
handler.ExpectQuery(regexp.QuoteMeta("update exist_tb_2 set v1=1 where v2=1")).
WillReturnRows(sqlmock.NewRows([]string{"rows", "type"}).AddRow("100000000", "range"))
runSingleRuleInspectCase(rule, t, "", inspect8, "update exist_tb_2 set v1=1 where v2=1", newTestResult())
runSingleRuleInspectCase(rule, t, "", inspect8, "update exist_tb_2 set v1=1 where v2=1", newTestResult().addResult(rulepkg.DMLCheckScanRows))

inspect9 := NewMockInspect(e)
handler.ExpectQuery(regexp.QuoteMeta("update exist_tb_2 set v1=1 where v2=1")).
Expand All @@ -6534,7 +6534,7 @@ func TestDMLCheckScanRows(t *testing.T) {
inspect11 := NewMockInspect(e)
handler.ExpectQuery(regexp.QuoteMeta("delete from exist_tb_2 where v1=1")).
WillReturnRows(sqlmock.NewRows([]string{"rows", "type"}).AddRow("100000000", "range"))
runSingleRuleInspectCase(rule, t, "", inspect11, "delete from exist_tb_2 where v1=1", newTestResult())
runSingleRuleInspectCase(rule, t, "", inspect11, "delete from exist_tb_2 where v1=1", newTestResult().addResult(rulepkg.DMLCheckScanRows))
}

// TODO
Expand Down
4 changes: 2 additions & 2 deletions sqle/driver/mysql/plocale/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ DMLCheckSameTableJoinedMultipleTimesAnnotation = "If a single table is queried m
DMLCheckSameTableJoinedMultipleTimesDesc = "It is not recommended to join the same table multiple times"
DMLCheckSameTableJoinedMultipleTimesMessage = "Table %v is joined multiple times"
DMLCheckScanRowsAnnotation = "The filter condition must include the primary key or index to reduce the time complexity of the database query and improve query efficiency."
DMLCheckScanRowsDesc = "The number of rows scanned exceeds the threshold, the filter condition must include the primary key or index"
DMLCheckScanRowsMessage = "The number of rows scanned exceeds the threshold, the filter condition must include the primary key or index"
DMLCheckScanRowsDesc = "The number of rows scanned exceeds the threshold, please check the index configuration."
DMLCheckScanRowsMessage = "The number of rows scanned exceeds the threshold, please check the index configuration."
DMLCheckScanRowsParams1 = "Number of rows scanned (ten thousand)"
DMLCheckSelectForUpdateAnnotation = "SELECT FOR UPDATE will add an exclusive lock to each row of data in the query result set. Other threads will be blocked from updating and deleting the record, which can easily lead to a large number of lock waits in the database under high concurrency, affecting database query performance."
DMLCheckSelectForUpdateDesc = "It is not recommended to use SELECT FOR UPDATE"
Expand Down
4 changes: 2 additions & 2 deletions sqle/driver/mysql/plocale/active.zh.toml
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ DMLCheckSameTableJoinedMultipleTimesAnnotation = "如果对单表查询多次,
DMLCheckSameTableJoinedMultipleTimesDesc = "不建议对同一张表连接多次"
DMLCheckSameTableJoinedMultipleTimesMessage = "表%v被连接多次"
DMLCheckScanRowsAnnotation = "筛选条件必须带上主键或索引可降低数据库查询的时间复杂度,提高查询效率。"
DMLCheckScanRowsDesc = "扫描行数超过阈值,筛选条件必须带上主键或者索引"
DMLCheckScanRowsMessage = "扫描行数超过阈值,筛选条件必须带上主键或者索引"
DMLCheckScanRowsDesc = "扫描行数超过阈值,请检查索引配置"
DMLCheckScanRowsMessage = "扫描行数超过阈值,请检查索引配置"
DMLCheckScanRowsParams1 = "扫描行数量(万)"
DMLCheckSelectForUpdateAnnotation = "SELECT FOR UPDATE 会对查询结果集中每行数据都添加排他锁,其他线程对该记录的更新与删除操作都会阻塞,在高并发下,容易造成数据库大量锁等待,影响数据库查询性能"
DMLCheckSelectForUpdateDesc = "不建议使用SELECT FOR UPDATE"
Expand Down
4 changes: 2 additions & 2 deletions sqle/driver/mysql/plocale/message_zh.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,9 @@ var (
DMLCheckSelectRowsAnnotation = &i18n.Message{ID: "DMLCheckSelectRowsAnnotation", Other: "筛选条件必须带上主键或索引可提高查询性能和减少全表扫描的成本。"}
DMLCheckSelectRowsMessage = &i18n.Message{ID: "DMLCheckSelectRowsMessage", Other: "查询数据量超过阈值,筛选条件必须带上主键或者索引"}
DMLCheckSelectRowsParams1 = &i18n.Message{ID: "DMLCheckSelectRowsParams1", Other: "查询数据量(万)"}
DMLCheckScanRowsDesc = &i18n.Message{ID: "DMLCheckScanRowsDesc", Other: "扫描行数超过阈值,筛选条件必须带上主键或者索引"}
DMLCheckScanRowsDesc = &i18n.Message{ID: "DMLCheckScanRowsDesc", Other: "扫描行数超过阈值,请检查索引配置"}
DMLCheckScanRowsAnnotation = &i18n.Message{ID: "DMLCheckScanRowsAnnotation", Other: "筛选条件必须带上主键或索引可降低数据库查询的时间复杂度,提高查询效率。"}
DMLCheckScanRowsMessage = &i18n.Message{ID: "DMLCheckScanRowsMessage", Other: "扫描行数超过阈值,筛选条件必须带上主键或者索引"}
DMLCheckScanRowsMessage = &i18n.Message{ID: "DMLCheckScanRowsMessage", Other: "扫描行数超过阈值,请检查索引配置"}
DMLCheckScanRowsParams1 = &i18n.Message{ID: "DMLCheckScanRowsParams1", Other: "扫描行数量(万)"}
DMLMustUseLeftMostPrefixDesc = &i18n.Message{ID: "DMLMustUseLeftMostPrefixDesc", Other: "使用联合索引时,必须使用联合索引的首字段"}
DMLMustUseLeftMostPrefixAnnotation = &i18n.Message{ID: "DMLMustUseLeftMostPrefixAnnotation", Other: "使用联合索引时,不包含首字段会导致联合索引失效"}
Expand Down
2 changes: 0 additions & 2 deletions sqle/driver/mysql/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5203,10 +5203,8 @@ func checkScanRows(input *RuleHandlerInput) error {
}
for _, record := range epRecords {
if record.Rows > int64(max)*int64(TenThousand) {
if record.Type == executor.ExplainRecordAccessTypeIndex || record.Type == executor.ExplainRecordAccessTypeAll {
addResult(input.Res, input.Rule, input.Rule.Name)
break
}
}
}
return nil
Expand Down

0 comments on commit 796cbd0

Please sign in to comment.