-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: add sys
schema, sys.SCHEMA_UNUSED_INDEXES
view and sys.SCHEMA_INDEX_USAGE
view
#22126
base: master
Are you sure you want to change the base?
Changes from all commits
425326f
813af86
6871943
3f7dd4e
58fbffa
4aeff8d
8130cc8
030dd70
ebad728
c3b752c
0988be5
b1ac966
640ddc7
d68834c
3f29a08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sysschema | ||
|
||
var sysSchemaTables = []string{ | ||
viewUnusedIndexes, | ||
viewIndexUsage, | ||
} | ||
|
||
const viewUnusedIndexes = "CREATE VIEW sys." + viewNameUnusedIndexes + | ||
` AS SELECT DISTINCT i.table_schema AS table_schema, i.table_name AS table_name, i.key_name AS index_name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider a situation, table |
||
FROM information_schema.tidb_indexes AS i | ||
LEFT JOIN information_schema.tables AS t | ||
ON t.table_schema = i.table_schema | ||
AND t.table_name = i.table_name | ||
LEFT JOIN mysql.schema_index_usage AS u | ||
ON u.table_id = t.tidb_table_id | ||
AND u.index_id = i.index_id | ||
WHERE (u.query_count = 0 OR u.query_count is null) | ||
AND i.key_name != 'PRIMARY' | ||
AND i.table_schema not in ('mysql', 'PERFORMANCE_SCHEMA', 'INFORMATION_SCHEMA');` | ||
|
||
const viewIndexUsage = "CREATE VIEW sys." + viewNameIndexUsage + | ||
` AS SELECT DISTINCT i.table_schema AS table_schema, | ||
i.table_name AS table_name, | ||
i.key_name AS index_name, | ||
IFNULL(u.query_count, 0) AS query_count, | ||
IFNULL(u.rows_selected, 0) AS rows_selected, | ||
u.last_used_at AS last_used_at | ||
FROM information_schema.tidb_indexes AS i | ||
LEFT JOIN information_schema.tables AS t | ||
ON t.table_schema = i.table_schema | ||
AND t.table_name = i.table_name | ||
LEFT JOIN mysql.schema_index_usage AS u | ||
ON u.table_id = t.tidb_table_id | ||
AND u.index_id = i.index_id | ||
WHERE i.key_name != 'PRIMARY' | ||
AND i.table_schema not in ('mysql', 'PERFORMANCE_SCHEMA', 'INFORMATION_SCHEMA');` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sysschema | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/pingcap/parser" | ||
"github.com/pingcap/parser/ast" | ||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/parser/mysql" | ||
"github.com/pingcap/tidb/ddl" | ||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/meta/autoid" | ||
"github.com/pingcap/tidb/util" | ||
) | ||
|
||
var once sync.Once | ||
|
||
// Init register the `sys` views. | ||
// It should be init(), and the ideal usage should be: | ||
// | ||
// import _ "github.com/pingcap/tidb/sysschema" | ||
// | ||
// This function depends on plan/core.init(), which initialize the expression.EvalAstExpr function. | ||
// The initialize order is a problem if init() is used as the function name. | ||
func Init() { | ||
initOnce := func() { | ||
p := parser.New() | ||
tbls := make([]*model.TableInfo, 0) | ||
dbID := autoid.SysSchemaDBID | ||
for _, sql := range sysSchemaTables { | ||
stmt, err := p.ParseOneStmt(sql, "", "") | ||
if err != nil { | ||
panic(err) | ||
} | ||
meta, err := ddl.BuildTableInfoFromCreateViewAST(stmt.(*ast.CreateViewStmt)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
tbls = append(tbls, meta) | ||
var ok bool | ||
meta.ID, ok = tableIDMap[meta.Name.O] | ||
if !ok { | ||
panic(fmt.Sprintf("get sys table id failed, unknown system table `%v`", meta.Name.O)) | ||
} | ||
for i, c := range meta.Columns { | ||
c.ID = int64(i) + 1 | ||
} | ||
} | ||
dbInfo := &model.DBInfo{ | ||
ID: dbID, | ||
Name: util.SysSchemaName, | ||
Charset: mysql.DefaultCharset, | ||
Collate: mysql.DefaultCollationName, | ||
Tables: tbls, | ||
} | ||
infoschema.RegisterVirtualTable(dbInfo, tableFromMeta) | ||
} | ||
if expression.EvalAstExpr != nil { | ||
once.Do(initOnce) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about renaming it to
BuildViewInfoFromAST
to correspond toBuildTableInfoFromAST
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think this is a good name, because this function does not build
ViewInfo
, butTableInfo
.