php – MySQL语句需要花费多一点时间来解决

我在一个数据库庞大的网站上工作.当时有100万条记录在表中.当我执行查询时,花费太多时间来排除.一个示例查询如下: select * from `ratings` order by id limit 499500, 500 每个查询都需要一分钟以上的时间,但是当我把表放到10万条记录上时,这个查询执行得很

我在一个数据库庞大的网站上工作.当时有100万条记录在表中.当我执行查询时,花费太多时间来排除.一个示例查询如下:

select * from `ratings` order by id limit 499500,500

每个查询都需要一分钟以上的时间,但是当我把表放到10万条记录上时,这个查询执行得很快.

正如我已经看到的,表中的100万条记录没有问题,因为在数据库表中,没有大记录的问题.

我在Stack Overflow问题How do I add indices to MySQL tables?的帮助下使用表中的id索引,但是我仍然遇到同样的问题.

***我正在使用CodeIgniter的项目.

请注意,这不是建议使用MyISAM一分钟.我使用这个只能让我的ids,min,max和count排队.所以请忽略引擎.

create table ratings
(   id int auto_increment primary key,thing int null
)engine=MyISAM;
insert ratings (thing) values (null),(null),(null);
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;

insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;

insert ratings (thing) select thing from ratings;
insert ratings (thing) select thing from ratings;

我现在有4.7M行

select count(*),min(id),max(id) from ratings;
+----------+---------+---------+
| count(*) | min(id) | max(id) |
+----------+---------+---------+
|  4718592 |       1 | 4718592 |
+----------+---------+---------+
select * from `ratings` order by id limit 499500,500;
-- 1 second on a dumpy laptop

.

explain select * from `ratings` order by id limit 499500,500;
+----+-------------+---------+------+---------------+------+---------+------+---------+----------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows    | Extra          |
+----+-------------+---------+------+---------------+------+---------+------+---------+----------------+
|  1 | SIMPLE      | ratings | ALL  | NULL          | NULL | NULL    | NULL | 4718592 | Using filesort |
+----+-------------+---------+------+---------------+------+---------+------+---------+----------------+

.

explain select * from `ratings` where id>=499501 limit 500;
+----+-------------+---------+-------+---------------+---------+---------+------+---------+-----------------------+
| id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows    | Extra                 |
+----+-------------+---------+-------+---------------+---------+---------+------+---------+-----------------------+
|  1 | SIMPLE      | ratings | range | PRIMARY       | PRIMARY | 4       | NULL | 4198581 | Using index condition |
+----+-------------+---------+-------+---------------+---------+---------+------+---------+-----------------------+

道德的故事可能是使用where子句.

不能排除僵局的可能性.

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部