-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathindex.py
More file actions
176 lines (130 loc) · 5.53 KB
/
index.py
File metadata and controls
176 lines (130 loc) · 5.53 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from mysqlsh.plugin_manager import plugin, plugin_function
import mysqlsh
try:
import pandas as pd
pandas_present = True
except:
pandas_present = False
mysqlsh.globals.shell.log("WARNING",
"Python module pandas are not present, check.get_unused_indexes() won't work")
mysqlsh.globals.shell.log("WARNING",
"Try:\n mysqlsh --pym pip install --user pandas")
def _query_unused_indexes(session):
if not pandas_present:
print("Python module pandas are not present, check.get_unused_indexes() won't work")
print("Try:\n mysqlsh --py pip install --user pandas")
return
exclueded_schemas = ['information_schema',
'mysql',
'performance_schema',
'sys',
'mysql_innodb_cluster_metadata']
filter_string = "WHERE object_schema NOT IN ('{}')".format("','".join(exclueded_schemas))
stmt = """select object_schema,
object_name, index_name
from sys.schema_unused_indexes {}
""".format(filter_string)
# maybe can use pd.read_sql, if that, we need to use sqlalchemy or pymysql
try:
result = session.run_sql(stmt)
result = result.fetch_all()
if result:
result = pd.DataFrame([list(index) for index in result], columns=['schema', 'table', 'index'])
else:
result = pd.DataFrame(columns=['schema', 'table', 'index'])
except Exception as e:
print("Error: {}".format(e))
return False
return result
def _query_unused_indexes_group(addresses, user, password):
shell = mysqlsh.globals.shell
mysql = mysqlsh.mysql
pd_common_unused_indexes = pd.DataFrame()
for address in addresses:
my_classic_session = mysql.get_session(shell.unparse_uri(
{'user': user, 'password': password, 'host': address.split(':')[0], 'port': address.split(':')[1]}))
if pd_common_unused_indexes.empty:
pd_common_unused_indexes = _query_unused_indexes(my_classic_session)
if pd_common_unused_indexes is False:
break
if pd_common_unused_indexes.empty:
break
pd_unused_indexes = _query_unused_indexes(my_classic_session)
if pd_unused_indexes is False:
pd_common_unused_indexes = False
break
if pd_unused_indexes.empty:
pd_common_unused_indexes = pd_unused_indexes
break
pd_common_unused_indexes = pd.merge(pd_common_unused_indexes, pd_unused_indexes)
return pd_common_unused_indexes
def _get_common_unused_indexes(session, limit, cluster_object):
shell = mysqlsh.globals.shell
current_session_uri = session.get_uri()
addresses = [instance['address'] for instance in cluster_object.describe()['defaultReplicaSet']['topology']]
password = shell.prompt("Please Enter password again for {}: ".format(current_session_uri),
{'type': 'password'})
if password is None:
return
pd.set_option('display.max_rows', limit)
result = _query_unused_indexes_group(addresses,
shell.parse_uri(current_session_uri)['user'],
password)
if result is False:
return
if result.empty:
return
print(result.head(limit))
pd_rows = result.shape[0]
if pd_rows > limit:
awnser = shell.prompt('Show all unused indexes? ', {'type': 'confirm', 'yes': '&yes', 'defaultValue': 'no'})
if awnser == '&yes':
pd.set_option('display.max_rows', pd_rows)
print(result)
return
@plugin_function("check.getRstUnusedIndexes")
def get_rs_unused_indexes(limit=20, session=None):
"""
Print the unused indexes in a schema or table, or all schemas and tables in a Replica Set
Args:
limit (integer): limit the number of rows to display, default 20
session (object): The optional session object used to query the
database. If omitted the MySQL Shell's current session will be used.
"""
dba = mysqlsh.globals.dba
shell = mysqlsh.globals.shell
if session is None:
session = shell.get_session()
if session is None:
print("No session specified. Either pass a session object to this "
"function or connect the shell to a database")
return
try:
rs = dba.get_replica_set()
_get_common_unused_indexes(session, limit, rs)
except Exception as e:
print("Error: {}".format(e))
return
@plugin_function("check.getIcUnusedIndexes")
def get_ic_unused_indexes(limit=20, session=None):
"""
Print the unused indexes in a schema or table, or all schemas and tables in a cluster.
Args:
limit (integer): limit the number of rows to display, default 20
session (object): The optional session object used to query the
database. If omitted the MySQL Shell's current session will be used.
"""
dba = mysqlsh.globals.dba
shell = mysqlsh.globals.shell
if session is None:
session = shell.get_session()
if session is None:
print("No session specified. Either pass a session object to this "
"function or connect the shell to a database")
return
try:
ic = dba.get_cluster()
_get_common_unused_indexes(session, limit, ic)
except Exception as e:
print("Error: {}".format(e))
return