MySQL调优(一)未完成

Server 处理 Client 的请求过程

image-20210514091715754

性能监控

SHOW PROFILE 分析SQL执行耗时

  1. 开启分析

    1
    set profiling=1;
  2. 运行查询语句

    1
    2
    select * from user;
    select * from category;
  3. 查看查询语句耗时

    1
    2
    3
    4
    5
    6
    7
    show profiles;
    +----------+------------+------------------------+
    | Query_ID | Duration | Query |
    +----------+------------+------------------------+
    | 1 | 0.00019575 | select * from user |
    | 2 | 0.00018800 | select * from category |
    +----------+------------+------------------------+
  4. 查询单个语句的详细信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    show profile for query 1;
    +----------------------+----------+
    | Status | Duration |
    +----------------------+----------+
    | starting | 0.000052 |
    | checking permissions | 0.000005 |
    | Opening tables | 0.000014 |
    | init | 0.000014 |
    | System lock | 0.000006 |
    | optimizing | 0.000003 |
    | statistics | 0.000008 |
    | preparing | 0.000006 |
    | executing | 0.000002 |
    | Sending data | 0.000034 |
    | end | 0.000003 |
    | query end | 0.000004 |
    | closing tables | 0.000006 |
    | freeing items | 0.000031 |
    | cleaning up | 0.000008 |
    +----------------------+----------+
  5. 查看sql相关的所有分析【主要看i/o与cpu】

    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
    show profile all for query 1 \G;
    *************************** 1. row ***************************
    Status: starting
    Duration: 0.000052
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: NULL
    Source_file: NULL
    Source_line: NULL
    *************************** 2. row ***************************
    Status: checking permissions
    Duration: 0.000005
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_parse.cc
    Source_line: 5266
    *************************** 3. row ***************************
    Status: Opening tables
    Duration: 0.000014
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_base.cc
    Source_line: 5018
    *************************** 4. row ***************************
    Status: init
    Duration: 0.000014
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_select.cc
    Source_line: 1050
    *************************** 5. row ***************************
    Status: System lock
    Duration: 0.000006
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: lock.cc
    Source_line: 304
    *************************** 6. row ***************************
    Status: optimizing
    Duration: 0.000003
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_optimizer.cc
    Source_line: 138
    *************************** 7. row ***************************
    Status: statistics
    Duration: 0.000008
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_optimizer.cc
    Source_line: 362
    *************************** 8. row ***************************
    Status: preparing
    Duration: 0.000006
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_optimizer.cc
    Source_line: 485
    *************************** 9. row ***************************
    Status: executing
    Duration: 0.000002
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_executor.cc
    Source_line: 110
    *************************** 10. row ***************************
    Status: Sending data
    Duration: 0.000034
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_executor.cc
    Source_line: 190
    *************************** 11. row ***************************
    Status: end
    Duration: 0.000003
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_select.cc
    Source_line: 1105
    *************************** 12. row ***************************
    Status: query end
    Duration: 0.000004
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_parse.cc
    Source_line: 4965
    *************************** 13. row ***************************
    Status: closing tables
    Duration: 0.000006
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_parse.cc
    Source_line: 5013
    *************************** 14. row ***************************
    Status: freeing items
    Duration: 0.000031
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_parse.cc
    Source_line: 6404
    *************************** 15. row ***************************
    Status: cleaning up
    Duration: 0.000008
    CPU_user: 0.000000
    CPU_system: 0.000000
    Context_voluntary: NULL
    Context_involuntary: NULL
    Block_ops_in: NULL
    Block_ops_out: NULL
    Messages_sent: NULL
    Messages_received: NULL
    Page_faults_major: NULL
    Page_faults_minor: NULL
    Swaps: NULL
    Source_function: <unknown>
    Source_file: sql_parse.cc
    Source_line: 1772
  6. 关闭分析

    1
    set profiling=1;

MySQL Performance Schema

详细可以查看官方文档

查看线程连接个数 SHOW PROCESSLIST

1
2
3
4
5
6
+----+------+-----------------+----------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+----------+---------+------+-------+------------------+
| 45 | root | localhost:11156 | bugstack | Sleep | 210 | | NULL |
| 46 | root | localhost:13537 | NULL | Query | 0 | init | show processlist |
+----+------+-----------------+----------+---------+------+-------+------------------+
  • id:表示session id
  • user:表示操作的用户
  • host:表示操作的主机
  • db:表示操作的数据库
  • command:表示命令类型
  • info:表示详细的sql语句
  • time:表示相应命令执行时间
  • state:表示命令执行状态