作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 yii 框架
开发的应用程序,其中一个查询花费的时间太长。如果没有 criteria->order
操作,它会在 1 秒内返回数据。
该表有 100 万个注册。每列都有索引。查询是:
select *
from village
where map_type_id = 4 and status = 1
order by last_update_resource asc
limit 1
CREATE TABLE IF NOT EXISTS `village` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`x` int(11) NOT NULL DEFAULT '0',
`y` int(11) NOT NULL DEFAULT '0',
`k` int(11) NOT NULL DEFAULT '0',
`map_type_id` int(11) NOT NULL,
`village_type_id` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
`user_id` int(11) NOT NULL DEFAULT '0',
`nation_id` int(2) NOT NULL DEFAULT '0',
`points` int(11) NOT NULL DEFAULT '0',
`last_update` int(11) NOT NULL DEFAULT '0',
`last_update_resource` int(11) NOT NULL,
`verified` int(1) NOT NULL DEFAULT '0',
`last_verification` int(11) NOT NULL,
`last_update_points` int(11) NOT NULL DEFAULT '0',
`status` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `x` (`x`),
KEY `y` (`y`),
KEY `k` (`k`),
KEY `user_id` (`user_id`),
KEY `nation_id` (`nation_id`),
KEY `last_update_resource` (`last_update_resource`),
KEY `map_type_id` (`map_type_id`),
KEY `status` (`status`),
KEY `village_type_id` (`village_type_id`),
KEY `last_update` (`last_update`),
KEY `verified` (`verified`),
KEY `last_verification` (`last_verification`),
KEY `name` (`name`),
KEY `points` (`points`),
KEY `last_update_points` (`last_update_points`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1000001 ;
如何使请求时间达到 1 秒?
$criteria = new CDbCriteria();
$criteria->order = 'last_update_resource asc';
$criteria->limit = 1;
VillageSlaveM::$server_id = $world_id;
$model_village = VillageSlaveM::model()->findByAttributes(
array(
'map_type_id' => VillageM::$map_type_id['village'],
'status' => VillageM::ACTIVE), $criteria
);
最佳答案
在 (map_type_id ,status,last_update_resource )
上放置一个复合索引以涵盖您的 where 条件。
关于mysql - 使用 ORDER BY 时如何减少 sql 查询时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25220007/
我是一名优秀的程序员,十分优秀!