码迷,mamicode.com
首页 > 数据库 > 详细

SQL优化

时间:2016-04-29 20:21:28      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:优化   sql   

下载  Example Databases :http://dev.mysql.com/doc/index-other.html  sakila-db.zip

mysql> show status like "Com_select"; show status like "Com_insert"; show status like "Com_update"; show status like "Com_delete";

mysql> 

mysql> show status like "Innodb_rows_%";

+----------------------+-------+

| Variable_name        | Value |

+----------------------+-------+

| Innodb_rows_deleted  | 0     |

| Innodb_rows_inserted | 47280 |

| Innodb_rows_read     | 62    |

| Innodb_rows_updated  | 0     |

+----------------------+-------+

4 rows in set (0.00 sec)

mysql> 

mysql> show status like "Connections";show status like "Uptime"; show status like "Slow_%";

mysql> explain select * from  customer where customer_id=130;     


mysql> select @@have_profiling;

+------------------+

| @@have_profiling |

+------------------+

| YES              |

+------------------+

1 row in set, 1 warning (0.00 sec)

mysql> 

mysql> select @@profiling;

+-------------+

| @@profiling |

+-------------+

|           0 |

+-------------+

1 row in set, 1 warning (0.00 sec)


mysql> set profiling=1;

Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> select @@profiling;

+-------------+

| @@profiling |

+-------------+

|           1 |

+-------------+

1 row in set, 1 warning (0.00 sec)


mysql> 

mysql> select count(*) from payment;

+----------+

| count(*) |

+----------+

|    16049 |

+----------+

1 row in set (0.00 sec)


mysql> show profiles;

+----------+------------+------------------------------+

| Query_ID | Duration   | Query                        |

+----------+------------+------------------------------+

|        1 | 0.00022100 | select @@profiling           |

|        2 | 0.00073525 | show tables                  |

|        3 | 0.00203800 | select count(*) from payment |

+----------+------------+------------------------------+

3 rows in set, 1 warning (0.00 sec)


mysql> show profile for query 3;

+----------------------+----------+

| Status               | Duration |

+----------------------+----------+

| starting             | 0.000032 |

| checking permissions | 0.000003 |

| Opening tables       | 0.000011 |

| init                 | 0.000007 |

| System lock          | 0.000005 |

| optimizing           | 0.000007 |

| statistics           | 0.000007 |

| preparing            | 0.000008 |

| executing            | 0.000001 |

| Sending data         | 0.001884 |

| end                  | 0.000003 |

| query end            | 0.000003 |

| closing tables       | 0.000005 |

| freeing items        | 0.000056 |

| cleaning up          | 0.000008 |

+----------------------+----------+

15 rows in set, 1 warning (0.00 sec)


mysql> 


mysql> show profile all  for query 3;

mysql> show profile cpu  for query 3;  


mysql> create table payment_myisam like payment;

Query OK, 0 rows affected (0.43 sec)


mysql> alter table payment_myisam engine=myisam;

Query OK, 0 rows affected (0.41 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> insert into payment_myisam select * from payment; 

Query OK, 16049 rows affected, 1 warning (0.05 sec)

Records: 16049  Duplicates: 0  Warnings: 1


mysql> 

mysql> select count(*) from payment_myisam;

+----------+

| count(*) |

+----------+

|    16049 |

+----------+

1 row in set (0.00 sec)


mysql> show profiles;

mysql> show profile for query 14;

+----------------------+----------+

| Status               | Duration |

+----------------------+----------+

| starting             | 0.000083 |

| checking permissions | 0.000011 |

| Opening tables       | 0.000028 |

| init                 | 0.000021 |

| System lock          | 0.000017 |

| optimizing           | 0.000012 |

| executing            | 0.000016 |

| end                  | 0.000006 |

| query end            | 0.000005 |

| closing tables       | 0.000065 |

| freeing items        | 0.000021 |

| cleaning up          | 0.000043 |

+----------------------+----------+

12 rows in set, 1 warning (0.00 sec)



mysql> show profile  source for query 14;

+----------------------+----------+-----------------------+------------------+-------------+

| Status               | Duration | Source_function       | Source_file      | Source_line |

+----------------------+----------+-----------------------+------------------+-------------+

| starting             | 0.000083 | NULL                  | NULL             |        NULL |

| checking permissions | 0.000011 | check_access          | sql_parse.cc     |        5296 |

| Opening tables       | 0.000028 | open_tables           | sql_base.cc      |        5094 |

| init                 | 0.000021 | mysql_prepare_select  | sql_select.cc    |        1051 |

| System lock          | 0.000017 | mysql_lock_tables     | lock.cc          |         304 |

| optimizing           | 0.000012 | optimize              | sql_optimizer.cc |         138 |

| executing            | 0.000016 | exec                  | sql_executor.cc  |         110 |

| end                  | 0.000006 | mysql_execute_select  | sql_select.cc    |        1106 |

| query end            | 0.000005 | mysql_execute_command | sql_parse.cc     |        4995 |

| closing tables       | 0.000065 | mysql_execute_command | sql_parse.cc     |        5043 |

| freeing items        | 0.000021 | mysql_parse           | sql_parse.cc     |        6432 |

| cleaning up          | 0.000043 | dispatch_command      | sql_parse.cc     |        1777 |

+----------------------+----------+-----------------------+------------------+-------------+

12 rows in set, 1 warning (0.00 sec)


mysql> 






SQL优化

标签:优化   sql   

原文地址:http://wangqh.blog.51cto.com/5367393/1768953

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!