`

mongoDB高效分页类

 
阅读更多

最近在研究mongodb,发现她的性能比mysql好很多,但具体快多少还没有测试过。

 
项目组最近在策划新的游戏,我们程序都开始准备相关的东西,首先是游戏的后台,由于用mongodb,所以整个框架都得重新写过,分页类也不例外。本来分页是用skip+limit的,但有一次我不知道手哪里痒了,弄了80+w数据进去,发现这种分页方法的效率骤然下降,从第一页翻到最后一页竟然要0.9s,而且只是80+w数据,效率实在太低了,因为考虑到操作日志,消费日志这些大表,百万级数据是很正常的事情,所以这样的分页是伤不起的,所以我决定改进一下分页的算法。
从网上找到资料,原来对于大数据量的分页,都不用skip+limit的,是通过改变查询规则来起到分页的效果。由于不用略过,所以无聊数据量有多大,时间复杂度的波动不大。
 
<?php
//mongoDB分页类
class Page {
var $count='';
var $size='';
var $total='';
var $last='';
var $link='';
var $url='';
var $set='';
var $page='';
var $turnto='';
var $key = '';
var $next = '';
var $prev = '';
var $lefttresult = '';
var $rightresult = '';
var $left = '';
var $right = '';
var $orderby = '';
var $lastd = '';
var $db = '';
 
//构造函数
public function __construct($last, $key, $orderby){
global $DB;
$this->db = $DB;
$this->count = $this->db->count($this->db->get_table());
$url = SITE_ROOT.strtolower(CLASS_NAME).'/'.METHOD_NAME;
$this->url = $this->url ? $this->url : $url;
$set = $set ? $set : 5;
$this->set = $set;
$size = $size ? $size : PAGE_SIZE;
$this->size = $size;
$this->last = $last;
$this->prev = $DB->get_prev($this->last);
$this->next = $DB->get_next($this->last);
$this->page = GET::UINT('page');
$this->page = $this->page ? $this->page : 1;
$this->total = @ceil($this->count / $this->size);
$this->key = $key;
$this->orderby = $orderby;
}
 
//输出分页链接
public function get_link(){
if($this->total != 1){
$this->get_first();
$this->get_prev();
$this->get_center();
$this->get_next();
$this->get_last();
$this->get_turnto();
}
if($this->link){
$this->link = $this->turnto.$this->link.'共'.number_format($this->total).'页 '.number_format($this->count).'条记录';
}
if($this->turnto){
$this->link .= '</form>';
}
return $this->link;
}
 
//获取左边显示的个数
public function get_left(){
return  $this->left = ($this->set - $this->page >= 0) ? ($this->page - 1) : $this->set;
}
 
//获取右边显示的个数
public function get_right(){
return $this->right = ($this->total - $this->page > $this->set) ? $this->set : ($this->total - $this->page);
}
 
//设置左边的结果集
public function set_left_result($left_result){
$this->leftresult = $left_result;
}
 
//设置右边的结果集
public function set_right_result($right_result){
$this->rightresult = $right_result;
}
 
//设置排序条件
public function set_orderby($orderby){
$this->orderby = $orderby;
}
 
//设置最后一页
public function set_last($last){
$this->lastd = $last;
 
//设置中间显示页码个数
public function set($set){
$this->set = $set;
}
 
//获取首页
private function get_first(){
if($this->page != 1){
if($this->total > 0){
$this->link.='<a href="'.$this->url.'" title="首页">首页</a>';
}
}
}
 
//获取上一页
private function get_prev(){
if($this->prev){
$this->link.='<a href="'.$this->url.'/page/'.($this->page - 1).'/id/'.$this->prev.'" title="上一页">上一页</a>';
}
}
 
//中间显示
private function get_center(){
$start = ($this->page - $this->set) <= 0 ? 1 : ($this->page - $this->set);  
$end = ($this->page + $this->set + 1 >= $this->total) ? $this->total + 1 : ($this->page + $this->set + 1);
 
$ii = $this->left;
$iii = 0;
//显示左边的
for($i = $start; $i < $end; $i++, $ii--, $iii++){
if($this->page == $i){
$this->link.='<a style="color:#06F">'.$i.'</a>';
}else{
$the_id = $ii * $this->size - 1;
if($the_id > 0){
$this->link.='<a href="'.$this->url.'/page/'.$i.'/id/'.$this->leftresult[$the_id][$this->key].'" title="第'.$i.'页">'.$i.'</a>';
}else{
$the_id = ($iii - $this->left) * $this->size;
$this->link.='<a href="'.$this->url.'/page/'.$i.'/id/'.$this->rightresult[$the_id][$this->key].'" title="第'.$i.'页">'.$i.'</a>';
}
}
}
}
 
//获取下一页
private function get_next(){
if($this->next){
$this->link.='<a href="'.$this->url.'/page/'.($this->page + 1).'/id/'.$this->next.'" title="下一页">下一页</a>';
}
}
 
//获取尾页
private function get_last(){
if($this->page != $this->total){
$this->link.='<a href="'.$this->url.'/page/'.$this->total.'/id/'.$this->lastd.'" title="尾页">尾页</a>';
}
}
 
//跳转到
private function get_turnto(){
$this->turnto = '<form action="" method="get" onsubmit="window.location=\''.$this->url.'/search/\'+this.p.value+\''.'\';return false;">转到第 <input type="text" name="p" style="width:25px;text-align:center"> 页';
}
 
//求反
public function nor_orderby(){
foreach($this->orderby as $key => $order){
if($order==-1){
$orderby[$key] = 1;
}else{
$orderby[$key] = -1;
}
}
return $orderby;
}
 
//设置key
public function set_key($key){
$this->key = $key;
}
 
//分页操作
public function show(){
$this->set_key($this->key);
$this->set_orderby($this->orderby);
$left = $this->get_left();
$right = $this->get_right();
$leftresult = $this->db->get_left($left, $this->last);
$rightresult = $this->db->get_right($right, $this->last);
$this->set_left_result($leftresult);
$this->set_right_result($rightresult);
$last = $this->db->get_last();
$this->set_last($last); 
return $this->get_link();
}
}
/*      调用例子
global $DB;
$lastid = GET::UINT('id');
$table = 'log';
$key = '_id';
$orderby = array($key => -1);
 
$DB->set_table($table);
$DB->set_key($key);
$DB->set_orderby($orderby);
 
$log = $DB->page_query($lastid);
 
$page = new Page($lastid, $key, $orderby);
$pager = $page->show();
*/
?>
附上对应的mongodb类
<?php 
 
//MongoDB操作类
class DB 
{
 
private $CI;
private $config_file = 'MongoDB';
 
private $connection;
private $db;
private $connection_string;
 
private $collection = '';
private $host;
private $port;
private $user;
private $pass;
private $dbname;
private $key;
private $persist;
private $persist_key;
 
private $selects = array();
private $wheres = array();
private $sorts = array();
private $page_sorts = array();
 
private $limit = 999999;
private $offset = 0;
 
 
/**
 *--------------------------------------------------------------------------------
 *CONSTRUCTOR
 *--------------------------------------------------------------------------------
 *
 *Automatically check if the Mongo PECL extension has been installed/enabled.
 *Generate the connection string and establish a connection to the MongoDB.
 */
 
public function __construct($MONGODB_CONFIG)
{
if(!class_exists('Mongo'))
{
show_error("The MongoDB PECL extension has not been installed or enabled", 500);
}
$this->connection_string($MONGODB_CONFIG);
$this->connect();
}
 
 
 
/**
 *--------------------------------------------------------------------------------
 *Switch_db
 *--------------------------------------------------------------------------------
 *
 *Switch from default database to a different db
 */
 
public function switch_db($database = '')
{
if(empty($database))
{
show_error("To switch MongoDB databases, a new database name must be specified", 500);
}
$this->dbname = $database;
try
{
$this->db = $this->connection->{$this->dbname};
return(TRUE);
}
catch(Exception $e)
{
show_error("Unable to switch Mongo Databases: {$e->getMessage()}", 500);
}
}
 
/**
 *--------------------------------------------------------------------------------
 *SELECT FIELDS
 *--------------------------------------------------------------------------------
 *
 *Determine which fields to include OR which to exclude during the query process.
 *Currently, including and excluding at the same time is not available, so the 
 *$includes array will take precedence over the $excludes array.  If you want to 
 *only choose fields to exclude, leave $includes an empty array().
 *
 *@usage: $this->mongo_db->select(array('foo', 'bar'))->get('foobar');
 */
 
public function select($includes = array(), $excludes = array())
{
 if(!is_array($includes))
 {
 $includes = array();
 }
 
 if(!is_array($excludes))
 {
 $excludes = array();
 }
 
 if(!empty($includes))
 {
 foreach($includes as $col)
 {
 $this->selects[$col] = 1;
 }
 }
 else
 {
 foreach($excludes as $col)
 {
 $this->selects[$col] = 0;
 }
 }
 return($this);
}
 
/**
 *--------------------------------------------------------------------------------
 *WHERE PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents based on these search parameters.  The $wheres array should 
 *be an associative array with the field as the key and the value as the search
 *criteria.
 *
 *@usage = $this->mongo_db->where(array('foo' => 'bar'))->get('foobar');
 */
 
 public function where($wheres = array())
 {
 foreach($wheres as $wh => $val)
 {
 $this->wheres[$wh] = $val;
 }
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE_IN PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is in a given $in array().
 *
 *@usage = $this->mongo_db->where_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');
 */
 
 public function where_in($field = "", $in = array())
 {
 $this->where_init($field);
 $this->wheres[$field]['$in'] = $in;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE_NOT_IN PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is not in a given $in array().
 *
 *@usage = $this->mongo_db->where_not_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');
 */
 
 public function where_not_in($field = "", $in = array())
 {
 $this->where_init($field);
 $this->wheres[$field]['$nin'] = $in;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE GREATER THAN PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is greater than $x
 *
 *@usage = $this->mongo_db->where_gt('foo', 20);
 */
 
 public function where_gt($field = "", $x)
 {
 $this->where_init($field);
 $this->wheres[$field]['$gt'] = $x;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE GREATER THAN OR EQUAL TO PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is greater than or equal to $x
 *
 *@usage = $this->mongo_db->where_gte('foo', 20);
 */
 
 public function where_gte($field = "", $x)
 {
 $this->where_init($field);
 $this->wheres[$field]['$gte'] = $x;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE LESS THAN PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is less than $x
 *
 *@usage = $this->mongo_db->where_lt('foo', 20);
 */
 
 public function where_lt($field = "", $x)
 {
 $this->where_init($field);
 $this->wheres[$field]['$lt'] = $x;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE LESS THAN OR EQUAL TO PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is less than or equal to $x
 *
 *@usage = $this->mongo_db->where_lte('foo', 20);
 */
 
 public function where_lte($field = "", $x)
 {
 $this->where_init($field);
 $this->wheres[$field]['$lte'] = $x;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE BETWEEN PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is between $x and $y
 *
 *@usage = $this->mongo_db->where_between('foo', 20, 30);
 */
 
 public function where_between($field = "", $x, $y)
 {
 $this->where_init($field);
 $this->wheres[$field]['$gte'] = $x;
 $this->wheres[$field]['$lte'] = $y;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE BETWEEN AND NOT EQUAL TO PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is between but not equal to $x and $y
 *
 *@usage = $this->mongo_db->where_between_ne('foo', 20, 30);
 */
 
 public function where_between_ne($field = "", $x, $y)
 {
 $this->where_init($field);
 $this->wheres[$field]['$gt'] = $x;
 $this->wheres[$field]['$lt'] = $y;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE NOT EQUAL TO PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is not equal to $x
 *
 *@usage = $this->mongo_db->where_between('foo', 20, 30);
 */
 
 public function where_ne($field = "", $x)
 {
 $this->where_init($field);
 $this->wheres[$field]['$ne'] = $x;
 return($this);
 }
 
 /**
 *--------------------------------------------------------------------------------
 *WHERE OR
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the value of a $field is in one or more values
 *
 *@usage = $this->mongo_db->where_or('foo', array( 'foo', 'bar', 'blegh' );
 */
 
 public function where_or($field = "", $values)
 {
 $this->where_init($field);
 $this->wheres[$field]['$or'] = $values;
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *WHERE AND
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the elements match the specified values
 *
 *@usage = $this->mongo_db->where_and( array ( 'foo' => 1, 'b' => 'someexample' );
 */
 
 public function where_and( $elements_values = array() ) {
 foreach ( $elements_values as $element => $val ) {
 $this->wheres[$element] = $val;
 }
 return($this);
 }
 
 /**
 *--------------------------------------------------------------------------------
 *WHERE MOD
 *--------------------------------------------------------------------------------
 *
 *Get the documents where $field % $mod = $result
 *
 *@usage = $this->mongo_db->where_mod( 'foo', 10, 1 );
 */
 
 public function where_mod( $field, $num, $result ) {
 $this->where_init($field);
 $this->wheres[$field]['$mod'] = array ( $num, $result );
 return($this);
 }
 
/**
*--------------------------------------------------------------------------------
*Where size
*--------------------------------------------------------------------------------
*
*Get the documents where the size of a field is in a given $size int
*
*@usage : $this->mongo_db->where_size('foo', 1)->get('foobar');
*/
 
public function where_size($field = "", $size = "")
{
$this->_where_init($field);
$this->wheres[$field]['$size'] = $size;
return ($this);
}
 
/**
 *--------------------------------------------------------------------------------
 *LIKE PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Get the documents where the (string) value of a $field is like a value. The defaults
 *allow for a case-insensitive search.
 *
 *@param $flags
 *Allows for the typical regular expression flags:
 *i = case insensitive
 *m = multiline
 *x = can contain comments
 *l = locale
 *s = dotall, "." matches everything, including newlines
 *u = match unicode
 *
 *@param $enable_start_wildcard
 *If set to anything other than TRUE, a starting line character "^" will be prepended
 *to the search value, representing only searching for a value at the start of 
 *a new line.
 *
 *@param $enable_end_wildcard
 *If set to anything other than TRUE, an ending line character "$" will be appended
 *to the search value, representing only searching for a value at the end of 
 *a line.
 *
 *@usage = $this->mongo_db->like('foo', 'bar', 'im', FALSE, TRUE);
 */
 
 public function like($field = "", $value = "", $flags = "i", $enable_start_wildcard = TRUE, $enable_end_wildcard = TRUE)
 {
 $field = (string) trim($field);
 $this->where_init($field);
 $value = (string) trim($value);
 $value = quotemeta($value);
 
 if($enable_start_wildcard !== TRUE)
 {
 $value = "^" . $value;
 }
 
 if($enable_end_wildcard !== TRUE)
 {
 $value .= "$";
 }
 
 $regex = "/$value/$flags";
 $this->wheres[$field] = new MongoRegex($regex);
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *ORDER BY PARAMETERS
 *--------------------------------------------------------------------------------
 *
 *Sort the documents based on the parameters passed. To set values to descending order,
 *you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be
 *set to 1 (ASC).
 *
 *@usage = $this->mongo_db->where_between('foo', 20, 30);
 */
 
 public function order_by($fields = array())
 {
 foreach($fields as $col => $val)
 {
 if($val == -1 || $val === FALSE || strtolower($val) == 'desc')
 {
 $this->sorts[$col] = -1; 
 }
 else
 {
 $this->sorts[$col] = 1;
 }
 }
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *LIMIT DOCUMENTS
 *--------------------------------------------------------------------------------
 *
 *Limit the result set to $x number of documents
 *
 *@usage = $this->mongo_db->limit($x);
 */
 
 public function limit($x = 99999) {
 if($x !== NULL && is_numeric($x) && $x >= 1)
 {
 $this->limit = (int) $x;
 }
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *OFFSET DOCUMENTS
 *--------------------------------------------------------------------------------
 *
 *Offset the result set to skip $x number of documents
 *
 *@usage = $this->mongo_db->offset($x);
 */
 
 public function offset($x = 0)
 {
 if($x !== NULL && is_numeric($x) && $x >= 1)
 {
 $this->offset = (int) $x;
 }
 return($this);
 }
 
/**
 *--------------------------------------------------------------------------------
 *GET_WHERE
 *--------------------------------------------------------------------------------
 *
 *Get the documents based upon the passed parameters
 *
 *@usage = $this->mongo_db->get_where('foo', array('bar' => 'something'));
 */
 
 public function get_where($collection = "", $where = array(), $limit = 99999)
 {
 return($this->where($where)->limit($limit)->get($collection));
 }
 
/**
 *--------------------------------------------------------------------------------
 *GET
 *--------------------------------------------------------------------------------
 *
 *Get the documents based upon the passed parameters
 *
 *@usage = $this->mongo_db->get('foo', array('bar' => 'something'));
 */
 
 public function get($collection = "")
 {
 if(empty($collection))
 {
 show_error("In order to retreive documents from MongoDB, a collection name must be passed", 500);
 }
 $results = array();
 $documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this->limit)->skip((int) $this->offset)->sort($this->sorts);
 
 $returns = array();
 
 foreach($documents as $doc):
 $returns[] = $doc;
 endforeach;
$this->clear();
 return($returns);
 
 }
 
/**
 *--------------------------------------------------------------------------------
 *COUNT
 *--------------------------------------------------------------------------------
 *
 *Count the documents based upon the passed parameters
 *
 *@usage = $this->mongo_db->get('foo');
 */
 
 public function count($collection = "")
 {
 if(empty($collection))
 {
 show_error("In order to retreive a count of documents from MongoDB, a collection name must be passed", 500);
 }
 $count = $this->db->{$collection}->find($this->wheres)->limit((int) $this->limit)->skip((int) $this->offset)->count();
 $this->clear();
 return($count);
 }
 
/**
 * 自增ID实现
 * return insert_id
 */
private function insert_inc($table)
$update = array('$inc'=>array('id'=>1));
$query = array('table'=>$table);
$command = array(
'findandmodify'=>'_increase', 
'update'=>$update,
'query'=>$query, 
'new'=>true, 
'upsert'=>true
);
$id = $this->db->command($command);
return $id['value']['id'];
}
 
/**
 *--------------------------------------------------------------------------------
 *INSERT
 *--------------------------------------------------------------------------------
 *
 *Insert a new document into the passed collection
 *
 *@usage = $this->mongo_db->insert('foo', $data = array());
 */
 
 public function insert($collection = "", $data = array()) {
 if(empty($collection))
 {
 show_error("No Mongo collection selected to insert into", 500);
 }
 
 if(count($data) == 0 || !is_array($data))
 {
 show_error("Nothing to insert into Mongo collection or insert is not an array", 500);
 }
 
 try
 {
$inc = $this->insert_inc($collection);
$data['_id'] = $inc;
 $result = $this->db->{$collection}->insert($data, array('fsync' => TRUE));
 if($result['ok'] || $result){
return true;
}
else{
return false;
}
 }
 catch(MongoCursorException $e)
 {
 show_error("Insert of data into MongoDB failed: {$e->getMessage()}", 500);
 }
 
 }
 
/**
 *--------------------------------------------------------------------------------
 *UPDATE
 *--------------------------------------------------------------------------------
 *
 *Update a document into the passed collection
 *
 *@usage = $this->mongo_db->update('foo', $data = array());
 */
 
 public function update($collection = "", $data = array(), $flage = false)
 {
 if(empty($collection))
 {
 show_error("No Mongo collection selected to update", 500);
}
 if(count($data) == 0 || !is_array($data))
 {
 show_error("Nothing to update in Mongo collection or update is not an array", 500);
}
unset($data['_id']);
if($flage){
$arr = $this->wheres;
unset($arr['_id']);
if(is_array($arr)){
foreach($arr as $key => $w){
unset($data[$key]);
}
}
}
try
 {
 $res = $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => TRUE, 'multiple' => FALSE));
$this->clear();
return $res;
 }  
 catch(MongoCursorException $e)
 {
 show_error("Update of data into MongoDB failed: {$e->getMessage()}", 500);
 }
 
 }
 
/**
 *--------------------------------------------------------------------------------
 *UPDATE_ALL
 *--------------------------------------------------------------------------------
 *
 *Insert a new document into the passed collection
 *
 *@usage = $this->mongo_db->update_all('foo', $data = array());
 */
 
 public function update_all($collection = "", $data = array()) {
 if(empty($collection))
 {
 show_error("No Mongo collection selected to update", 500);
 }
 
 if(count($data) == 0 || !is_array($data))
 {
 show_error("Nothing to update in Mongo collection or update is not an array", 500);
 }
 
 try
 {
 $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => TRUE, 'multiple' => TRUE));
$this->clear();
 return(TRUE);
 
 catch(MongoCursorException $e)
 {
 show_error("Update of data into MongoDB failed: {$e->getMessage()}", 500);
 }
 
 }
 
 /**
 *--------------------------------------------------------------------------------
 *DELETE
 *--------------------------------------------------------------------------------
 *
 *delete document from the passed collection based upon certain criteria
 *
 *@usage = $this->mongo_db->delete('foo', $data = array());
 */
 
 public function delete($collection, $where)
 {
 if(empty($collection))
 {
 show_error("No Mongo collection selected to delete from", 500);
 }
if(!$where){
show_error("No data input to delete", 500);
}
 try
 {
$this->wheres = $where;
 $this->db->{$collection}->remove($this->wheres);
$this->clear();
 return(TRUE);
 }
 catch(MongoCursorException $e)
 {
 show_error("Delete of data into MongoDB failed: {$e->getMessage()}", 500);
 }
 
 }
 
/**
 *--------------------------------------------------------------------------------
 *DELETE_ALL
 *--------------------------------------------------------------------------------
 *
 *Delete all documents from the passed collection based upon certain criteria
 *
 *@usage = $this->mongo_db->delete_all('foo', $data = array());
 */
 
  public function delete_all($collection = "")
  {
     if(empty($collection))
     {
     show_error("No Mongo collection selected to delete from", 500);
  }
 
 try
 {
 $this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => FALSE));
$this->clear();
 return(TRUE);
 }
 catch(MongoCursorException $e)
 {
 show_error("Delete of data into MongoDB failed: {$e->getMessage()}", 500);
 }
 
  }
 
/**
 *--------------------------------------------------------------------------------
 *ADD_INDEX
 *--------------------------------------------------------------------------------
 *
 *Ensure an index of the keys in a collection with optional parameters. To set values to descending order,
 *you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be
 *set to 1 (ASC).
 *
 *@usage = $this->mongo_db->add_index($collection, array('first_name' => 'ASC', 'last_name' => -1), array('unique' => TRUE));
 */
 
public function add_index($collection = "", $keys = array(), $options = array())
{
if(empty($collection))
{
 show_error("No Mongo collection specified to add index to", 500);
 }
 
 if(empty($keys) || !is_array($keys))
 {
 show_error("Index could not be created to MongoDB Collection because no keys were specified", 500);
    }
  
 foreach($keys as $col => $val)
 {
 if($val == -1 || $val === FALSE || strtolower($val) == 'desc')
 {
 $keys[$col] = -1; 
 }
 else
 {
 $keys[$col] = 1;
 }
 }
 
 if($this->db->{$collection}->ensureIndex($keys, $options) == TRUE)
 {
 $this->clear();
 return($this);
 }
 else
 {
 show_error("An error occured when trying to add an index to MongoDB Collection", 500);
}
}
 
 
 
/**
 *--------------------------------------------------------------------------------
 *REMOVE_INDEX
 *--------------------------------------------------------------------------------
 *
 *Remove an index of the keys in a collection. To set values to descending order,
 *you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be
 *set to 1 (ASC).
 *
 *@usage = $this->mongo_db->remove_index($collection, array('first_name' => 'ASC', 'last_name' => -1));
 */
 
public function remove_index($collection = "", $keys = array())
{
if(empty($collection))
{
 show_error("No Mongo collection specified to remove index from", 500);
 }
 
 if(empty($keys) || !is_array($keys))
 {
 show_error("Index could not be removed from MongoDB Collection because no keys were specified", 500);
 }
 
 if($this->db->{$collection}->deleteIndex($keys, $options) == TRUE)
 {
 $this->clear();
 return($this);
 }
 else
 {
 show_error("An error occured when trying to remove an index from MongoDB Collection", 500);
}
}
 
/**
 *--------------------------------------------------------------------------------
 *REMOVE_ALL_INDEXES
 *--------------------------------------------------------------------------------
 *
 *Remove all indexes from a collection.
 *
 *@usage = $this->mongo_db->remove_all_index($collection);
 */
 
public function remove_all_indexes($collection = "") {
if(empty($collection))
{
 show_error("No Mongo collection specified to remove all indexes from", 500);
 }
 
 $this->db->{$collection}->deleteIndexes();
 $this->clear();
 return($this);
}
 
/**
 *--------------------------------------------------------------------------------
 *LIST_INDEXES
 *--------------------------------------------------------------------------------
 *
 *Lists all indexes in a collection.
 *
 *@usage = $this->mongo_db->list_indexes($collection);
 */
public function list_indexes($collection = "") {
if(empty($collection))
{
 show_error("No Mongo collection specified to remove all indexes from", 500);
 }
 return($this->db->{$collection}->getIndexInfo());
}
 
/**
 *--------------------------------------------------------------------------------
 *DROP COLLECTION
 *--------------------------------------------------------------------------------
 *
 *Removes the specified collection from the database.  Be careful because this
 *  can have some very large issues in production!
 */
 
 public function drop_collection($collection = "")
 {
   if(empty($collection))
 {
     show_error("No Mongo collection specified to drop from database", 500);
   }
   $this->db->{$collection}->drop();
   return TRUE;
 }
 
 
/**
 *--------------------------------------------------------------------------------
 *CONNECT TO MONGODB
 *--------------------------------------------------------------------------------
 *
 *Establish a connection to MongoDB using the connection string generated in
 *the connection_string() method.  If 'mongo_persist_key' was set to true in the
 *config file, establish a persistent connection.  We allow for only the 'persist'
 *option to be set because we want to establish a connection immediately.
 */
 
private function connect() {
$options = array();
if($this->persist === TRUE)
{
$options['persist'] = isset($this->persist_key) && !empty($this->persist_key) ? $this->persist_key : 'ci_mongo_persist';
}
 
try
{
$this->connection = new Mongo($this->connection_string, $options);
$this->db = $this->connection->{$this->dbname};
return($this);
catch(MongoConnectionException $e)
{
show_error("Unable to connect to MongoDB: {$e->getMessage()}", 500);
}
}
 
/**
 *--------------------------------------------------------------------------------
 *BUILD CONNECTION STRING
 *--------------------------------------------------------------------------------
 *
 *Build the connection string from the config file.
 */
 
private function connection_string($MONGODB_CONFIG) 
{
 
$this->host = trim($MONGODB_CONFIG['HOST']);
$this->port = trim($MONGODB_CONFIG['PORT']);
$this->user = trim($MONGODB_CONFIG['USER']);
$this->pass = trim($MONGODB_CONFIG['PWD']);
$this->dbname = trim($MONGODB_CONFIG['DATABASE']);
$this->persist = trim($MONGODB_CONFIG['PERSIST']);
$this->persist_key = trim($MONGODB_CONFIG['PERSIST_KEY']);
 
$connection_string = "mongodb://";
 
if(empty($this->host))
{
show_error("The Host must be set to connect to MongoDB", 500);
}
 
if(empty($this->dbname))
{
show_error("The Database must be set to connect to MongoDB", 500);
}
 
if(!empty($this->user) && !empty($this->pass))
{
$connection_string .= "{$this->user}:{$this->pass}@";
}
 
if(isset($this->port) && !empty($this->port))
{
$connection_string .= "{$this->host}:{$this->port}/{$this->dbname}";
}
else
{
$connection_string .= "{$this->host}";
}
 
$this->connection_string = trim($connection_string);
}
 
/**
 *--------------------------------------------------------------------------------
 *CLEAR
 *--------------------------------------------------------------------------------
 *
 *Resets the class variables to default settings
 */
 
private function clear()
{
$this->selects = array();
$this->wheres = array();
$this->limit = NULL;
$this->offset = NULL;
$this->sorts = array();
}
 
/**
 *--------------------------------------------------------------------------------
 *WHERE INITIALIZER
 *--------------------------------------------------------------------------------
 *
 *Prepares parameters for insertion in $wheres array().
 */
 
private function where_init($param) 
{
if(!isset($this->wheres[$param]))
{
$this->wheres[$param] = array();
  }
}
 
/**
 *--------------------------------------------------------------------------------
 *设置表
 *--------------------------------------------------------------------------------
 *  参数:
 *  $table 表名
 */
public function set_table($table){
$this->collection = $table;
}
 
/**
 *--------------------------------------------------------------------------------
 *获取表名
 *--------------------------------------------------------------------------------
 */
public function get_table(){
return $this->collection;
}
 
/**
 *--------------------------------------------------------------------------------
 *设置表排序
 *--------------------------------------------------------------------------------
 *  参数:
 *  $orderby 排序
 */
public function set_orderby($orderby){
$this->page_sorts = $orderby;
}
 
 
/**
 *--------------------------------------------------------------------------------
 *获取左边结果集
 *--------------------------------------------------------------------------------
 *  参数:
 *  $left 左边显示的个数
 *  $last 定位当前页的值
 *  $size 页面大小
 */
public function get_left($left, $last, $size = PAGE_SIZE){
if($last){
$order = $this->nor_orderby();
if($this->page_sorts[$this->key] == -1){
$this->where_gt($this->key, $last);
} else {
 $this->where_lt($this->key, $last);
}
return $this->limit($left * $size)->order_by($order)->get($this->collection); 
}
}
 
 
/**
 *--------------------------------------------------------------------------------
 *获取右边结果集
 *--------------------------------------------------------------------------------
 *  参数:
 *  $right 右边显示的个数
 *  $last 定位当前页的值
 *  $size 页面大小
 */
public function get_right($right, $last, $size = PAGE_SIZE){
if($last){
if($this->page_sorts[$this->key] == -1){
$this->where_lte($this->key, $last);
} else {
$this->where_gte($this->key, $last);
}
}
return $this->limit($right * $size + 1)->order_by($this->page_sorts)->get($this->collection);
}
 
/**
 *--------------------------------------------------------------------------------
 *设置key
 *--------------------------------------------------------------------------------
 *  参数:
 *  $key 设置索引主键
 */
public function set_key($key){
$this->key = $key;
}
 
/**
 *--------------------------------------------------------------------------------
 *求反
 *-------------------------------------------------------------------------------- 
 */
private function nor_orderby(){
foreach($this->page_sorts as $key => $order){
if($order == -1){
$orderby[$key] = 1;
}else{
$orderby[$key] = -1;
}
}
return $orderby;
}
 
/**
 *--------------------------------------------------------------------------------
 *获取上一页的值
 *-------------------------------------------------------------------------------- 
 *  参数:
 *  $last 定位当前页的值
 *  $size 页面大小
 */
public function get_prev($last, $size = PAGE_SIZE){
if($last){
if($this->page_sorts[$this->key] == 1){
$this->where_lt($this->key,$last)->order_by(array($this->key => -1));
} else {
$this->where_gt($this->key,$last)->order_by(array($this->key => 1));
}
$result = $this->limit($size)->get($this->collection);
}
return $result[$size - 1][$this->key];
}
 
 /**
 *--------------------------------------------------------------------------------
 *获取下一页的值
 *-------------------------------------------------------------------------------- 
 *  参数:
 *  $last 定位当前页的值
 *  $size 页面大小
 */
public function get_next($last, $size = PAGE_SIZE){
if($last){
if($this->page_sorts[$this->key] == 1){
$this->where_gte($this->key,$last);
} else {
$this->where_lte($this->key,$last);
}
}
$result = $this->limit($size+1)->order_by($this->page_sorts)->get($this->collection);
return $result[$size][$this->key];
}
 
/**
 *--------------------------------------------------------------------------------
 *获取最后一页的值
 *-------------------------------------------------------------------------------- 
 *  参数:
 *  $size 页面大小
 */
public function get_last($size = PAGE_SIZE){
$res = $this->count($this->collection) % $size;
$order = $this->nor_orderby();
if($res > 0){
$result = $this->limit($res)->order_by($order)->get($this->collection);
return $result[$res - 1][$this->key];
}else{
$result = $this->limit($size)->order_by($order)->get($this->collection);
return $result[$size - 1][$this->key];
}
}
 
/**
 *--------------------------------------------------------------------------------
 *分页查询
 *-------------------------------------------------------------------------------- 
 *  参数:
 *  $last 定位当前页的值
 *  $size 页面大小
 */
public function page_query($last, $size = PAGE_SIZE){
if($last){
if($this->page_sorts[$this->key]==1){
$this->where_gte($this->key,$last);
} else {
$this->where_lte($this->key,$last);
}
}
return $this->limit($size)->order_by($this->page_sorts)->get($this->collection);
 
/**
 * 批量执行代码_插入
 * @param String $collection
 * @param 二维数组 $code 
 */
public function execute_insert($collection,$code){
//将二维数组分成js格式
$strcode='';
foreach($code as $k=>$v){
foreach($v as $kk=>$vv){
$strcode.='db.getCollection("'.$collection.'").insert({ "'.$kk.'":"'.$vv.'" });';
}
}
//retrun array([ok]=>1);
return $this->db->execute($code);
}
 
 
}
?>
 
 
 
 
经过优化之后,89w数据直接翻到最后一页只用了0.002s,效率明显提高,不过和rockmongo的效率还有很大差距,rockmongo的分页思路和我的有点不一样。
分享到:
评论

相关推荐

    高效mongodb的php分页类(不使用skip).zip

    介绍了高效mongodb的php分页类,并且没有使用mongodb的skip来实现分页,需要的朋友可以参考下,mongodb分页skip limit分页要先查出所有结果再去跳过,这样如果查询页面越往后效率越低。 如果能够通过查询条件查出...

    java+mongodb+分页

    在Java开发中,MongoDB是一种常用的NoSQL数据库,它以其灵活性、高性能和易于扩展性而受到欢迎。本篇文章将深入探讨如何使用Java与...在实际开发中,我们需要根据具体需求选择最合适的分页策略,确保系统的高效运行。

    高效mongodb的php分页类(不使用skip)

    下面是一个实现这种高效分页的PHP类的简要示例: 1. `mongodb.class.php`: 这个类将用于处理MongoDB的连接和基本操作。它包含了数据库连接、选择数据库、执行查询等方法。 ```php class DB { // ... // 连接...

    MongoDB分页插件

    MongoDB分页插件是专为MongoDB数据库设计的一个实用工具,主要目的是为了方便开发者在进行数据查询时实现高效、灵活的分页功能。在Java开发中,数据库操作是不可或缺的一部分,而面对大量数据时,分页显示尤为重要,...

    java jquery mongodb/mysql 分页

    在提供的文件名中,Result.java和PagingVO.java可能是Java后端用于封装分页结果的类。`Result.java`可能包含查询结果和相关元数据(如总条数、当前页等)。`PagingVO.java`(View Object)可能是用于传输分页信息的...

    mongodb的小例子,增删改查,分页

    MongoDB是一款流行且功能强大的NoSQL数据库系统,以其灵活性、高性能和易扩展性而闻名。在"mongodb的小例子,增删改查,分页...通过实践这些示例,你将能够快速上手MongoDB,并为构建高效的数据驱动应用打下坚实基础。

    MongoDB中的分页–如何真正避免性能下降?

    本文将深入探讨在MongoDB中进行高效分页的方法,以避免性能问题。 在传统的SQL环境中,我们通常使用`OFFSET`和`LIMIT`来实现分页,但在MongoDB中,这种方法可能并不理想。MongoDB没有直接的`OFFSET`概念,而是依赖...

    Springmvc应用Mongodb分页实现

    总结,实现Spring MVC与MongoDB的分页功能涉及的关键点包括理解分页原理、利用MongoDB的`skip()`和`limit()`方法,以及创建一个封装分页信息的类。通过这样的设计,我们可以灵活地在Web应用中实现高效的数据分页,...

    MongoDB查询性能验证及优化

    8. **更新操作**:批量更新往往比单个更新更高效。`bulkWrite()` API可用于批量操作。 9. **查询优化器**:MongoDB会自动选择最佳执行计划,但有时可能选择不当。可以通过`hint()`强制指定索引,或使用`...

    mongoDB分页的两种方法(图例)

    MongoDB是一种流行的文档型数据库,它支持丰富的查询和操作,包括分页查询。...总之,MongoDB的分页查询是灵活且高效的,可以根据具体应用场景选择适合的方法。在设计分页系统时,一定要考虑性能、可扩展性和用户体验。

    spring mongodb 中文文档

    通过学习这篇文档,开发者可以熟练掌握Spring Data MongoDB 的各种特性,从而更高效地在Java 应用中利用MongoDB 的强大功能。无论是简单的数据操作还是复杂的查询,Spring Data MongoDB 都能提供简洁、强大的解决...

    springboot+mongodb

    8. **分页和排序**:Spring Data MongoDB 支持分页查询和结果排序,可以通过在 Repository 方法上添加 `Pageable` 参数来实现。 在 `spring-boot-mongodb` 示例中,开发者可能会展示如何设置这些配置,创建模型类,...

    Spring Data MongoDB中文文档

    ### Spring Data MongoDB中文文档知识点概览 ...总之,**Spring Data MongoDB** 是一个强大的工具,它不仅简化了与 MongoDB 数据库的交互,还提供了一系列实用的功能,使得开发者能够更加高效地开发应用。

    mongoDB-查询语法

    MongoDB 是一个基于NoSQL的数据库,具有高效、灵活、易扩展等特点。在 MongoDB 中,查询语法是非常重要的一部分,本文将对 MongoDB 的查询语法进行详细的介绍。 基本查询语法 在 MongoDB 中,基本查询语法为 `db....

    Spring集成Mongodb配置含jar包

    6. **分页和排序**: Spring Data MongoDB支持分页查询,可以通过`Pageable`接口实现。同时,我们还可以在查询时添加排序规则,利用`Sort`对象指定排序依据和方向。 7. **聚合框架**: Spring Data MongoDB还提供了对...

    mongodb的odm封装

    在PHP中,ODM框架可以帮助我们更高效、更便捷地进行MongoDB的数据操作。以下是对“mongodb的odm封装”这一主题的详细解释: 1. **ODM基本概念**: - **对象-文档映射(Object-Document Mapping)**:类似于关系型...

    springMVC集成mongoDB的demo

    在这个"springMVC集成mongoDB的demo"中,我们将探讨如何将这两个强大的工具结合在一起,实现一个高效、可扩展的数据存储和检索解决方案。 首先,让我们了解一下SpringMVC。SpringMVC是基于Model-View-Controller...

Global site tag (gtag.js) - Google Analytics