private function testWhereBenchMark() { Debug::mark('startSelect'); $select = Query::select('test'); Debug::mark('endSelect'); Debug::mark('startWhere'); $where1 = Where::createPair('test', 'zzzzzzzzzzzzzzzzzzzzzzz')->orCase(Where::createPlain('test is null')); $where = Where::createQuote('num in (?)', array(3, 5, 16))->andCase($where1, Where::HAND_LEFT); Debug::mark('endWhere'); //初始化adapter后才能escape $select->where($where); //echo $select->toString(); Debug::mark('startQuery'); $re = $select->query(); Debug::mark('endQuery'); \HuiLib\Helper\Debug::out($re->fetchAll()); Debug::elapsed('startSelect', 'endSelect'); Debug::elapsed('startWhere', 'endWhere'); Debug::elapsed('startQuery', 'endQuery'); Debug::elapsed('startSelect', 'endQuery'); Debug::elapsed('startSelect', 'endALL'); }
/** * 通过单个Field的多个IDS获取多条记录 * * 通过where in实现 * * @param string $field * @param string $ids */ public function getListByIds($field, $ids) { $select = Query::select(static::TABLE); if ($this->dbAdapter !== NULL) { $select->setAdapter($this->dbAdapter); } if ($this->forUpdate) { $select->enableForUpdate(); } return $this->rowSetObject($select->where(Where::createQuote($field . ' in (?) ', $ids))); }
/** * 从数据库获取数据,重建列表 */ protected function importFromDb() { //通过主键尝试数据表获取数据 $tableClass = static::TABLE_CLASS; $primaryIdKey = $this->getRowPrimaryIdKey(); $primaryId = 0; $fields = explode(',', $this->hashValueField); array_push($fields, $this->hashKeyField, $primaryIdKey); $fields = array_unique($fields); //删除旧数据 $this->getAdapter()->delete($this->getRedisKey()); do { $select = $tableClass::create()->select()->columns($fields); $select->where(Where::createQuote($primaryIdKey . ' >?', $primaryId))->limit(self::FETCH_PER_ACTION)->order($primaryIdKey . ' asc'); //echo $select->toString()."\n"; $dataList = $select->query()->fetchAll(); if ($dataList) { $result = array(); foreach ($dataList as $iter => $valueUnit) { //包含无限循环的要尽量限制严格些 if (!isset($valueUnit[$this->hashKeyField])) { throw new \Exception('Field fetch error.'); } $valueString = $this->getValueString($valueUnit); $keyString = $this->formatHashKey($valueUnit[$this->hashKeyField]); if (empty($valueString) || empty($keyString)) { continue; } $result[$keyString] = $valueString; } //包含无限循环的要尽量限制严格些 if (!isset($valueUnit[$primaryIdKey])) { throw new \Exception('Value of primary key fetch error.'); } $primaryId = $valueUnit[$primaryIdKey]; $this->getAdapter()->hMset($this->getRedisKey(), $result); } } while (!empty($dataList)); //埋Redis更新时间戳 $this->getAdapter()->hSet($this->getRedisKey(), self::REDIS_UPDATE_KEY, time()); return TRUE; }