自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

小記MySQL的mysql-udf-http效率測試

數(shù)據(jù)庫 MySQL
看到張宴的博客上關(guān)于"http/rest客戶端的文章",怎樣安裝啥的直接都跳過,下面直接進(jìn)入測試階段。

看到張宴的博客上關(guān)于"http/rest客戶端的文章",怎樣安裝啥的直接都跳過,下面直接進(jìn)入測試階段,測試環(huán)境:虛擬機(jī)

  1. [root@localhost ~]# uname -a  
  2. Linux sunss 2.6.18-128.el5 #1 SMP Wed Jan 21 10:44:23 EST 2009 i686 i686 i386 GNU/Linux  
內(nèi)存和交換分區(qū):
  1. [root@localhost ~]# free -m  
  2.              total       used       free     shared    buffers     cached  
  3. Mem:           376        363         13          0         23        105  
  4. -/+ buffers/cache:        233        142  
  5. Swap:         1023        133        890  
  6. mysql:  
  7. [root@localhost ~]# mysql -u root -p  
  8. Enter password:   
  9. Welcome to the MySQL monitor. Commands end with ; or \g.  
  10. Your MySQL connection id is 57  
  11. Server version: 5.1.26-rc-log Source distribution  
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  
  14. mysql>  
使用的表結(jié)構(gòu):
  1. DROP TABLE IF EXISTS `mytable`;  
  2.  
  3. CREATE TABLE `mytable` (  
  4.  `id` int(10) NOT NULL AUTO_INCREMENT,  
  5.  `addtime` int(10) NOT NULL,  
  6.  `title` varchar(255) NOT NULL,  
  7.  PRIMARY KEY (`id`)  
  8. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  
php操作MySQL的程序:
  1. <?php  
  2.     $type = $_GET['type'];  
  3.     print_r($_GET);  
  4.     include_once("gettime.php");  
  5.     $btime = getmicrotime();  
  6.     $loop_cnt= 1000; //循環(huán)次數(shù)  
  7.     $db_host = '127.0.0.1'//  
  8.     $db_user = 'sunss'//  
  9.     $db_pass = '123456'//  
  10.     $db_name = 'test'//  
  11.     $db_link = mysql_connect($db_host$db_user$db_passor die("Connected failed: ".mysql_error()."\n");  
  12.     mysql_query('set names utf8');  
  13.     mysql_db_query($db_name$db_link);  
  14.     if ("put" == $type) {//修改  
  15.     $i = 1;  
  16.     while ($i <= $loop_cnt) {  
  17.         $title = "jkjkjkjkjkjkjkjkjkjkjkjkjk";  
  18.         $tt    = time();  
  19.         $sql = "update mytable set addtime=".$tt.",title='".$title."' where id='".$i."'";  
  20.         $res = mysql_query($sql);  
  21.         if (FALSE == $res) {  
  22.             echo "update failed!\n";  
  23.         }  
  24.         $i++;  
  25.     }  
  26.     } else if ("delete" == $type) { //刪除  
  27.     $i = 1;  
  28.     while ($i <= $loop_cnt) {  
  29.         $sql = "delete from mytable where id='".$i."'";  
  30.         echo "delete sql: ".$sql."<br>";  
  31.         $res = mysql_query($sql);  
  32.         if (FALSE == $res) {  
  33.         echo "delete failed!\n";  
  34.         }  
  35.         $i++;  
  36.     }  
  37.       
  38.     } else if ("post" == $type) { //添加  
  39.     $i = 0;  
  40.     while ($i < $loop_cnt) {  
  41.         $title = "hahahahahahahahahahahahahahahahahaha";  
  42.         $tt    = time();  
  43.         $sql = "insert into mytable(addtime, title) values($tt, '".$title."')";  
  44.         //print "SQL: ".$sql."<br>";  
  45.         $res = mysql_query($sql);  
  46.         if (FALSE == $res) {  
  47.         echo "insert failed!\n";  
  48.         }  
  49.         $i++;  
  50.     }  
  51.     }  
  52.     mysql_close();  
  53.     $etime = getmicrotime();  
  54.     $runTime = round($etime - $btime, 4);  
  55.     echo "runTime: ".$runTime."\r\n<br>";  
  56. ?>  

單獨(dú)執(zhí)行php連接MySQL,單條連接添加1000條記錄需要:0.9s左右php操作memcache的程序:

  1. <?php  
  2.     include_once("gettime.php");  
  3.     $btime = getmicrotime();  
  4.     //   
  5.     $mem_host = "192.168.0.134";  
  6.     $mem_port = "11311";  
  7.     $timeout = 3600;  
  8.     $i = 0;  
  9.     $cnt = 1000;  
  10.     while ($i < $cnt) {  
  11.     $mem = new Memcache;  
  12.     $mem->connect($mem_host$mem_portor die("Could not connect!");  
  13.     $ret = $mem->set($i"11111111111", 0, $timeout);  
  14.     if (false == $ret) {  
  15.         file_put_contents("insert_failed.log""post failed!\n", FILE_APPEND);  
  16.     }  
  17.     $mem->close();  
  18.     $i++;  
  19.     }  
  20.  
  21.     //   
  22.     $etime = getmicrotime();  
  23.     $runTime = round($etime - $btime, 4);  
  24.     echo "runTime: ".$runTime."\r\n<br>";  
  25. ?>  

單條連接添加1000條記錄,需要0.8s左右,創(chuàng)建觸發(fā)器:

  1. DELIMITER $$  
  2.  
  3. DROP TRIGGER /*!50032 IF EXISTS */ `test`.`mytable_insert`$$  
  4.  
  5. CREATE 
  6.     /*!50017 DEFINER = 'root'@'localhost' */  
  7.     TRIGGER `mytable_insert` AFTER INSERT ON `mytable`   
  8.     FOR EACH ROW BEGIN   
  9.     SET @tt_resu = (SELECT http_put(CONCAT('http://192.168.0.134/mem_ss.php?type=post&id=', NEW.id, "&data=", NEW.addtime), 11));  
  10. END;  
  11. $$  

為觸發(fā)器寫個(gè)php更新memcache,代碼如下:

  1. <?php  
  2.     $id        = $_GET['id'];  
  3.     $type      = $_GET['type'];  
  4.     $json_data = $_GET['data'];  
  5.     var_dump($_GET);  
  6.     //  
  7.     $mem_host = "192.168.0.134";  
  8.     $mem_port = "11211";  
  9.     $timeout = 3600;  
  10.     $mem = new Memcache;  
  11.     $mem->connect($mem_host$mem_portor die("Could not connect!");  
  12.       
  13.     if ("get" == $type ) {  
  14.     $val = $mem->get($id);  
  15.     echo $val;  
  16.     //$arr = jsonDecode($val,'utf-8');  
  17.     //print_r($arr);  
  18.     } else if ("put" == $type) {  
  19.     $ret = $mem->replace($id$json_data, 0, $timeout);  
  20.     if (false == $ret) {  
  21.         file_put_contents("replace_failed.log""replace failed!\n", FILE_APPEND);  
  22.     }   
  23.     } else if ("delete" == $type) {   
  24.     $ret = $mem->delete($id);  
  25.     if (false == $ret) {  
  26.         file_put_contents("delete_failed.log""delete failed!\n", FILE_APPEND);  
  27.     }  
  28.     } else if ("post" == $type) {  
  29.     $ret = $mem->set($id$json_data, 0, $timeout);  
  30.     if (false == $ret) {  
  31.         file_put_contents("post_failed.log""post failed!\n", FILE_APPEND);  
  32.     }  
  33.     }  
  34.  
  35.     $mem->close();  
  36. ?>  

使用php觸發(fā)MySQL添加1000條記錄,同時(shí)觸發(fā)器觸動php更新memcache,使用時(shí)間9s左右,因?yàn)槊看味缄P(guān)閉鏈接memcache,看是不是關(guān)閉鏈接導(dǎo)致慢,又寫了一個(gè)程序:

  1. <?php  
  2.     include_once("gettime.php");  
  3.     $btime = getmicrotime();  
  4.     //連接  
  5.     $mem_host = "192.168.0.134";  
  6.     $mem_port = "11311";  
  7.     $timeout = 3600;  
  8.     $i = 0;  
  9.     $cnt = 1000;  
  10.     while ($i < $cnt) {  
  11.     $mem = new Memcache;  
  12.     $mem->connect($mem_host$mem_portor die("Could not connect!");  
  13.     $ret = $mem->set($i"11111111111", 0, 3600);  
  14.     if (false == $ret) {  
  15.         file_put_contents("insert_failed.log""post failed!\n", FILE_APPEND);  
  16.     }  
  17.     $mem->close();  
  18.     $i++;  
  19.     }  
  20.  
  21.     //關(guān)閉連接  
  22.     $etime = getmicrotime();  
  23.     $runTime = round($etime - $btime, 4);  
  24.     echo "runTime: ".$runTime."\r\n<br>";  
  25. ?>  

耗時(shí)0.9s左右,比一個(gè)連接慢不了多少。為了定位是觸發(fā)器慢還是http_put慢,創(chuàng)建一個(gè)臨時(shí)表tmp_mytable,表結(jié)構(gòu)如下:

  1. CREATE TABLE `mytable` (  
  2.  `id` int(10) NOT NULL AUTO_INCREMENT,  
  3.  `addtime` int(10) NOT NULL,  
  4.  `title` varchar(255) NOT NULL 
  5. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  
再次修改觸發(fā)器,如下:
  1. DELIMITER $$  
  2.  
  3. DROP TRIGGER /*!50032 IF EXISTS */ `test`.`mytable_insert`$$  
  4.  
  5. CREATE 
  6.     /*!50017 DEFINER = 'root'@'localhost' */  
  7.     TRIGGER `mytable_insert` AFTER INSERT ON `mytable`   
  8.     FOR EACH ROW BEGIN   
  9.      insert into tmp_mytable values(NEW.id,NEW.addtime,NEW.title);     
  10. END;  
  11. $$  

再次用php向MySQL中添加1000條記錄,消耗時(shí)間0.7s左右,證明效率消耗在http_put,也就是mysql-udf-http慢。不知道我的測試有錯(cuò)沒?還請正在使用mysql-udf-http的高手,或者對mysql-udf-http有研究的高手指教。

 

原文鏈接:http://www.cnblogs.com/sunss/archive/2011/05/09/2041283.html

【編輯推薦】

  1. MySQL中創(chuàng)建及優(yōu)化索引組織結(jié)構(gòu)的思路
  2. 微博 請問你是怎么優(yōu)化數(shù)據(jù)庫的?
  3. MySQL技巧:結(jié)合相關(guān)參數(shù) 做好Limit優(yōu)化
  4. MySQL數(shù)據(jù)庫的優(yōu)化(下)MySQL數(shù)據(jù)庫的高可用架構(gòu)方案
  5. MySQL數(shù)據(jù)庫的優(yōu)化(上)單機(jī)MySQL數(shù)據(jù)庫的優(yōu)化
責(zé)任編輯:艾婧 來源: sunss的博客
相關(guān)推薦

2011-06-20 09:52:56

MySQL

2011-07-27 17:22:10

mysql極限測試索引

2010-05-31 16:46:40

2009-06-25 16:09:37

2010-05-27 17:16:20

MySQL數(shù)據(jù)庫

2013-12-25 10:32:41

MySQL性能測試

2013-05-07 09:47:30

測試MySQLMySQL測試

2010-05-21 14:36:00

MySQL left

2021-12-01 10:18:08

MongoDBMySQL數(shù)據(jù)庫

2017-03-07 08:50:17

2010-10-08 16:26:49

mysql查找

2010-11-22 13:23:52

MySQL數(shù)據(jù)庫優(yōu)化

2010-11-25 13:05:26

MySQL列類型

2012-07-06 09:00:34

MySQL

2019-03-25 12:20:29

數(shù)據(jù)MySQL性能測試

2010-04-16 16:12:51

jdbc分頁

2010-11-25 11:07:28

MySQL慢查詢

2011-04-02 09:33:08

MySQL數(shù)據(jù)庫查詢效率

2011-04-02 09:33:13

MySQL數(shù)據(jù)庫查詢效率

2011-04-02 09:23:19

MySQL數(shù)據(jù)庫查詢效率
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號