Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4175666
  • 博文数量: 601
  • 博客积分: 15410
  • 博客等级: 上将
  • 技术积分: 6884
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-16 08:11
个人简介

独学而无友,则孤陋而寡闻!

文章分类

全部博文(601)

文章存档

2020年(1)

2018年(4)

2017年(7)

2016年(42)

2015年(25)

2014年(15)

2013年(36)

2012年(46)

2011年(117)

2010年(148)

2009年(82)

2008年(37)

2007年(41)

分类: Mysql/postgreSQL

2013-11-02 21:00:14

网上最近的的资料也有好几年了,我重新测试一下吧:

一、环境:
(一)各软件版本:FreeBSD9.2,ports安装的PostgreSql9.3.1 和 Mysql5.5.33,用php测试,php版本为5.3.37,直接用命令行测试。至强Intel(R) Xeon(R) CPU E3-1240 V2 @ 3.40GHz,4G内存,500G SATA硬盘。
        mysql用的huge.cnf,PostgreSql仅添加了shared_buffers = 800MB。

(二)数据表:test,两个数据库都建一个专门的数据表,数据库放到同一下硬盘,同一个目录下,分别建子目录。先测试不带索引的情况,然后再建索引的情况。三个字段:id:int(6),name:varchar(10),text:varchr(250)。

(三)代码:注释掉的几行,是为了测试各种情形,防止干扰,pgsql和mysql没有放在一起比较,分成两个文件。
(1)pgsql:

  1. <?php

  2. $time_start = microtime(true);

  3. $conn = pg_connect("dbname=test user=test password=test");

  4. if (!$conn) {

  5.     echo "An error occured.\n";
  6.     exit;
  7. }

  8. $sql = 'BEGIN';
  9. //pg_query($conn,$sql);

  10. for($i=0;$i<=2000;$i++){
  11.         $name_str = "zhang" . $i;
  12.         $text_str = str_pad("The string is ", $i%200 , "d");
  13.         //$sql = "INSERT INTO test( id,name,text) VALUES (" . $i .",'" . $name_str . "','" . $text_str ."')";
  14.         //$sql = "SELECT * from test WHERE text = '" . $text_str . "'";
  15.         $sql = "SELECT * from test WHERE id = " . $i ;
  16.         $result = pg_query($conn,$sql);

  17.         if (!$result) {
  18.                 echo "An error occured!" ;
  19.                 die("__");
  20.         }

  21. // $rows = pg_fetch_all($result);

  22. }

  23. $sql = 'COMMIT';
  24. //pg_query($conn,$sql);
  25. $time_end = microtime(true);
  26. $time = $time_end - $time_start;

  27. $sql = "TRUNCATE test";
  28. //pg_query($conn,$sql);

  29. echo "\ntime is $time \n";


  30. ?>

(二)mysql

  1. <?php

  2. $time_start = microtime(true);

  3. $conn = mysql_connect('localhost', 'test', 'test');
  4. mysql_select_db("test");

  5. if (!$conn) {

  6.     echo "An error occured.\n";
  7.     exit;
  8. }

  9. for($i=0;$i<=20000;$i++){
  10.         $name_str = "zhang" . $i;
  11.         $text_str = str_pad("The string is ", $i%200 , "d");
  12. // $sql = "INSERT INTO test( id,name,text) VALUES (" . $i .",'" . $name_str . "','" . $text_str ."')";
  13. // $sql = "SELECT * from test WHERE text = '" . $text_str . "'";
  14.         $sql = "SELECT * from test WHERE id = " . $i ;
  15.         $result = mysql_query($sql);

  16.         if (!$result) {
  17.                 echo "An error occured!" ;
  18.                 die("__");
  19.         }
  20. }

  21. $time_end = microtime(true);
  22. $time = $time_end - $time_start;

  23. $sql = "TRUNCATE test";
  24. //mysql_query($sql);

  25. echo "\ntime is $time \n";
  26. ?>

二、结果
(一)插入两万条记录:
pgsql第一列,为先写了begin,最后加上commit,相当于插入的时候忽略rollback功能,等效于myisam;
pgsql with begin pgsql without begin mysql with innodb mysql with myisam
1.380398035 12.74742222 28.3199389 1.045269966
1.036226988 12.58433795 22.8662529 0.854581118
0.989035845 12.25525784 24.40100813 0.85179615
0.979285955 12.29677892 22.71812797 1.10293889
0.983509064 12.57599187 24.45858788 0.857715845

插入时磁盘的IO情况:
项目
pgsql mysql with innodb
KB/t 32.23 32.22
tps 3237 3507
MB/s 102 110
%busy 74 80



(二)不带索引,检索text列,20000次:
查询大字符串




4.421151876 3.629865885 3.07119298
4.166283846 1.738970995 1.731379032
4.162822008 1.738083839 1.739454031
4.161303043 1.738959074 1.738510132
4.159893036 1.735637903 1.730456829
4.158488989 1.731363058 1.730481863


(三)检索带索引的id列:
索引id    2000次 20000次 20000次 20000次
0.415557146 1.524525881 1.396705866 1.199653864
0.127171993 1.247123957 0.376204967 0.374560833
0.125981808 1.249945879 0.37511301 0.368445873
0.125764847 1.246695995 0.373888969 0.368315935
0.125821114 1.250742912 0.373873949 0.366961002
0.127412081 1.254354954 0.374575138 0.371459961


三、总结:
1、简单的插入和查询,pgsql没有占到便宜,可能还有优化的空间。
2、在插入的时候,pgsql在带事务的情况下,性能还是比innodb强一些。在不带事务的时候,跟myisam也有一拼了。这两者可能都受到磁盘的影响。磁盘已经达到100MB/s,基本上sata硬盘的峰值了。
3、mysql查询的缓存好像做的比较好,pgsql这方面要差一些。

四、未尽事宜:
1、数据表还可以再大一些。
2、join和view可能更能考验数据表。
3、全文检索?




----续
插入2m个记录:
Postgresql:
98.395066976547

innodb 20分钟没完成,直接^+C了。

mysqlisam:
96.253983974457

查询,text字段 ,2000次,从2m记录中
Postgresql:
442.35978007317

Mysql with myisam:
1053.6569011211


查询,id字段
1.4101810455322
1.1692430973053


---Mysql
1.5967738628387
0.37165594100952






阅读(2605) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~