-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path35_Performance_Schema.sql
More file actions
313 lines (283 loc) · 8.87 KB
/
35_Performance_Schema.sql
File metadata and controls
313 lines (283 loc) · 8.87 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**************************************************************
* MySQL 8.0 Performance Schema Tutorial
* This script demonstrates how to use performance_schema
* and the sys schema to monitor, diagnose, and tune
* MySQL workloads. It serves as the MySQL equivalent of
* SQL Server Query Store and Extended Events. It covers:
* - Enabling and configuring performance_schema consumers.
* - Statement statistics (top queries by latency / rows).
* - Table and index I/O wait statistics.
* - Wait event analysis (lock waits, I/O waits).
* - sys schema views for actionable insights.
* - Diagnosing missing indexes via performance_schema.
* - Slow query log integration.
* - Session and connection tracking.
**************************************************************/
-------------------------------------------------
-- Region: 0. Verify Performance Schema is Available
-------------------------------------------------هد
/*
performance_schema is enabled by default in MySQL 8.0.
Confirm it is active.
*/
SHOW VARIABLES LIKE 'performance_schema';
/*
List available databases.
*/
SHOW DATABASES;
-------------------------------------------------
-- Region: 1. Enabling Key Consumers and Instruments
-------------------------------------------------
/*
1.1 Check which consumers are currently active.
*/
SELECT NAME, ENABLED
FROM performance_schema.setup_consumers
ORDER BY NAME;
/*
1.2 Enable the statement-history consumers (off by default).
*/
UPDATE performance_schema.setup_consumers
SET ENABLED = 'YES'
WHERE NAME IN (
'events_statements_history',
'events_statements_history_long',
'events_waits_history',
'events_waits_history_long'
);
/*
1.3 Verify statement instrumentation is active.
*/
SELECT NAME, ENABLED, TIMED
FROM performance_schema.setup_instruments
WHERE NAME LIKE 'statement/%'
LIMIT 10;
/*
1.4 Enable all statement instruments and waits.
*/
UPDATE performance_schema.setup_instruments
SET ENABLED = 'YES', TIMED = 'YES'
WHERE NAME LIKE 'statement/%'
OR NAME LIKE 'wait/%';
-------------------------------------------------
-- Region: 2. Statement Statistics (Top Slow Queries)
-------------------------------------------------
/*
2.1 Top 10 queries by total combined latency (picoseconds).
*/
SELECT
DIGEST_TEXT AS query_pattern,
COUNT_STAR AS executions,
ROUND(SUM_TIMER_WAIT / 1e12, 3) AS total_latency_sec,
ROUND(AVG_TIMER_WAIT / 1e12, 6) AS avg_latency_sec,
ROUND(MAX_TIMER_WAIT / 1e12, 6) AS max_latency_sec,
SUM_ROWS_EXAMINED AS total_rows_examined,
ROUND(SUM_ROWS_EXAMINED / NULLIF(COUNT_STAR, 0), 0) AS avg_rows_examined,
SUM_ROWS_SENT AS total_rows_sent,
SUM_NO_INDEX_USED AS no_index_used
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;
/*
2.2 Queries with full-table scans (no index used).
*/
SELECT
DIGEST_TEXT,
COUNT_STAR AS executions,
SUM_NO_INDEX_USED AS full_scans
FROM performance_schema.events_statements_summary_by_digest
WHERE SUM_NO_INDEX_USED > 0
ORDER BY SUM_NO_INDEX_USED DESC
LIMIT 10;
/*
2.3 High row-examination-to-sent ratio (index inefficiency indicator).
*/
SELECT
DIGEST_TEXT,
COUNT_STAR AS executions,
ROUND(SUM_ROWS_EXAMINED / NULLIF(SUM_ROWS_SENT, 0), 1) AS examine_to_send_ratio
FROM performance_schema.events_statements_summary_by_digest
WHERE SUM_ROWS_SENT > 0
ORDER BY examine_to_send_ratio DESC
LIMIT 10;
-------------------------------------------------
-- Region: 3. Table I/O Wait Statistics
-------------------------------------------------
/*
3.1 Tables with the highest cumulative latency.
*/
SELECT
OBJECT_SCHEMA AS db_name,
OBJECT_NAME AS table_name,
COUNT_STAR AS total_io_requests,
ROUND(SUM_TIMER_WAIT / 1e12, 3) AS total_latency_sec,
COUNT_READ,
COUNT_WRITE,
COUNT_FETCH
FROM performance_schema.table_io_waits_summary_by_table
WHERE OBJECT_SCHEMA NOT IN ('performance_schema','information_schema',
'mysql','sys')
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;
/*
3.2 Index-level I/O: detect hot indexes and unused ones.
*/
SELECT
OBJECT_NAME AS table_name,
INDEX_NAME,
COUNT_READ,
COUNT_WRITE,
ROUND(SUM_TIMER_WAIT / 1e12, 3) AS latency_sec
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE OBJECT_SCHEMA = DATABASE()
AND INDEX_NAME IS NOT NULL
ORDER BY SUM_TIMER_WAIT DESC;
-------------------------------------------------
-- Region: 4. Wait Event Analysis
-------------------------------------------------
/*
4.1 Top wait events by accumulated wait time.
*/
SELECT
EVENT_NAME,
COUNT_STAR AS occurrences,
ROUND(SUM_TIMER_WAIT / 1e12, 3) AS total_wait_sec,
ROUND(AVG_TIMER_WAIT / 1e12, 6) AS avg_wait_sec
FROM performance_schema.events_waits_summary_global_by_event_name
WHERE SUM_TIMER_WAIT > 0
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;
/*
4.2 InnoDB lock waits (current blocking waits).
*/
SELECT *
FROM performance_schema.data_lock_waits;
/*
4.3 Inspect detailed lock information.
*/
SELECT
ENGINE_LOCK_ID,
ENGINE_TRANSACTION_ID,
THREAD_ID,
OBJECT_SCHEMA,
OBJECT_NAME,
INDEX_NAME,
LOCK_TYPE,
LOCK_MODE,
LOCK_STATUS
FROM performance_schema.data_locks
LIMIT 20;
-------------------------------------------------
-- Region: 5. sys Schema Views (Actionable Insights)
-------------------------------------------------
/*
5.1 Statements contributing most to server latency.
*/
SELECT query, exec_count, total_latency, avg_latency
FROM sys.statement_analysis
ORDER BY total_latency DESC
LIMIT 10;
/*
5.2 Schemas with the most table data.
*/
SELECT * FROM sys.schema_table_statistics
ORDER BY total_latency DESC
LIMIT 10;
/*
5.3 Unused indexes.
*/
SELECT * FROM sys.schema_unused_indexes
WHERE object_schema = DATABASE();
/*
5.4 Redundant indexes.
*/
SELECT * FROM sys.schema_redundant_indexes
WHERE table_schema = DATABASE();
/*
5.5 Tables without a primary key.
*/
SELECT * FROM sys.schema_tables_with_full_table_scans
LIMIT 10;
/*
5.6 Current sessions and their resource usage.
*/
SELECT conn_id, user, db, command, time, state, current_statement
FROM sys.session
ORDER BY time DESC
LIMIT 20;
-------------------------------------------------
-- Region: 6. Slow Query Log
-------------------------------------------------
/*
6.1 Review slow query log settings.
*/
SHOW VARIABLES LIKE 'slow_query_log%';
SHOW VARIABLES LIKE 'long_query_time';
/*
6.2 Enable and configure the slow query log for this session.
Set the threshold to 1 second.
*/
-- SET GLOBAL slow_query_log = 'ON';
-- SET GLOBAL long_query_time = 1;
-- SET GLOBAL slow_query_log_file = '/var/log/mysql/slow.log';
-- SET GLOBAL log_queries_not_using_indexes = 'ON';
/*
6.3 The slow log can also be routed to a table (useful for SQL analysis).
*/
-- SET GLOBAL log_output = 'TABLE';
-- After enabling, queries exceeding long_query_time appear here:
-- SELECT * FROM mysql.slow_log ORDER BY start_time DESC LIMIT 20;
-------------------------------------------------
-- Region: 7. Session and Connection Tracking
-------------------------------------------------
/*
7.1 Show active connections and their resource usage.
*/
SELECT
ID AS connection_id,
USER,
HOST,
DB,
COMMAND,
TIME AS seconds_running,
STATE,
INFO AS current_query
FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE COMMAND <> 'Sleep'
ORDER BY TIME DESC;
/*
7.2 Connection counts by user.
*/
SELECT
USER,
COUNT(*) AS connections
FROM INFORMATION_SCHEMA.PROCESSLIST
GROUP BY USER
ORDER BY connections DESC;
/*
7.3 Memory usage by thread (requires memory instrumentation).
*/
SELECT
t.PROCESSLIST_USER AS user,
SUM(m.CURRENT_NUMBER_OF_BYTES_USED) AS memory_bytes
FROM performance_schema.memory_summary_by_thread_by_event_name m
JOIN performance_schema.threads t ON m.THREAD_ID = t.THREAD_ID
GROUP BY t.PROCESSLIST_USER
ORDER BY memory_bytes DESC
LIMIT 10;
-------------------------------------------------
-- Region: 8. Resetting Statistics
-------------------------------------------------
/*
8.1 Truncate all statement summary tables to start fresh after a
workload change.
*/
-- TRUNCATE TABLE performance_schema.events_statements_summary_by_digest;
-- TRUNCATE TABLE performance_schema.events_waits_summary_global_by_event_name;
/*
8.2 Reset all performance_schema statistics.
*/
-- CALL sys.ps_reset_statement_instrumentation(); -- requires sys schema
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------