MySQL语句需要超过一分钟才能执行

M R*_*wan 9 php mysql

我正在一个数据库庞大的网站上工作.当时表中有100万条记录.当我执行查询时,执行需要太多时间.下面给出一个示例查询:

select * from `ratings` order by id limit 499500, 500
Run Code Online (Sandbox Code Playgroud)

每个查询都需要超过一分钟,但是当我将表删除到1万条记录时,此查询会快速执行.

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

我已经在Stack Overflow问题的帮助下在表中使用了id的索引如何在MySQL表中添加索引?,但我仍然遇到同样的问题.

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

Dre*_*rew 15

请注意,这并不是建议使用MyISAM一分钟.我只是用它来得到我的id,min,max和count来排队.所以请忽略引擎.

create table ratings
(   id int auto_increment primary key,
    thing int null
)engine=MyISAM;
insert ratings (thing) values (null),(null),(null),(null),(null),(null),(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;
Run Code Online (Sandbox Code Playgroud)

我现在有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
Run Code Online (Sandbox Code Playgroud)

.

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 |
+----+-------------+---------+------+---------------+------+---------+------+---------+----------------+
Run Code Online (Sandbox Code Playgroud)

.

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 |
+----+-------------+---------+-------+---------------+---------+---------+------+---------+-----------------------+
Run Code Online (Sandbox Code Playgroud)

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

人们不能排除陷入僵局的可能性.