MySQL - 系统存储过程 - sys

create_synonym_db()
Given a schema name, this procedure creates a synonym schema containing views that refer to all the tables and views in the original schema.
mysql> CALL sys.create_synonym_db(‘INFORMATION_SCHEMA’, ‘info’);

diagnostics()
Creates a report of the current server status for diagnostic purposes.
The maximum data collection time in seconds.
The sleep time between data collections in seconds
ENUM(‘current’, ‘medium’, ‘full’)
mysql> tee diag.out;
mysql> CALL sys.diagnostics(120, 30, ‘current’);
mysql> notee;

execute_prepared_stmt()
Given an SQL statement as a string, executes it as a prepared statement. The prepared statement is deallocated after execution, so it is not subject to reuse. Thus, this procedure is useful primarily for executing dynamic statements on a one-time basis.
mysql> CALL sys.execute_prepared_stmt(‘SELECT COUNT(*) FROM mysql.user’);

ps_setup_disable_background_threads()
Disables Performance Schema instrumentation for all background threads.
mysql> CALL sys.ps_setup_disable_background_threads();

ps_setup_disable_consumer()
Disables Performance Schema consumers with names that contain the argument. Produces a result set indicating how many consumers were disabled. Already disabled consumers do not count.
mysql> CALL sys.ps_setup_disable_consumer(‘statement’);

ps_setup_disable_instrument()
Disables Performance Schema instruments with names that contain the argument. Produces a result set indicating how many instruments were disabled
mysql> CALL sys.ps_setup_disable_instrument(‘wait/lock/metadata/sql/mdl’);
mysql> CALL sys.ps_setup_disable_instrument(‘mutex’);

ps_setup_disable_thread()
Given a connection ID, disables Performance Schema instrumentation for the thread.
mysql> CALL sys.ps_setup_disable_thread(225);
mysql> CALL sys.ps_setup_disable_thread(CONNECTION_ID()); – current connection

ps_setup_enable_background_threads()
Enables Performance Schema instrumentation for all background threads.
mysql> CALL sys.ps_setup_enable_background_threads();

ps_setup_enable_consumer()
Enables Performance Schema consumers with names that contain the argument.
mysql> CALL sys.ps_setup_enable_consumer(‘statement’);

ps_setup_enable_instrument()
Enables Performance Schema instruments with names that contain the argument.
mysql> CALL sys.ps_setup_enable_instrument(‘wait/lock/metadata/sql/mdl’);
mysql> CALL sys.ps_setup_enable_instrument(‘mutex’);

ps_setup_enable_thread()
Given a connection ID, enables Performance Schema instrumentation for the thread.
mysql> CALL sys.ps_setup_enable_thread(225);
mysql> CALL sys.ps_setup_enable_thread(CONNECTION_ID());

ps_setup_reload_saved()
Reloads a Performance Schema configuration saved earlier within the same session using ps_setup_save().

ps_setup_reset_to_default()
Resets the Performance Schema configuration to its default settings.
mysql> CALL sys.ps_setup_reset_to_default(TRUE)\G

ps_setup_save()
Saves the current Performance Schema configuration. This enables you to alter the configuration temporarily for debugging or other purposes, then restore it to the previous state by invoking the ps_setup_reload_saved() procedure.
mysql> CALL sys.ps_setup_save(10);

ps_setup_show_disabled()
Displays all currently disabled Performance Schema configuration.
mysql> CALL sys.ps_setup_show_disabled(TRUE, TRUE);

ps_setup_show_disabled_consumers()
Displays all currently disabled Performance Schema consumers.
mysql> CALL sys.ps_setup_show_disabled_consumers();

ps_setup_show_disabled_instruments()
Displays all currently disabled Performance Schema instruments. This might be a long list.
mysql> CALL sys.ps_setup_show_disabled_instruments()\G

ps_setup_show_enabled()
Displays all currently enabled Performance Schema configuration.
mysql> CALL sys.ps_setup_show_enabled(FALSE, FALSE); – in_show_instruments BOOLEAN, in_show_threads BOOLEAN

ps_setup_show_enabled_consumers()
Displays all currently enabled Performance Schema consumers.
mysql> CALL sys.ps_setup_show_enabled_consumers();

ps_setup_show_enabled_instruments()
Displays all currently enabled Performance Schema instruments. This might be a long list.
mysql> CALL sys.ps_setup_show_enabled_instruments()\G

ps_statement_avg_latency_histogram()
Displays a textual histogram graph of the average latency values across all normalized statements tracked within the Performance Schema events_statements_summary_by_digest table.
mysql> CALL sys.ps_statement_avg_latency_histogram()\G

ps_trace_statement_digest()
Traces all Performance Schema instrumentation for a specific statement digest.
CALL sys.ps_trace_statement_digest(‘891ec6860f98ba46d89dd20b0c03652c’, 10, 0.1, TRUE, TRUE); --in_digest VARCHAR(32), in_runtime INT, in_interval DECIMAL(2,2), in_start_fresh BOOLEAN, in_auto_enable BOOLEAN
mysql> CALL sys.ps_trace_statement_digest(‘891ec6860f98ba46d89dd20b0c03652c’, 10, 0.1, TRUE, TRUE);

ps_trace_thread()
Dumps all Performance Schema data for an instrumented thread to a .dot formatted graph file (for the DOT graph description language). Each result set returned from the procedure should be used for a complete graph.
mysql> CALL sys.ps_trace_thread(25, CONCAT(’/tmp/stack-’, REPLACE(NOW(), ’ ', ‘-’), ‘.dot’), NULL, NULL, TRUE, TRUE, TRUE); – in_thread_id INT, in_outfile VARCHAR(255), in_max_runtime DECIMAL(20,2), in_interval DECIMAL(20,2), in_start_fresh BOOLEAN, in_auto_setup BOOLEAN, in_debug BOOLEAN

ps_truncate_all_tables()
Truncates all Performance Schema summary tables, resetting all aggregated instrumentation as a snapshot.
mysql> CALL sys.ps_truncate_all_tables(FALSE); --in_verbose BOOLEAN

statement_performance_analyzer()
Creates a report of the statements running on the server. The views are calculated based on the overall and/or delta activity.
– in_action ENUM(‘snapshot’, ‘overall’, ‘delta’, ‘create_tmp’, ‘create_table’, ‘save’, ‘cleanup’) , in_table VARCHAR(129), in_views SET (‘with_runtimes_in_95th_percentile’, ‘analysis’, ‘with_errors_or_warnings’, ‘with_full_table_scans’, ‘with_sorting’, ‘with_temp_tables’, ‘custom’),

To create a report with the queries in the 95th percentile since the last truncation of events_statements_summary_by_digest and with a one-minute delta period:

  1. Create a temporary table to store the initial snapshot.
    mysql> CALL sys.statement_performance_analyzer(‘create_tmp’, ‘mydb.tmp_digests_ini’, NULL);
  2. Create the initial snapshot.
    mysql> CALL sys.statement_performance_analyzer(‘snapshot’, NULL, NULL);
  3. Save the initial snapshot in the temporary table.
    mysql> CALL sys.statement_performance_analyzer(‘save’, ‘mydb.tmp_digests_ini’, NULL);
  4. Wait one minute.
    mysql> DO SLEEP(60);
  5. Create a new snapshot.
    mysql> CALL sys.statement_performance_analyzer(‘snapshot’, NULL, NULL);
  6. Perform analysis based on the new snapshot.
    mysql> CALL sys.statement_performance_analyzer(‘overall’, NULL, ‘with_runtimes_in_95th_percentile’);
  7. Perform analysis based on the delta between the initial and new snapshots.
    mysql> CALL sys.statement_performance_analyzer(‘delta’, ‘mydb.tmp_digests_ini’, ‘with_runtimes_in_95th_percentile’);

Create an overall report of the 95th percentile queries and the top 10 queries with full table scans:
1.
mysql> CALL sys.statement_performance_analyzer(‘snapshot’, NULL, NULL);
2.
mysql> SET @sys.statement_performance_analyzer.limit = 10;
3.
mysql> CALL sys.statement_performance_analyzer(‘overall’, NULL, ‘with_runtimes_in_95th_percentile,with_full_table_scans’);

Use a custom view showing the top 10 queries sorted by total execution time, refreshing the view every minute using the watch command in Linux:
mysql> CREATE OR REPLACE VIEW mydb.my_statements AS
SELECT sys.format_statement(DIGEST_TEXT) AS query,
SCHEMA_NAME AS db,
COUNT_STAR AS exec_count,
sys.format_time(SUM_TIMER_WAIT) AS total_latency,
sys.format_time(AVG_TIMER_WAIT) AS avg_latency,
ROUND(IFNULL(SUM_ROWS_SENT / NULLIF(COUNT_STAR, 0), 0)) AS rows_sent_avg,
ROUND(IFNULL(SUM_ROWS_EXAMINED / NULLIF(COUNT_STAR, 0), 0)) AS rows_examined_avg,
ROUND(IFNULL(SUM_ROWS_AFFECTED / NULLIF(COUNT_STAR, 0), 0)) AS rows_affected_avg,
DIGEST AS digest
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC;
mysql> CALL sys.statement_performance_analyzer(‘create_table’, ‘mydb.digests_prev’, NULL);

shell> watch -n 60 "mysql sys --table -e "

table_exists()
Tests whether a given table exists as a regular table, a TEMPORARY table, or a view.
– in_db VARCHAR(64), in_table VARCHAR(64), out_exists ENUM(’’, ‘BASE TABLE’, ‘VIEW’, ‘TEMPORARY’)
CALL sys.table_exists(‘db1’, ‘t1’, @exists); SELECT @exists;


Functions

extract_schema_from_file_name()
Given a file path name, returns the path component that represents the schema name. This function assumes that the file name lies within the schema directory.
mysql> SELECT sys.extract_schema_from_file_name(’/usr/local/mysql/data/world/City.ibd’); – path VARCHAR(512)

format_bytes()
Given a byte count, converts it to human-readable format and returns a string consisting of a value and a units indicator. Depending on the size of the value, the units part is bytes, KiB (kibibytes), MiB (mebibytes), GiB (gibibytes), TiB (tebibytes), or PiB (pebibytes).
mysql> SELECT sys.format_bytes(512), sys.format_bytes(18446644073709551615); – bytes TEXT

format_path()
Given a path name, returns the modified path name after replacing subpaths that match the values of the following system variables
mysql> SELECT sys.format_path(’/usr/local/mysql/data/world/City.ibd’); – path VARCHAR(512)

format_statement()
Given a string (normally representing an SQL statement), reduces it to the length given by the statement_truncate_len configuration option, and returns the result. No truncation occurs if the string is shorter than statement_truncate_len. Otherwise, the middle part of the string is replaced by an ellipsis (…).
mysql> SET @stmt = ‘SELECT variable, value, set_time, set_by FROM sys_config’;
mysql> SELECT sys.format_statement(@stmt);
mysql> SET @sys.statement_truncate_len = 32;
mysql> SELECT sys.format_statement(@stmt);
±----------------------------------+
| sys.format_statement(@stmt) |
±----------------------------------+
| SELECT variabl … ROM sys_config |
±----------------------------------+

format_time()
Given a Performance Schema latency or wait time in picoseconds, converts it to human-readable format and returns a string consisting of a value and a units indicator.
mysql> SELECT sys.format_time(3501), sys.format_time(188732396662000); – picoseconds TEXT

list_add(), list_drop()
Adds/Remove a value to a comma-separated list of values and returns the result.
mysql> SET @@sql_mode = sys.list_add(@@sql_mode, ‘NO_ENGINE_SUBSTITUTION’); – in_list TEXT, in_add_value TEXT
mysql> SET @@sql_mode = sys.list_drop(@@sql_mode, ‘ONLY_FULL_GROUP_BY’); – in_list TEXT, in_drop_value TEXT

ps_is_account_enabled()
Returns YES or NO to indicate whether Performance Schema instrumentation for a given account is enabled.
mysql> SELECT sys.ps_is_account_enabled(‘localhost’, ‘root’); – in_host VARCHAR(60), in_user VARCHAR(32)

ps_is_consumer_enabled()
Returns YES or NO to indicate whether a given Performance Schema consumer is enabled, or NULL if the argument is NULL.
mysql> SELECT sys.ps_is_consumer_enabled(‘thread_instrumentation’); – in_consumer VARCHAR(64)

ps_is_instrument_default_enabled()
Returns YES or NO to indicate whether a given Performance Schema instrument is enabled by default.
mysql> SELECT sys.ps_is_instrument_default_enabled(‘memory/innodb/row_log_buf’); – in_instrument VARCHAR(128)

ps_is_instrument_default_timed()
Returns YES or NO to indicate whether a given Performance Schema instrument is timed by default.
mysql> SELECT sys.ps_is_instrument_default_timed(‘memory/innodb/row_log_buf’); – in_instrument VARCHAR(128): The name of the instrument to check.

ps_is_thread_instrumented()
Returns YES or NO to indicate whether Performance Schema instrumentation for a given connection ID is enabled, UNKNOWN if the ID is unknown, or NULL if the ID is NULL.
mysql> SELECT sys.ps_is_thread_instrumented(43);

ps_thread_account()
Given a Performance Schema thread ID, returns the user_name@host_name account associated with the thread.
mysql> SELECT sys.ps_thread_account(sys.ps_thread_id(CONNECTION_ID())); – in_thread_id BIGINT UNSIGNED

ps_thread_id()
Returns the Performance Schema thread ID assigned to a given connection ID, or the thread ID for the current connection if the connection ID is NULL.
mysql> SELECT sys.ps_thread_id(260); – in_connection_id BIGINT UNSIGNED

ps_thread_stack()
Returns a JSON formatted stack of all statements, stages, and events within the Performance Schema for a given thread ID.
mysql> SELECT sys.ps_thread_stack(37, FALSE) AS thread_stack\G – in_thread_id BIGINT, in_verbose BOOLEAN

ps_thread_trx_info()
Returns a JSON object containing information about a given thread
mysql> SELECT sys.ps_thread_trx_info(48)\G – in_thread_id BIGINT UNSIGNED

quote_identifier()
Given a string argument, this function produces a quoted identifier suitable for inclusion in SQL statements. This is useful when a value to be used as an identifier is a reserved word or contains backtick () characters. mysql> SELECT sys.quote_identifier('plain'); -- in_identifier TEXT mysql> SELECT sys.quote_identifier('trickier’);
mysql> SELECT sys.quote_identifier(‘integer’);

sys_get_config()
Given a configuration option name, returns the option value from the sys_config table, or the provided default value (which may be NULL) if the option does not exist in the table.
mysql> SELECT sys.sys_get_config(‘statement_truncate_len’, 128) AS Value; – in_variable_name VARCHAR(128), in_default_value VARCHAR(128)

version_major()
This function returns the major version of the MySQL server.
mysql> SELECT VERSION(), sys.version_major();

version_minor()
This function returns the minor version of the MySQL server.
mysql> SELECT VERSION(), sys.version_minor();

version_patch()
This function returns the patch release version of the MySQL server.
mysql> SELECT VERSION(), sys.version_patch();

发布了213 篇原创文章 · 获赞 7 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/chuckchen1222/article/details/101219847
sys
今日推荐