qqruser 发表于 2015-7-8 08:27:03

关于PHP与MongoDB的一些杂记

  测试环境:PHP5.3 + mongo-1.1.4-php5.3vc6ts的php_mongo.dll
  使用mongostat观察发现:
  1.mongostat本身也占用一个数据库连接

  2.PHP的mongo扩展默使用短连接,类似如此代码:new Mongo("mongodb://192.168.1.108/test"),这种短连接一般在超出变量作用域后会自己关闭

  3.PHP也可以使用长连接:



for($i=0;$i "x"));
      $arr=$conn->test->post->find();
      foreach ( $arr as $key => $val ) {
            echo "$key => ";
            var_dum($val);
            echo "";
      }
}  
  使用这种长连接,执行整个循环,从始至终都只有一个连接,切页面执行完毕以后,连接也不会被关闭。(这种长连接性能明显比短连接要好得多)
3.长连接的使用必须要主机名,端口,标识符,用户名以及密码一样才行,否则会重新创建一个长连接,英文原文如下:


For a persistent connection to be used, the hostname, port, persist string, andusername and password (if given) must match an existing persistent connection.Otherwise, a new connection will be created with this identifying information
例如如下代码就会创建100个长连接:


  
4.主机名,端口,标识符,用户名以及密码一样一样的长连接也可能会创建多个,但是这需要大并发量才会出现,假设http://localhost/index.php的代码如下:



for($i=0;$i "x"));
      $arr=$conn->test->post->find();
      foreach ( $arr as $key => $val ) {
            echo "$key => ";
            var_dum($val);
            echo "";
      }
}如上代码,假如我们写一个程序使用异步同时发送几十乃至几百个连接去请求http://localhost/index.php,使用mongostat观察会发现创建了多个长连接。  
  
  
页: [1]
查看完整版本: 关于PHP与MongoDB的一些杂记