Skip to content

Commit b4459c0

Browse files
committed
o/ A Whole Lotta Changing Going On o/
* Restructured to split out each object in to an individual file - Allows reuse of the same object across versions with no copy/paste - Still have a ps_helper_<version>.sql file to call to load by version * Renamed many of the views, with a somewhat more logical/descriptive naming * Added a version for 5.7 - Includes new memory views - Includes changes to existing views to augment with memory info * Update to dump_thread_stack() (Thanks to Jesper Krogh) - Now can auto-enable required instruments (and reset config after) - Now can disable all other threads (so better chance of catching more) - Now can sit and monitor a thread for a specific frequency/interval (again to try and catch more) * Added new procedures to save / restore config (Thanks to Jesper Krogh) * Added _raw views for all views that use formatting functions (for tools, rather than CLI) * Added reverse_format_time(), so you can still use a formatted view, but order by some other time column * Added a number of new views
1 parent 80706ff commit b4459c0

File tree

65 files changed

+4491
-1994
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4491
-1994
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,18 @@ dbahelper
22
=========
33

44
A collection of scripts to help MySQL DBAs
5+
6+
There are install files available for 5.5, 5.6 and 5.7 respectively. To load these, you must position yourself within the directory that you downloaded to, as these top level files SOURCE individual files that are shared across versions in most cases (though not all).
7+
8+
For instance if you download to /tmp/dbahelper/ you should:
9+
10+
cd /tmp/dbahelper/
11+
mysql -u user -p < ./ps_helper_<version>.sql
12+
13+
Of if you would like to log in to the client:
14+
15+
cd /tmp/dbahelper/
16+
mysql -u user -p
17+
SOURCE ./ps_helper_<version>.sql
18+
19+
Alternatively, you could just choose to load individual files based on your needs, but beware, certain objects have dependencies on other objects.

after_setup.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SET @@sql_log_bin = @sql_log_bin;

before_setup.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SET NAMES utf8;
2+
SET @sql_log_bin = @@sql_log_bin;
3+
SET sql_log_bin = 0;
4+
5+
CREATE DATABASE IF NOT EXISTS ps_helper DEFAULT CHARACTER SET utf8;
6+
7+
USE ps_helper;
8+
9+
CREATE OR REPLACE VIEW version AS SELECT '2.0.0';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Function: extract_schema_from_file_name()
3+
*
4+
* Takes a raw file path, and extracts the schema name from it
5+
*
6+
* Parameters
7+
* path: The raw file name value to extract the schema name from
8+
*/
9+
10+
DROP FUNCTION IF EXISTS extract_schema_from_file_name;
11+
12+
DELIMITER $$
13+
14+
CREATE FUNCTION extract_schema_from_file_name(path VARCHAR(512))
15+
RETURNS VARCHAR(512) DETERMINISTIC
16+
RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(path, '\\', '/'), '/', -2), '/', 1)
17+
$$
18+
19+
DELIMITER ;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Function: extract_table_from_file_name()
3+
*
4+
* Takes a raw file path, and extracts the table name from it
5+
*
6+
* Parameters
7+
* path: The raw file name value to extract the table name from
8+
*/
9+
10+
DROP FUNCTION IF EXISTS extract_table_from_file_name;
11+
12+
DELIMITER $$
13+
14+
CREATE FUNCTION extract_table_from_file_name(path VARCHAR(512))
15+
RETURNS VARCHAR(512) DETERMINISTIC
16+
RETURN SUBSTRING_INDEX(REPLACE(SUBSTRING_INDEX(REPLACE(path, '\\', '/'), '/', -1), '@0024', '$'), '.', 1);
17+
$$
18+
19+
DELIMITER ;

functions/format_bytes.sql

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Function: format_bytes()
3+
*
4+
* Takes a raw bytes value, and converts it to a human readable form
5+
*
6+
* Parameters
7+
* bytes: The raw bytes value to convert
8+
*
9+
* mysql> select format_bytes(2348723492723746);
10+
* +--------------------------------+
11+
* | format_bytes(2348723492723746) |
12+
* +--------------------------------+
13+
* | 2.09 PiB |
14+
* +--------------------------------+
15+
* 1 row in set (0.00 sec)
16+
*
17+
* mysql> select format_bytes(2348723492723);
18+
* +-----------------------------+
19+
* | format_bytes(2348723492723) |
20+
* +-----------------------------+
21+
* | 2.14 TiB |
22+
* +-----------------------------+
23+
* 1 row in set (0.00 sec)
24+
*
25+
* mysql> select format_bytes(23487234);
26+
* +------------------------+
27+
* | format_bytes(23487234) |
28+
* +------------------------+
29+
* | 22.40 MiB |
30+
* +------------------------+
31+
* 1 row in set (0.00 sec)
32+
*/
33+
34+
DROP FUNCTION IF EXISTS format_bytes;
35+
36+
DELIMITER $$
37+
38+
CREATE FUNCTION format_bytes(bytes BIGINT)
39+
RETURNS VARCHAR(16) DETERMINISTIC
40+
BEGIN
41+
IF bytes IS NULL THEN RETURN NULL;
42+
ELSEIF bytes >= 1125899906842624 THEN RETURN CONCAT(ROUND(bytes / 1125899906842624, 2), ' PiB');
43+
ELSEIF bytes >= 1099511627776 THEN RETURN CONCAT(ROUND(bytes / 1099511627776, 2), ' TiB');
44+
ELSEIF bytes >= 1073741824 THEN RETURN CONCAT(ROUND(bytes / 1073741824, 2), ' GiB');
45+
ELSEIF bytes >= 1048576 THEN RETURN CONCAT(ROUND(bytes / 1048576, 2), ' MiB');
46+
ELSEIF bytes >= 1024 THEN RETURN CONCAT(ROUND(bytes / 1024, 2), ' KiB');
47+
ELSE RETURN CONCAT(bytes, ' bytes');
48+
END IF;
49+
END $$
50+
51+
DELIMITER ;

functions/format_path.sql

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Function: format_path()
3+
*
4+
* Takes a raw path value, and strips out the datadir or tmpdir
5+
* replacing with @@datadir and @@tmpdir respectively.
6+
*
7+
* Also normalizes the paths across operating systems, so backslashes
8+
* on Windows are converted to forward slashes
9+
*
10+
* Parameters
11+
* path: The raw file path value to format
12+
*
13+
* mysql> select @@datadir;
14+
* +-----------------------------------------------+
15+
* | @@datadir |
16+
* +-----------------------------------------------+
17+
* | /Users/mark/sandboxes/SmallTree/AMaster/data/ |
18+
* +-----------------------------------------------+
19+
* 1 row in set (0.06 sec)
20+
*
21+
* mysql> select format_path('/Users/mark/sandboxes/SmallTree/AMaster/data/mysql/proc.MYD');
22+
* +----------------------------------------------------------------------------+
23+
* | format_path('/Users/mark/sandboxes/SmallTree/AMaster/data/mysql/proc.MYD') |
24+
* +----------------------------------------------------------------------------+
25+
* | @@datadir/mysql/proc.MYD |
26+
* +----------------------------------------------------------------------------+
27+
* 1 row in set (0.03 sec)
28+
*/
29+
30+
DROP FUNCTION IF EXISTS format_path;
31+
32+
DELIMITER $$
33+
34+
CREATE FUNCTION format_path(path VARCHAR(260))
35+
RETURNS VARCHAR(260) CHARSET UTF8 DETERMINISTIC
36+
BEGIN
37+
DECLARE v_path VARCHAR(260);
38+
39+
/* OSX hides /private/ in variables, but Performance Schema does not */
40+
IF path LIKE '/private/%'
41+
THEN SET v_path = REPLACE(path, '/private', '');
42+
ELSE SET v_path = path;
43+
END IF;
44+
45+
IF v_path IS NULL THEN RETURN NULL;
46+
ELSEIF v_path LIKE CONCAT(@@global.datadir, '%') ESCAPE '|' THEN
47+
RETURN REPLACE(REPLACE(REPLACE(v_path, @@global.datadir, '@@datadir/'), '\\\\', ''), '\\', '/');
48+
ELSEIF v_path LIKE CONCAT(@@global.tmpdir, '%') ESCAPE '|' THEN
49+
RETURN REPLACE(REPLACE(REPLACE(v_path, @@global.tmpdir, '@@tmpdir/'), '\\\\', ''), '\\', '/');
50+
ELSE RETURN v_path;
51+
END IF;
52+
END$$
53+
54+
DELIMITER ;

functions/format_statement.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Function: format_statement()
3+
*
4+
* Formats a normalized statement with a truncated string if > 64 characters long
5+
*
6+
* Parameters
7+
* filename: The raw file name value to extract the table name from
8+
*/
9+
10+
DROP FUNCTION IF EXISTS format_statement;
11+
12+
DELIMITER $$
13+
14+
CREATE FUNCTION format_statement(statement LONGTEXT)
15+
RETURNS VARCHAR(65) DETERMINISTIC
16+
BEGIN
17+
IF LENGTH(statement) > 64 THEN RETURN REPLACE(CONCAT(LEFT(statement, 30), ' ... ', RIGHT(statement, 30)), '\n', ' ');
18+
ELSE RETURN REPLACE(statement, '\n', ' ');
19+
END IF;
20+
END $$
21+
22+
DELIMITER ;

functions/format_time.sql

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Function: format_time()
3+
*
4+
* Takes a raw picoseconds value, and converts it to a human readable form.
5+
* Picoseconds are the precision that all latency values are printed in
6+
* within MySQL's Performance Schema.
7+
*
8+
* Parameters
9+
* picoseconds: The raw picoseconds value to convert
10+
*
11+
* mysql> select format_time(342342342342345);
12+
* +------------------------------+
13+
* | format_time(342342342342345) |
14+
* +------------------------------+
15+
* | 00:05:42 |
16+
* +------------------------------+
17+
* 1 row in set (0.00 sec)
18+
*
19+
* mysql> select format_time(342342342);
20+
* +------------------------+
21+
* | format_time(342342342) |
22+
* +------------------------+
23+
* | 342.34 µs |
24+
* +------------------------+
25+
* 1 row in set (0.00 sec)
26+
*
27+
* mysql> select format_time(34234);
28+
* +--------------------+
29+
* | format_time(34234) |
30+
* +--------------------+
31+
* | 34.23 ns |
32+
* +--------------------+
33+
* 1 row in set (0.00 sec)
34+
*
35+
* mysql> select format_time(342);
36+
* +------------------+
37+
* | format_time(342) |
38+
* +------------------+
39+
* | 342 ps |
40+
* +------------------+
41+
* 1 row in set (0.00 sec)
42+
*/
43+
44+
DROP FUNCTION IF EXISTS format_time;
45+
46+
DELIMITER $$
47+
48+
CREATE FUNCTION format_time(picoseconds BIGINT UNSIGNED)
49+
RETURNS VARCHAR(16) CHARSET UTF8 DETERMINISTIC
50+
BEGIN
51+
IF picoseconds IS NULL THEN RETURN NULL;
52+
ELSEIF picoseconds >= 3600000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 3600000000000000, 2), 'h');
53+
ELSEIF picoseconds >= 60000000000000 THEN RETURN SEC_TO_TIME(ROUND(picoseconds / 1000000000000, 2));
54+
ELSEIF picoseconds >= 1000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000000, 2), ' s');
55+
ELSEIF picoseconds >= 1000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000, 2), ' ms');
56+
ELSEIF picoseconds >= 1000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000, 2), ' us');
57+
ELSEIF picoseconds >= 1000 THEN RETURN CONCAT(ROUND(picoseconds / 1000, 2), ' ns');
58+
ELSE RETURN CONCAT(picoseconds, ' ps');
59+
END IF;
60+
END $$
61+
62+
DELIMITER ;

functions/is_account_enabled.sql

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Function: is_account_enabled()
3+
*
4+
* Determines whether instrumentation of an account is enabled.
5+
*
6+
* Parameters
7+
* in_host .....: The hostname of the account to check.
8+
* in_user .....: The username of the account to check.
9+
*
10+
* Returns
11+
* An enum whether the account is enabled or not.
12+
*
13+
* mysql> SELECT is_account_enabled('localhost', 'root');
14+
* +-----------------------------------------+
15+
* | is_account_enabled('localhost', 'root') |
16+
* +-----------------------------------------+
17+
* | YES |
18+
* +-----------------------------------------+
19+
* 1 row in set (0.00 sec)
20+
*
21+
* Contributed by Jesper Krogh of MySQL Support @ Oracle
22+
*/
23+
24+
DROP FUNCTION IF EXISTS is_account_enabled;
25+
26+
DELIMITER $$
27+
28+
CREATE DEFINER='root'@'localhost' FUNCTION is_account_enabled(in_host VARCHAR(60), in_user VARCHAR(16)) RETURNS enum('YES','NO', 'PARTIAL')
29+
COMMENT 'Returns whether a user account is enabled.'
30+
LANGUAGE SQL
31+
DETERMINISTIC
32+
READS SQL DATA
33+
SQL SECURITY INVOKER
34+
BEGIN
35+
RETURN IF(EXISTS(SELECT 1
36+
FROM performance_schema.setup_actors
37+
WHERE (`HOST` = '%' OR `HOST` = in_host)
38+
AND (`USER` = '%' OR `USER` = in_user)
39+
),
40+
'YES', 'NO'
41+
);
42+
END $$
43+
44+
DELIMITER ;

0 commit comments

Comments
 (0)