当前位置: 首页 > 面试题库 >

MySQL语句执行需要超过一分钟的时间

宰父远
2023-03-14
问题内容

我正在一个数据库很大的网站上工作。当时表中有1百万条记录。当我执行查询时,这将花费太多时间来执行。以下是一个示例查询:

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

每个查询都需要一分钟以上的时间,但是当我将表放到1万条记录中时,该查询就会快速执行。

正如我所读过的,在一个表中有一百万条记录没有问题,因为在数据库表中没有大记录的问题。

我已经通过堆栈溢出问题在表中使用了ID索引,
如何向MySQL表添加索引 ,但仍然有同样的问题。

***我正在为该项目使用CodeIgniter。


问题答案:

请注意,这并不是建议您过一会儿再使用MyISAM。我只用它来获取我的ID,最小,最大和计数。因此,请忽略引擎。

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子句。

不能排除出现僵局的可能性。



 类似资料:
  • 我在Redis git hub上发布了这个问题,如果我看到任何回复,我会更新双方。 在VisualStudio 2015和x64上运行C 我注意到调用“get”几乎需要2秒钟才能返回一个值。我的钥匙是“控制:107:1”;我在本地机器上运行Redis服务器;它有大约200把钥匙。 我甚至解构了这个命令:redis_客户端- 我的结果是: redis_client- 收到1939年 val.as_s

  • 本文向大家介绍MySQL开启记录执行过的SQL语句方法,包括了MySQL开启记录执行过的SQL语句方法的使用技巧和注意事项,需要的朋友参考一下 概述 很多时候,我们需要知道 MySQL 执行过哪些 SQL 语句,比如 MySQL 被注入后,需要知道造成什么伤害等等。只要有 SQL 语句的记录,就能知道情况并作出对策。服务器是可以开启 MySQL 的 SQL 语句记录功能,从而就能间接地检测到客户端

  • 我真的需要帮助,我在stackoverflow上找到的所有问题中都搜索到了,但没有任何效果。我以前从未使用过hibernate,我不知道自己做错了什么 这是我的存储库:https://github.com/ionutincau/test_db 我收到了这个错误:

  • 我试图从Coldfusion 10服务器执行一个MySQL insert语句,这会导致一个Cold Fusion错误。 当从MySQL工作台执行MySQL insert语句时,它可以正常工作。此外,我确认在Coldfusion 10服务器上执行的select语句工作正常。 接下来,我尝试设置变量“binlog format”cfquery本身。 这导致了一个新的CFE。 最后,我尝试在“MySQL

  • 问题内容: 在我的Web应用程序中,一些postgres sql查询需要一些时间才能执行。我只想为其中一部分设置语句超时。 查询的一部分必须通过超时取消,但是其他部分必须可以不受任何限制地工作。 在postgres中存在statement_timeout函数。 如何使用statement_timeout函数包装SqlAlchemy查询? 像这样: 对我来说,设置超时的最佳方法是这样的: SqlAl

  • 我在3个VM上运行Spark1.6(即1x主服务器;2x从服务器),它们都有4个内核和16GB RAM。 正如你所看到的,这需要很长的时间。我的表实际上相当大(存储了大约2.2亿行,每行11个字段),但是这样的查询可以立即使用“普通”sql(例如pyodbc)执行。 我想我没有理解/没有使用Spark,你会有这样的想法或建议来让它更好地工作吗?