我在一个数据库很大的网站上工作。当时表中有 100 万条记录。当我执行查询时,执行需要花费太多时间。下面给出了一个示例查询:
select * from `ratings` order by id limit 499500, 500
每个查询都需要一分钟多的时间,但是当我将表减少到 10,000 条记录时,这个查询执行得很快。
据我所知,一个表中有 100 万条记录没有问题,因为在数据库表中,没有大记录的问题。
我在 Stack Overflow 问题的帮助下在表中使用了 id 索引 How do I add indices to MySQL tables? ,但我还是遇到了同样的问题。
*** 我正在使用 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),(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;
我现在有 470 万行
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 子句。
不能排除死锁的可能性。
关于php - MySQL 语句执行时间超过一分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33665994/