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

前端之家收集整理的这篇文章主要介绍了php – MySQL语句需要花费多一点时间来解决前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个数据库庞大的网站上工作.当时有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子句.

不能排除僵局的可能性.

原文链接:https://www.f2er.com/php/130534.html

猜你在找的PHP相关文章