简体   繁体   English

MYSQL 随机更新查询耗时较长

[英]MYSQL Update Query randomly takes a long time

I have a php script on a webserver which is called ones per second.我在网络服务器上有一个 php 脚本,称为每秒一个。 This script accesses a mysql database and reads/updates some information.此脚本访问 mysql 数据库并读取/更新一些信息。 Most of the time the script returns the result in <30ms.大多数情况下,脚本会在 <30 毫秒内返回结果。 Sometimes however, the scripts takes 4-8 seconds.然而,有时脚本需要 4-8 秒。

The table which is edited only has 1 row and 4 columns.编辑的表格只有 1 行 4 列。

By recording the time different parts of the script use, i have found that the time is always consumed by the first UPDATE query.通过记录脚本使用的不同部分的时间,我发现时间总是被第一个 UPDATE 查询所消耗。 This is also the case if the order of the queries is changed.如果查询的顺序发生变化,情况也是如此。

I have also tried putting everything in one mySql transaction, then the commit function takes the time normaly used by the first UPDATE query.我还尝试将所有内容放在一个 mySql 事务中,然后提交 function 需要第一个 UPDATE 查询正常使用的时间。

I have checked for locked tables via "show open tables WHERE In_use > 0";我已经通过“显示打开的表 WHERE In_use > 0”检查了锁定表; but none are found.但没有找到。

The code for the query which normaly takes the time is below:通常需要时间的查询代码如下:

// Add Timestamp
echo ("BeforeTimestamps Updated" . (time()- $time) . "Seconds"); // DEBUG DEBUG DEBUG
$time =  time();
// Try updating Timestamp
$sql = "UPDATE $LobbyID SET lastconnect=$reftime WHERE id = $PlayerID";
$conn -> query($sql);
echo ("Timestamps Updated" . (time()- $time) . "Seconds"); // DEBUG DEBUG DEBUG

So for some reason sometimes the first update query for a connection takes multiple seconds, and I have no idea why.所以出于某种原因,有时连接的第一次更新查询需要几秒钟,我不知道为什么。 So if anyone can help, I would be very happy.因此,如果有人可以提供帮助,我将非常高兴。

Edit: As Requested in the comments;编辑:根据评论中的要求; here is the SHOW CREATE TABLE这是显示创建表

+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                           |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| L2787828 | CREATE TABLE `L2787828` (
 `id` int unsigned NOT NULL,
 `type` varchar(2) NOT NULL,
 `command` text NOT NULL,
 `lastconnect` int NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

(Apologies if I am jumping to the wrong conclusion.) (如果我跳到错误的结论,请道歉。)

How many tables do you have?你有几张桌子? L2787828 makes me worry that you might have 2787828 tables, That is a serious problem -- There is limited caching space for tables. L2787828让我担心你可能有 2787828 个表,这是一个严重的问题——表的缓存空间有限。 opening (and the necessary closing) of lots and lots of tables would be very inefficient.打开(和必要的关闭)很多桌子效率很低。 It is much more efficient to have all similar tables combined into a single table with an extra column to distinguish the rows as needed.将所有相似的表合并到一个带有额外列的表中以根据需要区分行会更有效。

(It is quite OK to have a table with a single row, even a single column, but if it is not one of many similar tables.) (有一个单行甚至单列的表是完全可以的,但如果它不是许多类似表中的一个。)

The timing would be explained by:时间将通过以下方式解释:

  • Bump some other "open table" out of table_open_cache .table_open_cache中弹出一些其他“打开的表”。
  • Open L2787828 -- This involves touching at least two files via the OS, plus some meta tables inside InnoDB.打开L2787828这涉及通过操作系统接触至少两个文件,以及 InnoDB 中的一些元表。
  • And then proceed with the UPDATE .然后继续UPDATE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM