Skip to content

Commit 75160ff

Browse files
committed
tweak query inspector
Signed-off-by: neo <1100909+neowu@users.noreply.github.com>
1 parent 274f7ee commit 75160ff

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

core-ng/src/main/java/core/framework/internal/db/inspector/MySQLQueryAnalyzer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public QueryPlan explain(String sql, Object[] params) {
3030

3131
boolean isEfficient(Explain explain) {
3232
if ("ALL".equals(explain.type)) {
33-
if (explain.rows != null && explain.rows > 2000) {
33+
if (explain.rowsGreaterThan(2000)) {
3434
return false;
3535
}
3636
}
37-
if (explain.extra != null) {
38-
if (explain.extra.contains("Using temporary") || explain.extra.contains("Using filesort")) {
37+
if (explain.extra != null && (explain.extra.contains("Using temporary") || explain.extra.contains("Using filesort"))) {
38+
if (explain.rowsGreaterThan(10_000)) {
3939
return false;
4040
}
4141
}
@@ -76,6 +76,10 @@ static class Explain {
7676
Long rows;
7777
String filtered;
7878
String extra;
79+
80+
boolean rowsGreaterThan(long threshold) {
81+
return rows != null && rows > threshold;
82+
}
7983
}
8084

8185
static class ExplainRowMapper implements RowMapper<Explain> {

core-ng/src/test/java/core/framework/internal/db/inspector/MySQLQueryAnalyzerTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ void format() {
3131

3232
@Test
3333
void isEfficient() {
34-
var explain = parse("1|SIMPLE|t1||ALL|idx_col1_col2|idx_col1_col2|13|const|100||Using where; Using index; Using filesort");
34+
var explain = parse("1|SIMPLE|t1||ALL|idx_col1_col2|idx_col1_col2|13|const|3000||Using where; Using index; Using filesort");
3535
assertThat(analyzer.isEfficient(explain)).isFalse();
3636

3737
explain = parse("1|SIMPLE|t1||ALL|idx_col1_col2|idx_col1_col2|13|const|100||Using where; Using index");
3838
assertThat(analyzer.isEfficient(explain)).isTrue(); // scan less than 2000 rows
3939

4040
explain = parse("1|SIMPLE|daily_individual_stats||index_merge|PRIMARY,idx_customer_id|idx_customer_id,PRIMARY|149,149||1|100|Using intersect(idx_customer_id,PRIMARY); Using where");
4141
assertThat(analyzer.isEfficient(explain)).isTrue();
42+
43+
explain = parse("1|SIMPLE|role||ALL|||||24|100|Using filesort");
44+
assertThat(analyzer.isEfficient(explain)).isTrue();
45+
46+
explain = parse("1|SIMPLE|stat||range|PRIMARY,idx_customer_id|PRIMARY|3||7942|100.0|Using where; Using temporary; Using filesort");
47+
assertThat(analyzer.isEfficient(explain)).isTrue();
4248
}
4349

4450
// 1|SIMPLE|daily_individual_stats||index_merge|PRIMARY,idx_customer_id|idx_customer_id,PRIMARY|149,149||1|100|Using intersect(idx_customer_id,PRIMARY); Using where

0 commit comments

Comments
 (0)