記錄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的數(shù)據(jù)(插入的時候性能差別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";
返回結(jié)果:

一次查詢就會差別這么多??!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;

這個數(shù)據(jù)中就可以看到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的結(jié)果,然后將結(jié)果返回給客戶端。這個過程會有大量的IO操作。你可以使用show profile cpu for query XX;來進行查看,發(fā)現(xiàn)MyISAM的CPU_system比InnnoDB大很多。至此可以得出結(jié)論是MyISAM進行表查詢(區(qū)別僅僅使用索引就可以完成的查詢)比InnoDB慢。
- MySql 按時間段查詢數(shù)據(jù)方法(實例說明)
- mysql如何查詢某一時間段內(nèi)沒有賣出的商品
- mysql 按照時間段來獲取數(shù)據(jù)的方法
- 清空mysql 查詢緩存的可行方法
- MySQL查詢隨機數(shù)據(jù)的4種方法和性能對比
- mysql隨機查詢?nèi)舾蓷l數(shù)據(jù)的方法
- MySql實現(xiàn)跨表查詢的方法詳解
- Mysql中分頁查詢的兩個解決方法比較
- mysql嵌套查詢和聯(lián)表查詢優(yōu)化方法
- MySQL大表中重復(fù)字段的高效率查詢方法
- MySQL 查詢結(jié)果取交集的實現(xiàn)方法
- MySql查詢時間段的方法
相關(guān)文章
Symfony2實現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法
這篇文章主要介紹了Symfony2實現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法,結(jié)合實例形式分析了在doctrine中內(nèi)置數(shù)據(jù)的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02
laravel 操作數(shù)據(jù)庫常用函數(shù)的返回值方法
今天小編就為大家分享一篇laravel 操作數(shù)據(jù)庫常用函數(shù)的返回值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
php curl抓取網(wǎng)頁的介紹和推廣及使用CURL抓取淘寶頁面集成方法
抓取網(wǎng)頁內(nèi)容,分析網(wǎng)頁數(shù)據(jù)經(jīng)常使用php curl,簡潔易用,本篇文章通過代碼實例給大家講解 php curl抓取網(wǎng)頁的介紹和推廣及使用CURL抓取淘寶頁面集成方法,需要的朋友參考下2015-11-11
php 模擬post_驗證頁面的返回狀態(tài)(實例講解)
php模擬post_驗證頁面的返回狀態(tài)(實例講解)。需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10

