Mysql SQL Performance Analyzer

Source: Internet
Author: User

Author: skate
Time: 2012/02/17
 
Mysql SQL Performance Analyzer
 
The SQL Performance Analyzer of MySQL is mainly used to display the usage of various resources during SQL Execution. Analyzer can better display
Poor SQL Performance problems.
 
How to use mysql SQL profile
 
1. Enable mysql SQL profile
 
Check whether mysql SQL profile is enabled
Mysql> select @ profiling;
+ ------------- +
| @ Profiling |
+ ------------- +
| 0 |
+ ------------- +
1 row in set (0.01 sec)
 
By default, the value of profiling is 0, indicating that MySQL SQL Profiler is OFF. If SQL Performance Analyzer is enabled, the value of profiling is 1.
 
Mysql> set profiling = 1;
Query OK, 0 rows affected (0.03 sec)
 
Mysql> select @ profiling;
+ ------------- +
| @ Profiling |
+ ------------- +
| 1 |
+ ------------- +
1 row in set (0.01 sec)
 
 
As shown above, profiling has changed to 1, but this is session-level and is not supported by the system. Test
 
Exit mysql
Mysql> quit
Bye
[Root @ localhost ~] # Mysql-uroot-p
Enter password:
Welcome to the MySQL monitor. Commands end with; or \ g.
Your MySQL connection id is 3
Server version: 5.0.45-log Source distribution
 
Type 'help; 'or' \ H' for help. Type '\ C' to clear the buffer.
 
View profiling Value
Mysql> select @ profiling;
+ ------------- +
| @ Profiling |
+ ------------- +
| 0 |
+ ------------- +
1 row in set (0.01 sec)
 
If the default value is 0, what if the system level is set?
 
Mysql> set global profiling = 1;
ERROR 1228 (HY000): Variable 'profiling' is a SESSION variable and can't be used with SET GLOBAL
Mysql>
 
An error is reported here. Therefore, mysql SQL profile is session-level.
 
2. For example, how to use
 
Mysql> create table t5 as select * from t1;
ERROR 1046 (3D000): No database selected
Mysql> use backup;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with-
 
Database changed
Mysql> create table t5 as select * from t1;
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0
 
Mysql> select count (*) from t5;
+ ---------- +
| Count (*) |
+ ---------- +
| 2 |
+ ---------- +
1 row in set (0.00 sec)
 
Mysql> select count (*) from t5;
+ ---------- +
| Count (*) |
+ ---------- +
| 2 |
+ ---------- +
1 row in set (0.00 sec)
 
Mysql> show profiles;
+ ---------- + ------------ + --------------------------------------- +
| Query_ID | Duration | Query |
+ ---------- + ------------ + --------------------------------------- +
| 1 | 0.00382400 | select @ profiling |
| 2 | 0.00268500 | create table t5 as select * from t1 |
| 2 | 0.00017200 | select database () |
| 4 | 0.01985400 | show databases |
| 5 | 0.00018900 | show tables |
| 6 | 0.06225200 | create table t5 as select * from t1 |
| 7 | 0.00368800 | select count (*) from t5 |
| 8 | 0.00322200 | select count (*) from t5 |
+ ---------- + ------------ + --------------------------------------- +
8 rows in set (0.01 sec)
 
Mysql>
Mysql> show profile for query 7;
+ -------------------- + ---------- +
| Status | Duration |
+ -------------------- + ---------- +
| (Initialization) | 0.000414 |
| Opening tables | 0.000599 |
| System lock | 0.000254 |
| Table lock | 0.000175 |
| Init | 1, 0.000052 |
| Optimizing | 0.00001 |
| Executing | 0.002107 |
| End| 0.000042 |
| Query end | 0.000005 |
| Freeing items | 0.000014 |
| Closing tables | 0.000011 |
| Logging slow query | 0.000005 |
+ -------------------- + ---------- +
12 rows in set (0.03 sec)
 
Mysql> show profile for query 8;
+ -------------------- + ---------- +
| Status | Duration |
+ -------------------- + ---------- +
| (Initialization) | 0.000064 |
| Opening tables | 0.000018 |
| System lock | 0.00001 |
| Table lock | 0.000013 |
| Init | 1, 0.00002 |
| Optimizing | 0.00001 |
| Executing | 0.002589 |
| End| 0.000459 |
| Query end | 0.000007 |
| Freeing items | 0.000015 |
| Closing tables | 0.000012 |
| Logging slow query | 0.000005 |
+ -------------------- + ---------- +
12 rows in set (0.00 sec)
 
Mysql> select sum (format (duration, 6) as duration from information_schema.profiling where query_id = 7;
+ ---------- +
| Duration |
+ ---------- +
| 1, 0.003688 |
+ ---------- +
1 row in set (0.02 sec)
 
Mysql> select sum (format (duration, 6) as duration from information_schema.profiling where query_id = 8;
+ ---------- +
| Duration |
+ ---------- +
| 1, 0.003222 |
+ ---------- +
1 row in set (0.00 sec)
 
Mysql>
 
From the above information, we can see that the profile statistics of the two sqls differ greatly from the first four. This is the main difference between the two sqls, and there are many
Cached. The SQL Performance Analyzer can help us diagnose SQL statements that are difficult to identify performance problems and find out the root cause of the problem.
 
 
------ End -----

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.