简体   繁体   English

为什么此UPDATE查询会杀死我的CPU?

[英]Why would this UPDATE query be killing my CPU?

So over time I've noticed my website getting slower and slower, normally at peak traffic hours and weekends - the CPU load would hit 10x what it should be and often the website would turn unresponsive. 因此,随着时间的流逝,我注意到我的网站越来越慢,通常是在高峰时段和周末-CPU负载将达到应有的10倍,并且通常网站会变得无响应。

After much hacking away at stuff, I've discovered commenting out this line brings the CPU load down to normalcy: 经过大量修改之后,我发现注释掉这行代码会使CPU负载降低到正常水平:

$this->file_hits++;
$this->weekly_file_hits++;
$this->daily_file_hits++;
$this->file_last_dl_ip = $downloader_ip;
$this->file_last_dl_time = current_time('mysql');

$wpdb->query("UPDATE " . $wpdb->wpfilebase_files
    . " SET file_hits = file_hits + 1, weekly_file_hits = weekly_file_hits + 1, daily_file_hits = daily_file_hits + 1, file_last_dl_ip = '"
    . $downloader_ip . "', file_last_dl_time = '"
    . $this->file_last_dl_time . "' WHERE file_id = "
    . (0+$this->file_id));

This is code from a WordPress plugin. 这是来自WordPress插件的代码。 It fires when a file is downloaded, and is pretty self explanatory, just logs hits/IP/date etc into the appropriate row in the DB. 它在下载文件时触发,并且很容易解释,只需将命中/ IP /日期等记录到数据库中的相应行中。

I don't understand why this pretty simple update query would result in this? 我不明白为什么这个非常简单的更新查询会导致这种情况?

Some info: 一些信息:

  1. The website handles well under normal traffic ~150 simultaneous users, most of whom are not logged in and thus receive cached pages. 该网站在大约150个同时访问的正常流量下可以很好地处理用户,其中大多数没有登录,因此可以接收缓存的页面。
  2. Under large traffic ~300 simultaneous users, is when things get bad. 在大流量的情况下,大约300个并发用户会遇到麻烦。 But even at normal traffic CPU can jump around to "bad" ranges too. 但是即使在正常通信量下,CPU也会跳到“不良”范围。 Point is, the extreme CPU load seems very sporadic but is definitely more pronounced with heavier traffic. 关键是,极端的CPU负载似乎是零星的,但在流量较大的情况下,绝对更加明显。
  3. Removing this query - so downloads are still served and all the other download-related code is still executed besides the DB being updated - and CPU load stays at "good" to "normal" ranges even with ~300 users online. 删除此查询-这样就可以继续进行下载,并且除正在更新的DB外,其他所有与下载相关的代码也仍在执行-并且即使有约300个用户在线,CPU的负载也始终保持在“正常”至“正常”范围内。 So this query is definitely the culprit, or is at least the biggest culprit. 因此,此查询绝对是罪魁祸首,或者至少是最大的罪魁祸首。
  4. The DB table in question contains something like 23,000 rows. 有问题的数据库表包含大约23,000行。 If anything I'd think the text searching would have been the cause for this, but disabling users' front-end search didn't remedy anything like this did. 如果有什么原因,我认为可能是文本搜索引起的,但是禁用用户的前端搜索并不能解决这种问题。

It looks like your UPDATE query is running slowly. 看起来您的UPDATE查询运行缓慢。 That could be because 那可能是因为

  1. the table in your database has become fragmented or messed up somehow, or 数据库中的表因某种原因变得碎片化或混乱,或者
  2. it needs an index on the file_id column so WHERE file_id = something can run faster in your UPDATE . 它需要在file_id列上有一个索引,因此WHERE file_id = somethingUPDATE可以更快地运行。

Is this the WP-Filebase plugin? 这是WP-Filebase插件吗? If so, the usual advice to contact the plugin author doesn't apply, because the plugin has been closed by the wordpress.org authorities. 如果是这样,则联系插件作者的通常建议不适用,因为该插件已被wordpress.org当局关闭。

You are doing backups, right? 您正在备份,对不对? If not, back up your database before doing anything else. 如果没有,请先备份数据库,然后再执行其他操作。

You need to fix up the slow table yourself. 您需要自己修复慢速表。 It's best to do this at a low-traffic time for your site. 最好在您的网站流量少的时候执行此操作。

Open up phpmyadmin, open your WordPress database, and look for a table with a name like wp_filebase_files or wp_filebasefiles . 打开phpmyadmin,打开您的WordPress数据库,并查找名称类似于wp_filebase_fileswp_filebasefiles Click on the name. 单击名称。 (I don't know exactly what the table name is.) (我不知道确切的表名是什么。)

Then click the Operations tab at the top of your screen. 然后,单击屏幕顶部的“操作”选项卡。

Then, under Table maintenance click the Optimize Table link. 然后,在“表维护”下,单击“优化表”链接。 It may run for a while. 它可能会运行一段时间。 Then see if performance improves. 然后查看性能是否提高。 If that doesn't help, click the Repair Table link. 如果那没有帮助,请单击“修复表”链接。 Then try performance again. 然后再次尝试性能。

If these don't help, you may need an index on your table. 如果这些方法没有帮助,则您可能需要在表上建立索引。 Here's how to add one. 这是添加一个的方法。

On the left side of phpMyAdmin, click the [+] sign next to your table, then click the [+] signs next to Columns and Indexes. 在phpMyAdmin的左侧,单击表旁边的[+]符号,然后单击“列和索引”旁边的[+]符号。

If it shows file_id as an index, you already have the index your query needs, and my advice can't help you. 如果它显示file_id作为索引,则说明您已经具有查询所需的索引,而我的建议无济于事。 (Sorry.) (抱歉。)

If it doesn't, click New under Indexes. 如果不是,请单击索引下的“ 新建 ”。 You'll get a dialog box. 您将看到一个对话框。 Fill it out like this. 像这样填写。

  • Index name: filebase_file_id 索引名称:filebase_file_id
  • Index choice: choose "INDEX" from the dropdown menu. 索引选择:从下拉菜单中选择“索引”。
  • Column: from the dropdown, pick file_id. 列:从下拉列表中选择file_id。
  • Size: leave it blank. 大小:留空。

Then click Go. 然后单击执行。 It may take a while to create the index. 创建索引可能需要一段时间。 Then see if your performance picks up. 然后查看您的演奏是否加快。

This kind of thing happens to busy databases; 这种情况发生在繁忙的数据库中。 it's normal, but it's a pain in the neck. 这很正常,但是脖子很疼。

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

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