-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathstatistics.cc
136 lines (116 loc) · 5.8 KB
/
statistics.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Copyright (c) 2017, 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/dd/impl/system_views/statistics.h"
#include <string>
#include "sql/stateless_allocator.h"
namespace dd {
namespace system_views {
const Statistics_base &Statistics::instance() {
static Statistics_base *s_instance = new Statistics();
return *s_instance;
}
const Statistics_base &Show_statistics::instance() {
static Statistics_base *s_instance = new Show_statistics();
return *s_instance;
}
Statistics_base::Statistics_base() {
m_target_def.add_field(FIELD_TABLE_CATALOG, "TABLE_CATALOG",
"cat.name" + m_target_def.fs_name_collation());
m_target_def.add_field(FIELD_TABLE_SCHEMA, "TABLE_SCHEMA",
"sch.name" + m_target_def.fs_name_collation());
m_target_def.add_field(FIELD_TABLE_NAME, "TABLE_NAME",
"tbl.name" + m_target_def.fs_name_collation());
m_target_def.add_field(
FIELD_NON_UNIQUE, "NON_UNIQUE",
"IF (idx.type = 'PRIMARY' OR idx.type = 'UNIQUE',0,1)");
m_target_def.add_field(FIELD_INDEX_SCHEMA, "INDEX_SCHEMA",
"sch.name" + m_target_def.fs_name_collation());
m_target_def.add_field(FIELD_INDEX_NAME, "INDEX_NAME",
"idx.name COLLATE utf8mb3_tolower_ci");
m_target_def.add_field(FIELD_SEQ_IN_INDEX, "SEQ_IN_INDEX",
"icu.ordinal_position");
m_target_def.add_field(
FIELD_COLUMN_NAME, "COLUMN_NAME",
"IF (col.hidden = 'SQL', NULL, col.name COLLATE utf8mb3_tolower_ci)");
m_target_def.add_field(FIELD_COLLATION, "COLLATION",
"CASE WHEN icu.order = 'DESC' THEN 'D' "
"WHEN icu.order = 'ASC' THEN 'A' "
"ELSE NULL END");
m_target_def.add_field(FIELD_SUB_PART, "SUB_PART",
"GET_DD_INDEX_SUB_PART_LENGTH(icu.length,"
"col.type, col.char_length, col.collation_id,"
"idx.type)");
m_target_def.add_field(FIELD_PACKED, "PACKED", "NULL");
m_target_def.add_field(FIELD_NULLABLE, "NULLABLE",
"IF (col.is_nullable = 1, 'YES','')");
m_target_def.add_field(FIELD_INDEX_TYPE, "INDEX_TYPE",
"CASE WHEN idx.type = 'SPATIAL' THEN 'SPATIAL' "
"WHEN idx.algorithm = 'SE_PRIVATE' THEN '' "
"ELSE idx.algorithm END ");
m_target_def.add_field(
FIELD_COMMENT, "COMMENT",
"IF (idx.type = 'PRIMARY' OR idx.type = 'UNIQUE', "
" '',IF(INTERNAL_KEYS_DISABLED(tbl.options),'disabled', ''))");
m_target_def.add_field(FIELD_INDEX_COMMENT, "INDEX_COMMENT", "idx.comment");
m_target_def.add_field(FIELD_IS_VISIBLE, "IS_VISIBLE",
"IF (idx.is_visible, 'YES', 'NO')");
m_target_def.add_field(
FIELD_EXPRESSION, "EXPRESSION",
"IF (col.hidden = 'SQL', col.generation_expression_utf8, NULL)");
m_target_def.add_from("mysql.index_column_usage icu");
m_target_def.add_from("JOIN mysql.indexes idx ON idx.id=icu.index_id");
m_target_def.add_from("JOIN mysql.tables tbl ON idx.table_id=tbl.id");
m_target_def.add_from("JOIN mysql.columns col ON icu.column_id=col.id");
m_target_def.add_from("JOIN mysql.schemata sch ON tbl.schema_id=sch.id");
m_target_def.add_from("JOIN mysql.catalogs cat ON cat.id=sch.catalog_id");
m_target_def.add_from(
"JOIN mysql.collations coll "
"ON tbl.collation_id=coll.id");
m_target_def.add_where("CAN_ACCESS_TABLE(sch.name, tbl.name)");
m_target_def.add_where(
"AND IS_VISIBLE_DD_OBJECT(tbl.hidden, "
"idx.hidden OR icu.hidden, idx.options)");
}
Statistics::Statistics() {
m_target_def.set_view_name(view_name());
m_target_def.add_field(
FIELD_CARDINALITY, "CARDINALITY",
"INTERNAL_INDEX_COLUMN_CARDINALITY(sch.name, tbl.name, idx.name,"
"col.name, idx.ordinal_position,"
"icu.ordinal_position,"
"IF(ISNULL(tbl.partition_type), tbl.engine, ''),"
"tbl.se_private_id,"
"tbl.hidden != 'Visible' OR idx.hidden OR icu.hidden,"
"COALESCE(stat.cardinality, CAST(-1 AS UNSIGNED)),"
"COALESCE(CAST(stat.cached_time as UNSIGNED), 0))");
m_target_def.add_from(
"LEFT JOIN mysql.index_stats stat"
" ON tbl.name=stat.table_name"
" AND sch.name=stat.schema_name"
" AND idx.name=stat.index_name"
" AND col.name=stat.column_name");
}
Show_statistics::Show_statistics() {
m_target_def.set_view_name(view_name());
m_target_def.add_field(FIELD_INDEX_ORDINAL_POSITION, "INDEX_ORDINAL_POSITION",
"idx.ordinal_position");
m_target_def.add_field(FIELD_COLUMN_ORDINAL_POSITION,
"COLUMN_ORDINAL_POSITION", "icu.ordinal_position");
}
} // namespace system_views
} // namespace dd