当进口商运行SQL查询附件ID并将其排队以删除时

发布于 2025-02-08 15:29:23 字数 2109 浏览 2 评论 0原文

我一直在狂野的鹅大通上,试图找到罪魁祸首,因为我们的网站降低了我们的网站。我已经使用top -i对其进行了跟踪,然后检查了使用pidstat -t -p {process_id} 1的过程显示90+%CPU使用情况。最后,我停止了PIDSTAT的跟踪,抓住了MySQLD命令的TID,并用mysql>从performance_schema.threads选择thread_os_id = {process_id} \ g

mysql> select * from performance_schema.threads where THREAD_OS_ID = {PROCESS_ID} \G
*************************** 1. row ***************************
     THREAD_ID: 61
        NAME: thread/sql/one_connection
        TYPE: FOREGROUND
   PROCESSLIST_ID: 36
  PROCESSLIST_USER: {USER}
  PROCESSLIST_HOST: localhost
   PROCESSLIST_DB: {DB_NAME}
PROCESSLIST_COMMAND: Query
  PROCESSLIST_TIME: 0
 PROCESSLIST_STATE: Sending data
  PROCESSLIST_INFO: SELECT ID
            FROM wp_posts
            WHERE post_title = 'https://med05.example.co.uk/in4glestates/{SERIAL_NUMBER}/{SERIAL_NUMBER}/main/LOGO-MA-Roof-Seating.jpg'
            AND post_type = 'attachment'
  PARENT_THREAD_ID: NULL
        ROLE: NULL
    INSTRUMENTED: YES
      HISTORY: YES
  CONNECTION_TYPE: Socket
    THREAD_OS_ID: 8189
1 row in set (0.00 sec)

现在,我知道造成了很多麻烦的查询,而MySQL查询的ProcessList_Info表明,正是这一代码块对CPU造成了很大的需求:

private static function reset_attachments($new_property)
{
    global $wpdb;
    $sql = "SELECT ID FROM {$wpdb->prefix}posts WHERE post_parent = {$new_property} AND post_type='attachment'";
    $res = $wpdb->get_results($sql);
    foreach($res as $row) {
         wp_delete_attachment($row->ID, true);
    }
    return null;
}

我不是初学者,但这确实是我最大的尝试,我想知道是否有人可以帮助我加速我的功能reset_attachments()至我的CPU的耐受水平?

为什么此功能需要这么多CPU?

哪些更有效的方法是写下我的函数reset_attachments()?

,如果您想要任何进一步的信息,请让众所周知,我很乐意帮助您帮助我!

edit

考虑在这里附加此答案,引用processList_State:发送数据

https ://stackoverflow.com/a/24626122/10134447

我如何修改函数reset_attachments()将其分解以使其可以用交错的方法处理查询?

我很高兴牺牲某些执行时间,如果可以减轻CPU负载。

I have been on a wild goose chase trying to find the culprit downing our website. I have tracked it down using top -i followed by examining the process showing 90+% CPU usage with pidstat -t -p {PROCESS_ID} 1. Lastly I stopped the tracking of pidstat grabbed the TID of the mysqld command and queried it in mysql on the cli with mysql> select * from performance_schema.threads where THREAD_OS_ID = {PROCESS_ID} \G.

mysql> select * from performance_schema.threads where THREAD_OS_ID = {PROCESS_ID} \G
*************************** 1. row ***************************
     THREAD_ID: 61
        NAME: thread/sql/one_connection
        TYPE: FOREGROUND
   PROCESSLIST_ID: 36
  PROCESSLIST_USER: {USER}
  PROCESSLIST_HOST: localhost
   PROCESSLIST_DB: {DB_NAME}
PROCESSLIST_COMMAND: Query
  PROCESSLIST_TIME: 0
 PROCESSLIST_STATE: Sending data
  PROCESSLIST_INFO: SELECT ID
            FROM wp_posts
            WHERE post_title = 'https://med05.example.co.uk/in4glestates/{SERIAL_NUMBER}/{SERIAL_NUMBER}/main/LOGO-MA-Roof-Seating.jpg'
            AND post_type = 'attachment'
  PARENT_THREAD_ID: NULL
        ROLE: NULL
    INSTRUMENTED: YES
      HISTORY: YES
  CONNECTION_TYPE: Socket
    THREAD_OS_ID: 8189
1 row in set (0.00 sec)

Now I am aware of the query that is causing so much trouble, and the PROCESSLIST_INFO from the mysql query suggests it is this block of code that is causing so much demand on the CPU:

private static function reset_attachments($new_property)
{
    global $wpdb;
    $sql = "SELECT ID FROM {$wpdb->prefix}posts WHERE post_parent = {$new_property} AND post_type='attachment'";
    $res = $wpdb->get_results($sql);
    foreach($res as $row) {
         wp_delete_attachment($row->ID, true);
    }
    return null;
}

I am not a beginner but this truly is my greatest attempt, I was wondering if there is anyone who can help me speed my function reset_attachments() up to a tolerable level for my CPU?

Why is this function requiring so much CPU?

What is a more efficient way to write my function reset_attachments()?

As always, if you would like any further information, please let it be known and I am more than happy to help you help me!

EDIT:

Thought to append this answer here referencing the PROCESSLIST_STATE: Sending data:

https://stackoverflow.com/a/24626122/10134447

How can I amend the function reset_attachments() to break it down so it can handle the query in a staggered approach?

I am happy to sacrifice some execution time if it can bring the CPU load down.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文