Skip to content

Commit

Permalink
支持InnoDB引擎RowFormats行大小计算
Browse files Browse the repository at this point in the history
  • Loading branch information
xff committed Mar 12, 2024
1 parent ce8690d commit 1f5d488
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
30 changes: 19 additions & 11 deletions goinsight/internal/apps/inspect/controllers/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func VerifyTable(table string, db *DB) (error, string) {

// 获取DB变量
func GetDBVars(db *DB) (map[string]string, error) {
result, err := db.Query("show variables where Variable_name in ('innodb_large_prefix','version','character_set_database','innodb_default_row_format')")
result, err := db.Query(`show variables where Variable_name in ('innodb_large_prefix','version','character_set_database','innodb_default_row_format')`)
if err != nil {
return nil, err
}
Expand All @@ -105,24 +105,32 @@ func GetDBVars(db *DB) (map[string]string, error) {

// [map[Value:utf8 Variable_name:character_set_database] map[Value:5.7.35-log Variable_name:version]]
for _, row := range *result {
if strings.EqualFold(row["Variable_name"].(string), "version") {
data["dbVersion"] = row["Value"].(string)
variableName, ok := row["Variable_name"].(string)
if !ok {
return nil, fmt.Errorf("unexpected type for Variable_name")
}
if strings.EqualFold(row["Variable_name"].(string), "character_set_database") {
data["dbCharset"] = row["Value"].(string)

value, ok := row["Value"].(string)
if !ok {
return nil, fmt.Errorf("unexpected type for Value in row")
}
if strings.EqualFold(row["Variable_name"].(string), "innodb_large_prefix") {
switch row["Value"].(string) {

switch variableName {
case "version":
data["dbVersion"] = value
case "character_set_database":
data["dbCharset"] = value
case "innodb_large_prefix":
switch value {
case "0":
data["largePrefix"] = "OFF"
case "1":
data["largePrefix"] = "ON"
default:
data["largePrefix"] = strings.ToUpper(row["Value"].(string))
data["largePrefix"] = strings.ToUpper(value)
}
}
if strings.EqualFold(row["Variable_name"].(string), "innodb_default_row_format") {
data["innodbDefaultRowFormat"] = row["Value"].(string)
case "innodb_default_row_format":
data["innodbDefaultRowFormat"] = value
}
}
return data, nil
Expand Down
26 changes: 11 additions & 15 deletions goinsight/internal/apps/inspect/controllers/process/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package process

import (
"fmt"
"goInsight/global"
"goInsight/internal/apps/inspect/config"
"goInsight/internal/pkg/kv"
"goInsight/internal/pkg/utils"
Expand Down Expand Up @@ -351,24 +350,22 @@ type LargePrefix struct {
}

func (l *LargePrefix) Check(kv *kv.KVCache) error {
indexMaxLength := 767
const defaultLargePrefixLimit = 767
const LargePrefixLimit = 3072

dbVersion := kv.Get("dbVersion").(string)
versionIns := DbVersion{dbVersion}
if versionIns.IsTiDB() {
indexMaxLength = 3072
} else {
if versionIns.Int() > 80000 {
indexMaxLength = 3072
}
if versionIns.Int() > 50700 && kv.Get("largePrefix").(string) == "ON" {
indexMaxLength = 3072
}

indexMaxLength := defaultLargePrefixLimit

if versionIns.IsTiDB() || (versionIns.Int() > 80000) || (versionIns.Int() > 50700 && kv.Get("largePrefix").(string) == "ON") {
indexMaxLength = LargePrefixLimit
}
for _, i := range l.LargePrefixIndexColsMaps {

for _, indexMap := range l.LargePrefixIndexColsMaps {
// &{meta_cluster utf8 [{idx_datacenter [{datacenter 254 -1 128 } {cluster_domain 15 32 128 }]}]}
var maxSumLength int
for _, key := range i.Keys {
for _, key := range indexMap.Keys {
// 判断字符集,当列字符集为空,使用表的字符集
if len(key.Charset) == 0 {
key.Charset = l.Charset
Expand All @@ -381,9 +378,8 @@ func (l *LargePrefix) Check(kv *kv.KVCache) error {
maxSumLength += instDataBytes.Get(versionIns.Int())
}

global.App.Log.Debug(fmt.Sprintf("maxSumLength:%d, indexMaxLength:%d", maxSumLength, indexMaxLength))
if maxSumLength > indexMaxLength {
return fmt.Errorf("表`%s`的索引`%s`超出了innodb-large-prefix限制,当前索引长度为%d字节,最大限制为%d字节【例如可使用前缀索引,如:Field(length)】", l.Table, i.Name, maxSumLength, indexMaxLength)
return fmt.Errorf("表`%s`的索引`%s`超出了innodb-large-prefix限制,当前索引长度为%d字节,最大限制为%d字节【您可使用前缀索引,如:Column(length)】", l.Table, indexMap.Name, maxSumLength, indexMaxLength)
}
}
return nil
Expand Down
6 changes: 1 addition & 5 deletions goinsight/internal/apps/inspect/controllers/process/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package process

import (
"fmt"
"goInsight/global"
"goInsight/internal/pkg/kv"
"strings"

Expand Down Expand Up @@ -72,11 +71,8 @@ func (l *InnoDBRowSize) Check(kv *kv.KVCache) error {
maxSumRowsLength += instDataBytes.Get(versionIns.Int())
}
// 判断是否触发了行大小限制
msg := fmt.Sprintf("表`%s`触发了Row Size Limit,最大行大小为%d,当前为%d(表存储引擎为%s,行格式为%s)", l.Table, maxRowSize, maxSumRowsLength, l.Engine, rowFormat)
global.App.Log.Info(msg)

if maxSumRowsLength > maxRowSize {
return fmt.Errorf(msg)
return fmt.Errorf("表`%s`触发了Row Size Limit,最大行大小为%d,当前为%d(表存储引擎为%s,行格式为%s)", l.Table, maxRowSize, maxSumRowsLength, l.Engine, rowFormat)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,16 +529,16 @@ func (c *TraverseCreateTableDisabledIndexes) Leave(in ast.Node) (ast.Node, bool)

// TraverseCreateTableInnodbLargePrefix
type TraverseCreateTableInnodbLargePrefix struct {
LargePrefix process.LargePrefix
process.LargePrefix
}

func (c *TraverseCreateTableInnodbLargePrefix) Enter(in ast.Node) (ast.Node, bool) {
if stmt, ok := in.(*ast.CreateTableStmt); ok {
c.LargePrefix.Table = stmt.Table.Name.String()
c.Table = stmt.Table.Name.String()
for _, node := range stmt.Options {
switch node.Tp {
case ast.TableOptionCharset:
c.LargePrefix.Charset = node.StrValue
c.Charset = node.StrValue
}
}
var LargePrefixIndexColsMaps []process.LargePrefixIndexColsMap
Expand Down Expand Up @@ -573,7 +573,7 @@ func (c *TraverseCreateTableInnodbLargePrefix) Enter(in ast.Node) (ast.Node, boo
}
}
}
c.LargePrefix.LargePrefixIndexColsMaps = append(c.LargePrefix.LargePrefixIndexColsMaps, tmpLargePrefixIndexColsMap)
c.LargePrefixIndexColsMaps = append(c.LargePrefixIndexColsMaps, tmpLargePrefixIndexColsMap)
}
}
return in, false
Expand Down

0 comments on commit 1f5d488

Please sign in to comment.