未验证 提交 eae358c4 编写于 作者: martianzhang's avatar martianzhang 提交者: GitHub

Merge pull request #164 from liipx/master

fix #155 a new config arg 'min-cardinality' for index column selection
...@@ -628,7 +628,6 @@ func (idxAdv *IndexAdvisor) buildJoinIndex(meta common.Meta) []IndexInfo { ...@@ -628,7 +628,6 @@ func (idxAdv *IndexAdvisor) buildJoinIndex(meta common.Meta) []IndexInfo {
indexColsList := make(map[string]map[string][]*common.Column) indexColsList := make(map[string]map[string][]*common.Column)
for _, col := range IndexCols { for _, col := range IndexCols {
mergeIndex(indexColsList, col) mergeIndex(indexColsList, col)
} }
if common.Config.TestDSN.Disable || common.Config.OnlineDSN.Disable { if common.Config.TestDSN.Disable || common.Config.OnlineDSN.Disable {
...@@ -723,6 +722,11 @@ func (idxAdv *IndexAdvisor) buildIndexWithNoEnv(indexList map[string]map[string] ...@@ -723,6 +722,11 @@ func (idxAdv *IndexAdvisor) buildIndexWithNoEnv(indexList map[string]map[string]
// mergeIndex 将索引用到的列去重后合并到一起 // mergeIndex 将索引用到的列去重后合并到一起
func mergeIndex(idxList map[string]map[string][]*common.Column, column *common.Column) { func mergeIndex(idxList map[string]map[string][]*common.Column, column *common.Column) {
// 散粒度低于阈值将不会添加索引
if common.Config.MinCardinality/100 > column.Cardinality {
return
}
db := column.DB db := column.DB
tb := column.Table tb := column.Table
if idxList[db] == nil { if idxList[db] == nil {
......
...@@ -357,6 +357,8 @@ func TestRuleUpdatePrimaryKey(t *testing.T) { ...@@ -357,6 +357,8 @@ func TestRuleUpdatePrimaryKey(t *testing.T) {
func TestIndexAdvise(t *testing.T) { func TestIndexAdvise(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName()) common.Log.Debug("Entering function: %s", common.GetFunctionName())
minCardinalityBak := common.Config.MinCardinality
common.Config.MinCardinality = 20
vEnv, rEnv := env.BuildEnv() vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp() defer vEnv.CleanUp()
...@@ -377,12 +379,13 @@ func TestIndexAdvise(t *testing.T) { ...@@ -377,12 +379,13 @@ func TestIndexAdvise(t *testing.T) {
if idxAdvisor != nil { if idxAdvisor != nil {
rule := idxAdvisor.IndexAdvise().Format() rule := idxAdvisor.IndexAdvise().Format()
if len(rule) > 0 { if len(rule) > 0 {
pretty.Println(rule) _, _ = pretty.Println(rule)
} }
} }
} }
} }
common.Log.Debug("Exiting function: %s", common.GetFunctionName()) common.Log.Debug("Exiting function: %s", common.GetFunctionName())
common.Config.MinCardinality = minCardinalityBak
} }
func TestIndexAdviseNoEnv(t *testing.T) { func TestIndexAdviseNoEnv(t *testing.T) {
......
...@@ -110,6 +110,7 @@ type Configuration struct { ...@@ -110,6 +110,7 @@ type Configuration struct {
MaxSubqueryDepth int `yaml:"max-subquery-depth"` // 子查询最大尝试 MaxSubqueryDepth int `yaml:"max-subquery-depth"` // 子查询最大尝试
MaxVarcharLength int `yaml:"max-varchar-length"` // varchar最大长度 MaxVarcharLength int `yaml:"max-varchar-length"` // varchar最大长度
ColumnNotAllowType []string `yaml:"column-not-allow-type"` // 字段不允许使用的数据类型 ColumnNotAllowType []string `yaml:"column-not-allow-type"` // 字段不允许使用的数据类型
MinCardinality float64 `yaml:"min-cardinality"` // 添加索引散粒度阈值,范围 0~100
// ++++++++++++++EXPLAIN检查项+++++++++++++ // ++++++++++++++EXPLAIN检查项+++++++++++++
ExplainSQLReportType string `yaml:"explain-sql-report-type"` // EXPLAIN markdown 格式输出 SQL 样式,支持 sample, fingerprint, pretty 等 ExplainSQLReportType string `yaml:"explain-sql-report-type"` // EXPLAIN markdown 格式输出 SQL 样式,支持 sample, fingerprint, pretty 等
...@@ -163,6 +164,7 @@ var Config = &Configuration{ ...@@ -163,6 +164,7 @@ var Config = &Configuration{
ConnTimeOut: 3, ConnTimeOut: 3,
QueryTimeOut: 30, QueryTimeOut: 30,
Delimiter: ";", Delimiter: ";",
MinCardinality: 0,
MaxJoinTableCount: 5, MaxJoinTableCount: 5,
MaxGroupByColsCount: 5, MaxGroupByColsCount: 5,
...@@ -512,6 +514,7 @@ func readCmdFlags() error { ...@@ -512,6 +514,7 @@ func readCmdFlags() error {
connTimeOut := flag.Int("conn-time-out", Config.ConnTimeOut, "ConnTimeOut, 数据库连接超时时间,单位秒") connTimeOut := flag.Int("conn-time-out", Config.ConnTimeOut, "ConnTimeOut, 数据库连接超时时间,单位秒")
queryTimeOut := flag.Int("query-time-out", Config.QueryTimeOut, "QueryTimeOut, 数据库SQL执行超时时间,单位秒") queryTimeOut := flag.Int("query-time-out", Config.QueryTimeOut, "QueryTimeOut, 数据库SQL执行超时时间,单位秒")
delimiter := flag.String("delimiter", Config.Delimiter, "Delimiter, SQL分隔符") delimiter := flag.String("delimiter", Config.Delimiter, "Delimiter, SQL分隔符")
minCardinality := flag.Float64("min-cardinality", Config.MinCardinality, "MinCardinality,索引列散粒度最低阈值,散粒度低于该值的列不添加索引,建议范围0.0 ~ 100.0")
// +++++++++++++++日志相关+++++++++++++++++ // +++++++++++++++日志相关+++++++++++++++++
logLevel := flag.Int("log-level", Config.LogLevel, "LogLevel, 日志级别, [0:Emergency, 1:Alert, 2:Critical, 3:Error, 4:Warning, 5:Notice, 6:Informational, 7:Debug]") logLevel := flag.Int("log-level", Config.LogLevel, "LogLevel, 日志级别, [0:Emergency, 1:Alert, 2:Critical, 3:Error, 4:Warning, 5:Notice, 6:Informational, 7:Debug]")
logOutput := flag.String("log-output", Config.LogOutput, "LogOutput, 日志输出位置") logOutput := flag.String("log-output", Config.LogOutput, "LogOutput, 日志输出位置")
...@@ -612,6 +615,7 @@ func readCmdFlags() error { ...@@ -612,6 +615,7 @@ func readCmdFlags() error {
Config.IgnoreRules = strings.Split(*ignoreRules, ",") Config.IgnoreRules = strings.Split(*ignoreRules, ",")
Config.RewriteRules = strings.Split(*rewriteRules, ",") Config.RewriteRules = strings.Split(*rewriteRules, ",")
*blackList = strings.TrimSpace(*blackList) *blackList = strings.TrimSpace(*blackList)
Config.MinCardinality = *minCardinality
if filepath.IsAbs(*blackList) || *blackList == "" { if filepath.IsAbs(*blackList) || *blackList == "" {
Config.BlackList = *blackList Config.BlackList = *blackList
......
...@@ -71,6 +71,7 @@ max-subquery-depth: 5 ...@@ -71,6 +71,7 @@ max-subquery-depth: 5
max-varchar-length: 1024 max-varchar-length: 1024
column-not-allow-type: column-not-allow-type:
- boolean - boolean
min-cardinality: 0
explain-sql-report-type: pretty explain-sql-report-type: pretty
explain-type: extended explain-type: extended
explain-format: traditional explain-format: traditional
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册