mysql的性能记录在_记录mysql性能查询过程

本文通过实验对比InnoDB和MyISAM在处理30万数据查询时的性能差异,发现MyISAM在发送数据阶段耗时更多,CPU系统占用高,解释了为何即使使用索引,查询速度InnoDB更快。深入分析了查询过程中的I/O操作和源码原理。
摘要由CSDN通过智能技术生成

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

表: 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慢)

$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";

返回结果:

40d7ed1017fb94c5bd3a96b45e3813b0.png

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

首先是使用explain来进行查看

29115d7c423ab1af833ac73c039ca45c.png

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

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

使用方法简单来说:

Mysql > set profiling = 1;

Mysql>show profiles;

Mysql>show profile for query 1;

ed05fcf08a5c35d4de6d34fc4ec08087.png

这个数据中就可以看到MyISAM的Sending data比InnoDB的Sending data费时太多了。查看mysql文档

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慢。

至于再往下的为什么,我想就需要看源码了..于是,就此打住。

附带一篇文章,里面还有status的用法

参考文章:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值