mysql ----show profile

show profile:
    是mysql提供可以用来分析当前会话中语句执行的资源消耗的情况,可以用于sql的调优的测量。
    默认状态下参数处于关闭状态,并保存最近15次的运行结果。
    mysql> show variables like '%profil%';
        +------------------------+-------+
        | Variable_name          | Value |
        +------------------------+-------+
        | have_profiling         | YES   |
        | profiling              | OFF   |
        | profiling_history_size | 15    |
        +------------------------+-------+
        3 rows in set, 1 warning (0.01 sec)
    
    开关开启后就能拿到最近执行的sql语句了。
    mysql> show profiles;
        +----------+------------+--------------------------------+
        | Query_ID | Duration   | Query                          |
        +----------+------------+--------------------------------+
        |        1 | 0.05794450 | show variables like '%profil%' |
        |        2 | 0.40223375 | select * from stu              |
        |        3 | 1.00215675 | select sleep(1)                |
        |        4 | 0.19771625 | show tables                    |
        |        5 | 0.15127925 | select count(*) from employee  |
        +----------+------------+--------------------------------+
        5 rows in set, 1 warning (0.00 sec)
     
     查看执行query 3是cpu 和 io的信息
    mysql> show profile cpu ,block io for query 3;
    +----------------------+----------+----------+------------+--------------+---------------+
    | Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
    +----------------------+----------+----------+------------+--------------+---------------+
    | starting             | 0.000385 | 0.000000 |   0.000000 |         NULL |          NULL |
    | checking permissions | 0.000011 | 0.000000 |   0.000000 |         NULL |          NULL |
    | Opening tables       | 0.000039 | 0.000000 |   0.000000 |         NULL |          NULL |
    | init                 | 0.000013 | 0.000000 |   0.000000 |         NULL |          NULL |
    | optimizing           | 0.000016 | 0.000000 |   0.000000 |         NULL |          NULL |
    | executing            | 0.000022 | 0.000000 |   0.000000 |         NULL |          NULL |
    | User 
    | 1.000380 | 0.000000 |   0.000000 |         NULL |          NULL |
    | end                  | 0.000028 | 0.000000 |   0.000000 |         NULL |          NULL |
    | query end            | 0.000014 | 0.000000 |   0.000000 |         NULL |          NULL |
    | closing tables       | 0.000008 | 0.000000 |   0.000000 |         NULL |          NULL |
    | freeing items        | 0.000204 | 0.000000 |   0.000000 |         NULL |          NULL |
    | logging slow query   | 0.000881 | 0.000000 |   0.000000 |         NULL |          NULL |
    | cleaning up          | 0.000159 | 0.000000 |   0.000000 |         NULL |          NULL |
    +----------------------+----------+----------+------------+--------------+---------------+
    13 rows in set, 1 warning (0.00 sec)
    
    
    show profile 查看所有信息。
    如果在Status结果中出现:以下的信息那么这条sql就有优化的必要。
           coverting heap to MyISAM :查询结果太大,内存不够用了。
           creating tmp table:创建了临时表
           copying to temp table on disk:把内存中的信息赋值到磁盘
           locked
    mysql> show profile all for query 3;
    +----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+--------------------------+----------------------+-------------+
    | Status               | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent | Messages_received | Page_faults_major | Page_faults_minor | Swaps | Source_function          | Source_file          | Source_line |
    +----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+--------------------------+----------------------+-------------+
    | starting             | 0.000385 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | NULL                     | NULL                 |        NULL |
    | checking permissions | 0.000011 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | check_access             | sql_authorization.cc |        2202 |
    | Opening tables       | 0.000039 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | open_tables              | sql_base.cc          |        5587 |
    | init                 | 0.000013 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | Sql_cmd_dml::execute     | sql_select.cc        |         661 |
    | optimizing           | 0.000016 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | JOIN::optimize           | sql_optimizer.cc     |         213 |
    | executing            | 0.000022 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | JOIN::exec               | sql_executor.cc      |         228 |
    | User sleep           | 1.000380 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | Item_func_sleep::val_int | item_func.cc         |        4989 |
    | end                  | 0.000028 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | Sql_cmd_dml::execute     | sql_select.cc        |         714 |
    | query end            | 0.000014 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | mysql_execute_command    | sql_parse.cc         |        4520 |
    | closing tables       | 0.000008 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | mysql_execute_command    | sql_parse.cc         |        4566 |
    | freeing items        | 0.000204 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | mysql_parse              | sql_parse.cc         |        5237 |
    | logging slow query   | 0.000881 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | log_slow_do              | log.cc               |        1619 |
    | cleaning up          | 0.000159 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |          NULL |              NULL |              NULL |              NULL |  NULL | dispatch_command         | sql_parse.cc         |        2147 |
 

发布了76 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/shen_chengfeng/article/details/104663064
今日推荐