# Query causing 400% CPU Load

**URL:** <https://meta.discourse.org/t/query-causing-400-cpu-load/285542>\
**Category:** Bug\
**Tags:** chat\
**Created:** [November 15, 2023, 10:46am UTC](https://meta.discourse.org/t/query-causing-400-cpu-load/285542 "2023-11-15T10:46:49Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![sok777](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sok777](https://meta.discourse.org/u/sok777)\
**Post date:** [November 15, 2023, 10:46am UTC](https://meta.discourse.org/t/query-causing-400-cpu-load/285542/1 "2023-11-15T10:46:49Z")

</div>

We recently been having CPU overload.  
We’ve managed to pinpoint it to one query that keeps running simultaneously for hours (see below).  
The task running is postmaster - once all are killed CPU is reduced until it is rebuilt every five minutes.

```SQL
SELECT “users”.“id”, ARRAY_AGG(ARRAY [uccm.id, c_msg.id]) AS memberships_with_unread_messages
FROM “users”
         INNER JOIN “user_options” ON “user_options”.“user_id” = “users”.“id”
         INNER JOIN user_chat_channel_memberships uccm ON uccm.user_id = users.id
         INNER JOIN chat_channels cc ON cc.id = uccm.chat_channel_id
         INNER JOIN chat_messages c_msg ON c_msg.chat_channel_id = uccm.chat_channel_id
         LEFT OUTER JOIN chat_mentions c_mentions ON c_mentions.chat_message_id = c_msg.id
WHERE “user_options”.“chat_enabled” = TRUE
  AND “user_options”.“chat_email_frequency” = 1
  AND (users.last_seen_at < ‘2023 - 11 - 15 09:54:26.931723’)
  AND (c_msg.deleted_at IS NULL AND c_msg.user_id <> users.id)
  AND (c_msg.created_at > ‘2023 - 11 - 08 10:09:26.931848’)
  AND ((uccm.last_read_message_id IS NULL OR c_msg.id > uccm.last_read_message_id) AND
       (uccm.last_unread_mention_when_emailed_id IS NULL OR c_msg.id > uccm.last_unread_mention_when_emailed_id) AND
       (uccm.user_id = c_mentions.user_id AND uccm.following IS true AND
        cc.chatable_type = ‘Category’

```

**Can anyone help or suggest a solution?**

More info:

- running 3.2.0.beta4-dev, but this has been happening for at least a few months
- Here’s a list of plugins we have installed:

 ![Screenshot 2023-11-15 at 10.43.22](https://global.discourse-cdn.com/meta/original/4X/6/c/e/6ce3a05268396ec9ffc8df4232421c790b003346.png)

Thank you!

---

_[View the full topic](https://meta.discourse.org/t/query-causing-400-cpu-load/285542)._
