网上的一个很牛的python脚本,用来分析mysql的执行计划,不过是python2写的,改了一下支持python3,并且支持数据库名.表名这种SQL写法

原文地址:https://dbaplus.cn/blog-77-736-1.html

执行结果如下: /usr/bin/python3 mysql_tuning.py -s "select count(*) from slow_db.dbinfo,slow_db.mysql_slow_query_r" 主要内容包括:

===== BASIC INFORMATION =====
===== ORIGINAL SQL TEXT =====
===== SYSTEM PARAMETER =====
===== OPTIMIZER SWITCH =====
===== SQL PLAN =====
===== OPTIMIZER REWRITE SQL =====
===== OBJECT STATISTICS =====
===== SESSION STATUS (DIFFERENT) =====
===== SQL PROFILING(DETAIL)=====
===== SQL PROFILING(SUMMARY)=====
===== EXECUTE TIME =====
****************************************************************************************************
*                             MySQL SQL Tuning Tools v1.0 (by hanfeng)                             *
****************************************************************************************************
===== BASIC INFORMATION =====
+----------------------+------------+------------+------------+
|      server_ip       | user_name  |  db_name   | db_version |
+----------------------+------------+------------+------------+
|      127.0.0.1       |   admin    | Admin_12345 |   8.0.18   |
+----------------------+------------+------------+------------+
===== ORIGINAL SQL TEXT =====
SELECT count(*)
FROM slow_db.dbinfo,
     slow_db.mysql_slow_query_r
===== SYSTEM PARAMETER =====
+--------------------------------+------------------------------------------------------------+
|         parameter_name         |                           value                            |
+--------------------------------+------------------------------------------------------------+
| binlog_cache_size             |                                                     32.0 K |
| bulk_insert_buffer_size       |                                                      8.0 M |
| have_query_cache              |                                                         NO |
| interactive_timeout           |                                                      28800 |
| join_buffer_size              |                                                    256.0 K |
| key_buffer_size               |                                                      8.0 M |
| key_cache_age_threshold       |                                                        300 |
| key_cache_block_size          |                                                      1.0 K |
| key_cache_division_limit      |                                                        100 |
| large_pages                   |                                                        OFF |
| locked_in_memory              |                                                        OFF |
| long_query_time               |                                                  10.000000 |
| max_allowed_packet            |                                                   67108864 |
| max_binlog_cache_size         |                                            17179869184.0 G |
| max_binlog_size               |                                                      1.0 G |
| max_connect_errors            |                                                        100 |
| max_connections               |                                                        151 |
| max_join_size                 |                                            17179869184.0 G |
| max_length_for_sort_data      |                                                       4096 |
| max_seeks_for_key             |                                       18446744073709551615 |
| max_sort_length               |                                                       1024 |
| max_user_connections          |                                                          0 |
| optimizer_prune_level         |                                                          1 |
| optimizer_search_depth        |                                                         62 |
| query_prealloc_size           |                                                      8.0 K |
| range_alloc_block_size        |                                                      4.0 K |
| read_buffer_size              |                                                    128.0 K |
| read_rnd_buffer_size          |                                                    256.0 K |
| sort_buffer_size              |                                                    256.0 K |
| sql_mode                      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
| thread_cache_size             |                                                        9 B |
| tmp_table_size                |                                                     16.0 M |
| wait_timeout                  |                                                      28800 |
+--------------------------------+------------------------------------------------------------+
===== OPTIMIZER SWITCH =====
+------------------------------------------+------------+
|               switch_name               |   value    |
+------------------------------------------+------------+
| index_merge                             |         on |
| index_merge_union                       |         on |
| index_merge_sort_union                  |         on |
| index_merge_intersection                |         on |
| engine_condition_pushdown               |         on |
| index_condition_pushdown                |         on |
| mrr                                     |         on |
| mrr_cost_based                          |         on |
| block_nested_loop                       |         on |
| batched_key_access                      |        off |
| materialization                         |         on |
| semijoin                                |         on |
| loosescan                               |         on |
| firstmatch                              |         on |
| duplicateweedout                        |         on |
| subquery_materialization_cost_based     |         on |
| use_index_extensions                    |         on |
| condition_fanout_filter                 |         on |
| derived_merge                           |         on |
| use_invisible_indexes                   |        off |
| skip_scan                               |         on |
| hash_join                               |         on |
+------------------------------------------+------------+
===== SQL PLAN =====
+--------+------------------+------------+------------+----------------------+------------+------------+------------+------------+------------+------------+
|   id  |   select_type   |   table   |    type   |    possible_keys    |    key    |  key_len  |    ref    |    rows   |  filtered |   Extra    |
+--------+------------------+------------+------------+----------------------+------------+------------+------------+------------+------------+------------+
|      1| SIMPLE          | dbinfo    | index               | NULL      | PRIMARY   | 4         |       None|          1| 100.0     |
|      1| SIMPLE          | mysql_slow_query_r| index               | NULL      | PRIMARY   | 64        |       None|          1| 100.0     |
+--------+------------------+------------+------------+----------------------+------------+------------+------------+------------+------------+------------+

===== OPTIMIZER REWRITE SQL =====
SELECT count(0) AS `count(*)`
FROM `slow_db`.`dbinfo`
JOIN `slow_db`.`mysql_slow_query_r`

===== OBJECT STATISTICS =====
+-----------------+------------+------------+------------+------------+------------+------------+------------+
|    table_name  |   engine  |   format  | table_rows|  avg_row  |  total_mb |  data_mb  |  index_mb  |
+-----------------+------------+------------+------------+------------+------------+------------+------------+
| slow_db.dbinfo | InnoDB    | Dynamic   |          0|          0|       0.02|       0.02|       0.00 |
+-----------------+------------+------------+------------+------------+------------+------------+------------+
+-----------------+------------+------------+------------+------------+------------+------------+------------+
|    table_name  |   engine  |   format  | table_rows|  avg_row  |  total_mb |  data_mb  |  index_mb  |
+-----------------+------------+------------+------------+------------+------------+------------+------------+
| slow_db.mysql_slow_query_r| InnoDB    | Dynamic   |          0|          0|       0.02|       0.02|       0.00 |
+-----------------+------------+------------+------------+------------+------------+------------+------------+
===== SESSION STATUS (DIFFERENT) =====
+-------------------------------------+-----------------+-----------------+-----------------+
|             status_name            |      before    |      after     |       diff      |
+-------------------------------------+-----------------+-----------------+-----------------+
| Bytes_received                     |             484|             743|           259.0 |
| Bytes_sent                         |             650|           10997|         10347.0 |
| Handler_commit                     |               0|               1|             1.0 |
| Handler_external_lock              |               1|               7|             6.0 |
| Handler_read_rnd                   |               0|             291|           291.0 |
| Handler_read_rnd_next              |             298|             590|           292.0 |
| Innodb_buffer_pool_read_requests   |        26484757|        26484761|             4.0 |
| Last_query_cost                    |        2.724000|        0.699000| -2.0250000000000004 |
| Last_query_partial_plans           |               1|               3|             2.0 |
| Open_tables                        |            1263|            1264|             1.0 |
| Opened_tables                      |               1|               2|             1.0 |
| Queries                            |          283714|          283716|             2.0 |
| Questions                          |               6|               8|             2.0 |
| Select_full_join                   |               0|               1|             1.0 |
| Select_scan                        |               2|               4|             2.0 |
| Sort_rows                          |             291|             582|           291.0 |
| Sort_scan                          |               1|               2|             1.0 |
| Table_locks_immediate              |            7578|            7579|             1.0 |
| Table_open_cache_hits              |               0|               2|             2.0 |
| Table_open_cache_misses            |               1|               2|             1.0 |
| Uptime                             |          494756|          494757|             1.0 |
| Uptime_since_flush_status          |          494756|          494757|             1.0 |
+-------------------------------------+-----------------+-----------------+-----------------+
===== SQL PROFILING(DETAIL)=====
+--------------------------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
|             state             | duration| cpu_user| cpu_sys |  bk_in  |  bk_out |  msg_s  |  msg_r  |  p_f_ma |  p_f_mi |  swaps   |
+--------------------------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| starting                      | 0.000024| 0.000021| 0.000000|        0|        0|        0|        0|        0|        0|        0 |
| query end                     | 0.000004| 0.000004| 0.000000|        0|        0|        0|        0|        0|        0|        0 |
| closing tables                | 0.000003| 0.000003| 0.000000|        0|        0|        0|        0|        0|        0|        0 |
| freeing items                 | 0.000010| 0.000010| 0.000000|        0|        0|        0|        0|        0|        0|        0 |
| cleaning up                   | 0.000007| 0.000007| 0.000000|        0|        0|        0|        0|        0|        0|        0 |
+--------------------------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
bk_in:   block_ops_in
bk_out:  block_ops_out
msg_s:   message sent
msg_r:   message received
p_f_ma:  page_faults_major
p_f_mi:  page_faults_minor
===== SQL PROFILING(SUMMARY)=====
+-------------------------------------+-----------------+------------+-------+-----------------+
|                state               |     total_r    |   pct_r   | calls|      r/call     |
+-------------------------------------+-----------------+------------+-------+-----------------+
| starting                           |        0.000024|      50.00|     1|    0.0000240000 |
| freeing items                      |        0.000010|      20.83|     1|    0.0000100000 |
| cleaning up                        |        0.000007|      14.58|     1|    0.0000070000 |
| query end                          |        0.000004|       8.33|     1|    0.0000040000 |
| closing tables                     |        0.000003|       6.25|     1|    0.0000030000 |
+-------------------------------------+-----------------+------------+-------+-----------------+
===== EXECUTE TIME =====
0 day 0 hour 0 minute 0 second 266 microsecond