MySQL query optimization: Show profile

Source: Internet
Author: User
Tags mysql query switches mysql query optimization

Query Profiler is a query diagnostic analysis tool that comes with MySQL to analyze where the performance bottlenecks of an SQL statement are. Usually we are using the explain, and slow query log is not accurate analysis, but query Profiler can locate a SQL statement execution of the various resource consumption, such as Cpu,io, and the time spent on SQL execution, and so on. However, this tool is only available in MySQL 5.0.37 and in the above versions.
by default, the MySQL feature is not turned on and needs to be started manually . You can see whether the current MySQL server is turned on by the following methods.

MySQL find SQL time-consuming bottleneck SHOW profiles

1, but the version should be after 5.0.37. (Show PROFILES and show profile were added in MySQL 5.0.37.)

SELECT @ @profiling;

To see if profile is enabled, and if the PROFILNG value is 0, you can

SET profiling = 1;

To enable. When profiling is enabled, we execute a query statement, such as:

Select COUNT (*) from roi_summary;

Then show Profiles view as follows:

+----------------+------------+----------------------------------+| query_id       | Duration   | Query                            |+----------------+------------+----------------------------------+|        1       | 0.00021500 | SELECT @ @profiling               | |        2       | 0.05522700 | Select COUNT (*) from roi_summary |+----------------+------------+------------------------------- ---+

2 rows in Set (0.00 sec)

Where the statement with ID 5 is the query statement that was just executed


2, variable profiling is a user variable, each time you have to re-enable.
Here are some of the experiments I did. The data is obvious, and it's not much to explain.

mysql> use testdatabase changedmysql> set profiling=1; Query OK, 0 rows Affected (0.00 sec) mysql> Show tables;+----------------+| Tables_in_test |+----------------+| Bag_item | | Bag_user | | Score | | T | +----------------+4 rows in Set (0.03 sec) mysql> Select COUNT (*) from t;+----------+| COUNT (*) |+----------+| 2097152 | +----------+1 row in Set (0.74 sec) mysql> Show profiles;+----------+------------+------------------------+| query_id | Duration |        Query |+----------+------------+------------------------+| 1 | 0.02717000 |        Show Tables | | 2 | 0.74770100 | Select COUNT (*) from T | +----------+------------+------------------------+2 rows in Set (0.00 sec) mysql> Show profiles for query 2;+---------- ----------------------+----------+| Status | Duration |+--------------------------------+----------+| (initialization) | 0.000004 | | Checking query cache for Query | 0.000044 | | Opening tables                 | 0.000012 | | System Lock | 0.000017 | | Table Lock | 0.00003 | | init | 0.000013 | | Optimizing | 0.000008 | | Statistics | 0.000013 | | Preparing | 0.000011 | | Executing | 0.000006 | | Sending Data | 0.747313 | | End | 0.000014 | | Query End | 0.000006 | | Storing result in query cache | 0.000006 | | Freeing items | 0.000012 | | Closing Tables | 0.000009 | | Logging Slow Query | 0.000183 | +--------------------------------+----------+17 rows in Set (0.00 sec) mysql> Show Profiles block io,cpu for query 2;+--- -----------------------------+----------+----------+------------+--------------+---------------+| Status | Duration | Cpu_user | Cpu_system | block_ops_in | Block_ops_out |+--------------------------------+----------+----------+------------+--------------+---------------+| (initialization) | 0.000004 | 0 |            0 |             0 | 0 | | Checking query cache for Query | 0.000044 | 0 |            0 |             0 | 0 | | Opening Tables | 0.000012 | 0 |            0 |             0 | 0 | | System Lock | 0.000017 | 0 |            0 |             0 | 0 | | Table Lock | 0.00003 | 0 |            0 |             0 | 0 | | init | 0.000013 | 0 |            0 |             0 | 0 | | Optimizing | 0.000008 | 0 |            0 |             0 | 0 | | Statistics | 0.000013 | 0 |            0 |             0 | 0 | | Preparing | 0.000011 | 0 |            0 |             0 | 0 | | Executing | 0.000006 | 0 |            0 |             0 | 0 | | Sending Data | 0.747313 | 0.746887 |            0 |             0 | 0 | | End | 0.000014 | 0 |            0 |             0 | 0 | | Query End | 0.000006 | 0 |            0 |             0 | 0 | | Storing result in query cache | 0.000006 | 0 |            0 |             0 | 0 | | Freeing items | 0.000012 | 0 |            0 |             0 | 0 | | Closing Tables | 0.000009 | 0 |            0 |             0 | 0 | | Logging Slow Query | 0.000183 | 0 |            0 |             0 | 0 | +--------------------------------+----------+----------+------------+--------------+---------------+17 rows in Set (0.00 sec) mysql> insert into T (username) select username from T; Query OK, 2097152 rows Affected (34.17 sec) records:2097152 duplicates:0 warnings:0mysql> show profiles;+----------+-------- -----+------------------------------------------------+| query_id | Duration | Query |+----------+-------------+------------------------------------------------        +|  1 | 0.02717000 |        Show Tables | |  2 | 0.74770100 |        Select COUNT (*) from T | |  3 | 0.00004200 |        Show Prifile for Query 2 | | 4 | 34.30410100 | INSERT into T (username) select username from T | +----------+-------------+------------------------------------------------+4 rows in Set (0.00 sec) Mysql> Show Profile Cpu,block io,memory,swaps for query 4;mysql> select COUNT (*) from t;+----------+|  COUNT (*) |+----------+| 4194304 | +----------+1 row in Set (1.51 sec) mysql> Show profiles;+----------+-------------+-------------------------------- ----------------+| query_id | Duration | Query |+----------+-------------+------------------------------------------------        +|  1 | 0.02717000 |        Show Tables | |  2 | 0.74770100 |        Select COUNT (*) from T | |  3 | 0.00004200 |        Show Prifile for Query 2 | | 4 | 34.30410100 |        INSERT into T (username) select username from T | |  5 | 1.50563800 | Select COUNT (*) from T | +----------+-------------+------------------------------------------------+5 rows in Set (0.00 sec) Mysql> Show Profile Cpu,block io,memory,swaps,context switches,source for query 5;......mysql> update t set username = ' Waill '; Query OK, 4194304 rows Affected (44.82 sec) rows matched:4194304 changed:4194304 warnings:0mysql> show profiles;+-- --------+-------------+------------------------------------------------+| query_id | Duration |         Query                                 |+----------+-------------+------------------------------------------------+|  1 | 0.02717000 |        Show Tables | |  2 | 0.74770100 |        Select COUNT (*) from T | |  3 | 0.00004200 |        Show Prifile for Query 2 | | 4 | 34.30410100 |        INSERT into T (username) select username from T | |  5 | 1.50563800 |        Select COUNT (*) from T | | 6 | 44.82054700 | Update T set username = ' Waill ' | +----------+-------------+------------------------------------------------+6 rows in Set (0.00 sec) Mysql> Show Profile Cpu,block Io,memory,swaps,context switches,source for query 6;
Mysql> Show profile all for query 6;

MySQL query optimization: Show profile

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.