Skip to content

Commit 1bd81f5

Browse files
author
Haibin Xie
committed
use batch insert
1 parent 700d749 commit 1bd81f5

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

statistics/update.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"math"
1919
"strconv"
20+
"strings"
2021
"sync"
2122
"time"
2223

@@ -365,16 +366,16 @@ func (h *Handle) dumpTableStatColSizeToKV(id int64, delta variable.TableDelta) (
365366
}
366367
}
367368
}
369+
values := make([]string, 0, len(delta.ColSize))
368370
for key, val := range delta.ColSize {
369371
if val == 0 {
370372
continue
371373
}
372-
sql := fmt.Sprintf("insert into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count) values (%d, 0, %d, 0) on duplicate key update tot_col_size = tot_col_size + %d", id, key, val)
373-
_, err = exec.Execute(ctx, sql)
374-
if err != nil {
375-
return
376-
}
374+
values = append(values, fmt.Sprintf("(%d, 0, %d, 0, %d)", id, key, val))
377375
}
376+
sql = fmt.Sprintf("insert into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, tot_col_size) "+
377+
"values %s on duplicate key update tot_col_size = tot_col_size + values(tot_col_size)", strings.Join(values, ","))
378+
_, err = exec.Execute(ctx, sql)
378379
return
379380
}
380381

0 commit comments

Comments
 (0)