我们有一个拥有大约 300,000 人的网站 - 我们要做的是创建一个页面,其中包含所有以 A、B 或 C 开头的人名。
挑战在于速度。
你将如何设置你的数据库。你做一个缓存集合。你使用正则表达式还是其他东西。
我所做的是以下内容;
$letter = 'A';
$where = array();
$where['name'] = new MongoRegex("/^" . $letter . "/i");
$sort = array('name' => 1);
if($hasImage){
$where['images.profiles.0'] = array('$exists' => true);
}
$fields = array('name' => 1, 'images.profiles' => 1);
$this->mdb->data_people->ensureIndex(array('name' => 1, 'images.profiles' => 1), array('background' => true) );
$people = $this->mdb->data_people->find( $where, $fields );
$people = $people->sort( $sort );
$page['total'] = 100;
$page['current'] = 1;
$page['perPage'] = 20;
if(isset($this->domain->getQuery['_page']) && $this->domain->getQuery['_page'] > 1){
$page['current'] = $this->domain->getQuery['_page'];
}
$data['pages'] = $this->pageNavigation->setNavigation((int) $page['total'], (int) $page['perPage'], (int) $page['current'] );
$data['pages']['page'] = $this->domain->menu_reverse[0];
$data['pages']['path'] = $this->domain->path;
$data['data'] = $people->limit($page['perPage'])->skip( ($page['perPage']*($page['current']-1)) );