LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2895|回复: 2

PostgreSQL 与 MySQL 性能大测试(第二节)

[复制链接]
发表于 2009-5-9 16:53:44 | 显示全部楼层 |阅读模式

  1. 本测试文档,采用GPL 协议,如转载请注明出处 : http://blog.bowenye.com/read.php?4  
  2. 如商业,请联系本人  QQ : 84437129
复制代码



各位同学,大家好,上一节介绍 MySQL与PostgreSQL 对比的前期内容和一堆费话资料,这节将正式开始进入第一个阶段测试比较
上一节所需的条件已完备,现在先公布PHP的测试代码

MySQL 测试代码

  1. <?PHP
  2. //MySQL 测试代码
  3. //OK,开始写测试代码,下面是PHP手册中标准的运用
  4. // MyISAM 的表为:uchome_docomment
  5. // InnoDB的表为: uchome_docomment_innodb ,
  6. //这儿只贴出MYISAM的PHP代码

  7. header("Content-type: text/html; charset=utf-8");
  8. //insert 用的 本来想随机来插入内容的,有点懒啦

  9. $in_sql = "INSERT INTO uchome_docomment (upid ,doid ,uid ,username ,dateline ,message ,ip ,grade)VALUES (
  10. '1', '2', '1', 'myname', '1234567890', 'messagemessagemessagemessagemessa
  11. gemessagemessagemessagemessagem
  12. essagemessagemessagemessagemessage', '127.0.0.1', '2'
  13. )";
  14. //insert 次数
  15. $i_i=0;
  16. //select 用, 全select 出来吧 :)  ,取一条,留作 delete 或 update用
  17. $se_sql ="select * from uchome_docomment LIMIT 1";
  18. //select 次数
  19. $i_s=0;
  20. //delete 用 ,
  21. //随机 del id
  22. $idd=1;
  23. $del_sql ="DELETE FROM uchome_docomment WHERE id = ".$idd." ";
  24. //delete 次数
  25. $i_d=0;
  26. //update 用
  27. //update 随机 id
  28. $idu=1;
  29. $up_sql = "UPDATE uchome_docomment SET doid = '4' WHERE id =".$idu."   ";
  30. //update 次数
  31. $i_u=0;

  32. //先连接数据
  33. $link = mysql_connect('localhost', 'bowen', 'bowen') or die('Could not connect: ' . mysql_error());
  34. echo 'Connected successfully';
  35. mysql_select_db('bowen',$link) or die('Could not select database'. mysql_error());

  36. //开始计时
  37. $start_time = microtime_float();
  38. // 在一万次FOR中,随机进行SQL操作
  39. for($i=0;$i<10;$i++) {
  40.   $a=rand(1,4);
  41.   switch ($a) {
  42.     case $a==1:
  43.       $i_s++;
  44.       //select
  45.         $result = mysql_query($se_sql) or die('Query failed: ' . mysql_error());
  46.         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  47.             rand(0,1)?$idd=$line['id']:$idu=$line['id'];
  48.         }
  49.         // 释放结果集
  50.         mysql_free_result($result);
  51.       break;
  52.     case $a==2:
  53.       $i_i++;
  54.       //insert
  55.       $result = mysql_query($in_sql) or die('Query insert failed: ' . pg_last_error());  
  56.       break;
  57.     case $a==3:
  58.       $i_d++;
  59.       //delete
  60.       $result = mysql_query($del_sql) or die('Query delete failed: ' . pg_last_error());  
  61.       break;
  62.     case $a==4:
  63.       $i_u++;
  64.       //update
  65.       $result = mysql_query($up_sql) or die('Query update failed: ' . pg_last_error());
  66.       break;
  67.     default:
  68.       echo "here is default";
  69.       break;
  70.   }
  71. }
  72.    

  73. //结束
  74. $end_time = microtime_float();

  75. // 关闭连接
  76. mysql_close($link);

  77. echo "<br />用了 : ".($end_time-$start_time)." 时间";
  78. echo "<br /> select 次数".$i_s;
  79. echo "<br /> insert 次数".$i_i;
  80. echo "<br /> delete 次数".$i_d;
  81. echo "<br /> update 次数".$i_u;
  82. // 释放结果集

  83. //标准的PHP手册 例子
  84. function microtime_float()
  85. {
  86.     list($usec, $sec) = explode(" ", microtime());
  87.     return ((float)$usec + (float)$sec);
  88. }
  89. ?>
复制代码


PostgreSQL 测试代码

  1. <?PHP
  2. //PostgreSQL 测试代码
  3. //OK,开始写测试代码,下面是PHP手册中标准的运用

  4. header("Content-type: text/html; charset=utf-8");
  5. //insert 用的 本来想随机来插入内容的,有点懒啦

  6. $in_sql = "INSERT INTO uchome_docomment (upid ,doid ,uid ,username ,dateline ,message ,ip ,grade)VALUES (
  7. '1', '2', '1', 'myname', '1234567890', 'messagemessagemessagemessage
  8. messagemessagemessagemessagem
  9. essagemessagemessagemessagemessagemessage', '127.0.0.1', '2'
  10. )";
  11. //insert 次数
  12. $i_i=0;
  13. //select 用, 全select 出来吧 :)  ,取一条,留作 delete 或 update用
  14. $se_sql ="select * from uchome_docomment LIMIT 1";
  15. //select 次数
  16. $i_s=0;
  17. //delete 用 ,
  18. //随机 del id
  19. $idd=1;
  20. $del_sql ="DELETE FROM uchome_docomment WHERE id = ".$idd." ";
  21. //delete 次数
  22. $i_d=0;
  23. //update 用
  24. //update 随机 id
  25. $idu=1;
  26. $up_sql = "UPDATE uchome_docomment SET doid = '4' WHERE id =".$idu."   ";
  27. //update 次数
  28. $i_u=0;

  29. //先连接数据
  30. $dbconn = pg_connect("host=localhost dbname=bowen user=bowen password=bowen") or die('Could not connect: ' . pg_last_error());

  31. //开始计时
  32. $start_time = microtime_float();
  33. // 在一万次FOR中,随机进行SQL操作
  34. for($i=0;$i<10;$i++) {
  35.   $a=rand(1,4);
  36.   switch ($a) {
  37.     case $a==1:
  38.       $i_s++;
  39.       //select
  40.         $result = pg_query($se_sql) or die('Query select failed: ' . pg_last_error());  
  41.         while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  42.             rand(0,1)?$idd=$line['id']:$idu=$line['id'];
  43.         }
  44.         pg_free_result($result);
  45.       break;
  46.     case $a==2:
  47.       $i_i++;
  48.       //insert
  49.       $result = pg_query($in_sql) or die('Query insert failed: ' . pg_last_error());  
  50.       break;
  51.     case $a==3:
  52.       $i_d++;
  53.       //delete
  54.       $result = pg_query($del_sql) or die('Query delete failed: ' . pg_last_error());  
  55.       break;
  56.     case $a==4:
  57.       $i_u++;
  58.       //update
  59.       $result = pg_query($up_sql) or die('Query update failed: ' . pg_last_error());
  60.       break;
  61.     default:
  62.       echo "here is default";
  63.       break;
  64.   }
  65. }
  66.    
  67. //结束
  68. $end_time = microtime_float();


  69. // 关闭连接
  70. pg_close($dbconn);


  71. echo "<br />用了 : ".($end_time-$start_time)." 时间";
  72. echo "<br /> select 次数".$i_s;
  73. echo "<br /> insert 次数".$i_i;
  74. echo "<br /> delete 次数".$i_d;
  75. echo "<br /> update 次数".$i_u;
  76. // 释放结果集

  77. //标准的PHP手册 例子
  78. function microtime_float()
  79. {
  80.     list($usec, $sec) = explode(" ", microtime());
  81.     return ((float)$usec + (float)$sec);
  82. }
  83. ?>
复制代码


测试即将开始了,现在先说明规则


  1. 测试规则
  2. 一.五次,相隔20秒左右访问一次PHP文件
  3. 二.打开10个页面,同时连接PHP文件
  4. 三.顺序MYISAM -> InnoDB -> PostgreSQL
复制代码


let's go


经过一段时间之后,成绩出来了,先看图
Y座标是时间
X座标是测试次数






哈哈,不要惊讶,本人可以证明,配置都是默认,本人绝不对配置进行过改动
但为什么出现的性能与网上大多数的观点不一至呢?看到这儿,可能有一大堆MySQL 的fans们
已经拿好鸡蛋等着我了,哈哈,再看看一下面一份测试结果的txt文件
看看时间,竟然一节课又过了,老话:欲知后事如何,请看第三节
发表于 2009-6-2 08:49:08 | 显示全部楼层
完全是两个级别的东西么... MySQL应该和SQLite比比
回复 支持 反对

使用道具 举报

发表于 2009-6-4 19:23:48 | 显示全部楼层
Post by MeaCulpa;1993285
完全是两个级别的东西么... MySQL应该和SQLite比比


请教PostgreSQL和MySQL分别是什么级别的?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表