发布于 2014-11-20 00:54:42 | 154 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Mysql教程,程序狗速度看过来!

Mysql关系型数据库管理系统

MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司。MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。


本文是一篇mysql使用笔记为大家 讲解的是mysql查询性能过程分析,感兴趣的同学参考下。

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

表:


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=======================' . "rn";

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

echo '===================MyISAM=======================' . "rn";

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


返回结果:


一次查询就会差别这么多!!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慢。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务