gpt4 book ai didi

mysql - 使用 ORDER BY 时如何减少 sql 查询时间

转载 作者:行者123 更新时间:2023-11-29 05:21:32 24 4
gpt4 key购买 nike

我有一个使用 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 条件。

http://www.sqlfiddle.com/#!2/3bc6b/1

关于mysql - 使用 ORDER BY 时如何减少 sql 查询时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25220007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com