博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
记录mysql性能查询过程
阅读量:6886 次
发布时间:2019-06-27

本文共 2215 字,大约阅读时间需要 7 分钟。

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

表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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慢)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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

具体使用可以参考:

使用方法简单来说:

1
2
3
4
5
Mysql > set profiling = 1;
 
Mysql>show profiles;
 
Mysql>show profile
for 
query 1;

 

这个数据中就可以看到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的用法

 

本文转自轩脉刃博客园博客,原文链接:http://www.cnblogs.com/yjf512/archive/2012/10/10/2717857.html,如需转载请自行联系原作者

你可能感兴趣的文章
维护没有源代码的遗留 Java 项目
查看>>
早期乳腺增生案
查看>>
Android -- ViewDragHelper
查看>>
Git凭证存储(简单易懂,一学就会,认真看)
查看>>
QTP:General Error while saving the test 的解决方法
查看>>
AP_INVOICES_ALL 表结构
查看>>
android 增加Add-on属性支持的方法
查看>>
IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用
查看>>
竞争格局、本土机会、未来发展—–深度解读MIPS收购案(转)
查看>>
JS键盘码
查看>>
JS伪3D 图形透视效果
查看>>
poj 3294 Life Forms
查看>>
WCF RIA Services使用详解(转载)
查看>>
Spring整合Hibernate的步骤
查看>>
CSS背景图拉伸不变形
查看>>
求訪问啊啊啊啊
查看>>
【iCore3 双核心板】例程二十七:DMA LAN实验——高速数据传输测速
查看>>
SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式...
查看>>
***IT程序员自我修养和情商提升文章
查看>>
Oracle恢复已删除数据
查看>>