设置:
db.t3.xlarge
RDS 实例。- MySQL 8.0.33 引擎。
- 默认参数和选项组。
- ~900k 记录表。
- RDS 根本没有启用日志记录,我们现在无法重新启动实例来启用它。
该数据库连接到在 Lambda 中运行的 Laravel 应用程序。lambda 中的过程之一需要计算表中的行数spin
。
在第一次迭代期间,我们发现select count()
以下面的方式编写的内容有时会花费大量的秒数。有些请求是立即发出的,而另一些请求可能需要 20 秒以上。
select count(*) from spin;
我们在互联网上挖掘了一些抱怨此事的人的答案。我们决定向查询添加一个条件,这使其成为亚秒级查询:
select count(*) from spin where id > 0;
直到几天前,我们的服务开始收到比平时更多的流量,并且查询运行时间变得非常跳跃。
+--------+-------+------------------+---------+---------+------+-----------+----------------------------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+--------+-------+------------------+---------+---------+------+-----------+----------------------------------------------------------+
| 114168 | vmx | 10.0.2.175:43169 | fdata | Execute | 60 | executing | select count(*) as aggregate from `spin` where `id` > 0 |
| 114171 | vmx | 10.0.3.149:31136 | fdata | Execute | 58 | executing | select count(*) as aggregate from `spin` where `id` > 0 |
| 114118 | vmx | 10.0.2.175:36571 | fdata | Execute | 109 | executing | select count(*) as aggregate from `spin` where `id` > 0 |
+--------+-------+------------------+---------+---------+------+-----------+----------------------------------------------------------+
我怀疑这一定是由于对表的某些访问锁定造成的spin
。在表锁定期间,select count()
挂起。
任何意见表示赞赏,谢谢。