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