Yii:为什么这个查询需要15秒?

时间:2014-04-01 09:59:51

标签: mysql yii

我正在使用Yii MCV多个dbs

为什么运行此代码需要15秒?

如何改进?

我使用db connection整数值切换server_id

这是切换数据库连接的class

class VillageSlaveM extends VillageM {

    const UNVERIFIED = 0;
    const VERIFIED = 1;

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static $server_id;
    public static $slave_db;

    public function getDbConnection() {
        self::$slave_db = Yii::app()->dbx;
        if (self::$slave_db instanceof CDbConnection) {
            self::$slave_db->active = false;
            $config = require(Yii::app()->getBasePath() . '/config/main.php');
            $connectionString = $config['components']['dbx']['connectionString'];
            self::$slave_db->connectionString = sprintf($connectionString, self::$server_id);
            self::$slave_db->setActive(true);
            return self::$slave_db;
        }
        else
            throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
    }

}

这是需要15秒执行的代码,不知道为什么这么多。

它选择具有最早last_update_resource时间戳值的一个村庄。

我为所涉及的所有数据库表字段设置了索引。

$criteria = new CDbCriteria();
        $criteria->condition = 'last_update_resource <= ' . ($time_start - 60 * 60 * 8);
        $criteria->order = 'last_update_resource asc';
        $criteria->limit = 1;

        VillageSlaveM::$server_id = $world_id;
        $start_x = time();
        $model_village = VillageSlaveM::model()->findByAttributes(array('map_type_id' => VillageM::$map_type_id['village'], 'status' => VillageM::ACTIVE), $criteria);
        $stop_x = time();
        $msg[] = 'start_x: ' . ($stop_x - $start_x);

ps:在此代码运行后,我有更复杂的查询,它们会立即运行;

0 个答案:

没有答案