当前位置: 移动技术网 > IT编程>开发语言>PHP > 记录mysql性能查询过程的使用方法

记录mysql性能查询过程的使用方法

2019年06月05日  | 移动技术网IT编程  | 我要评论

一切源于一个实验,请看下面的例子:

表:

复制代码 代码如下:

create table if not exists `foo` (
`a` int(10) unsigned not null auto_increment,
`b` int(10) unsigned not null,
`c` varchar(100) not null,
primary key (`a`),
key `bar` (`b`,`a`)
) engine=innodb;

create table if not exists `foo2` (
`a` int(10) unsigned not null auto_increment,
`b` int(10) unsigned not null,
`c` varchar(100) not null,
primary key (`a`),
key `bar` (`b`,`a`)

) engine=myisam;

我往两个表中插入了30w的数据(插入的时候性能差别innodb比myisam慢)

复制代码 代码如下:

<?php

$host = '192.168.100.166';

$dbname = 'test';

$user = 'root';

$password = '';

$db = mysql_connect($host, $user, $password) or die('db connect failed');

mysql_select_db($dbname, $db);

echo '===================innodb=======================' . "\r\n";

$start = microtime(true);

mysql_query("select sql_no_cache sql_calc_found_rows * from foo where b = 1 limit 1000, 10");

$end = microtime(true);

echo $end - $start . "\r\n";

echo '===================myisam=======================' . "\r\n";

$start = microtime(true);

mysql_query("select sql_no_cache sql_calc_found_rows * from foo2 where b = 1 limit 1000, 10");

$end = microtime(true);

echo $end - $start . "\r\n";

返回结果:


一次查询就会差别这么多!!innodb和myisam,赶紧分析分析为什么。

首先是使用explain来进行查看

确定两边都没有使用index,第二个查询查的rows,并且myisam的查询rows还比innodb少这么多,反而是查询慢于innodb!!这y的有点奇怪。

 

没事,还有一个牛掰工具profile

具体使用可以参考:http://dev.mysql.com/doc/refman/5.0/en/show-profile.html

使用方法简单来说:

复制代码 代码如下:

mysql > set profiling = 1;

mysql>show profiles;

mysql>show profile for query 1;


这个数据中就可以看到myisam的sending data比innodb的sending data费时太多了。查看mysql文档

http://dev.mysql.com/doc/refman/5.0/en/general-thread-states.html

 

sending data

the thread is reading and processing rows for a select statement, and sending data to the client. because operations occurring during this this state tend to perform large amounts of disk access (reads), it is often the longest-running state over the lifetime of a given query.
 

sending data是去磁盘中读取select的结果,然后将结果返回给客户端。这个过程会有大量的io操作。你可以使用show profile cpu for query xx;来进行查看,发现myisam的cpu_system比innnodb大很多。至此可以得出结论是myisam进行表查询(区别仅仅使用索引就可以完成的查询)比innodb慢。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网