# User Card display performance

**URL:** https://meta.discourse.org/t/user-card-display-performance/23747
**Category:** Bug
**Tags:** slow-sql
**Created:** [2015 年 1 月 7 日午後 5:09 UTC](https://meta.discourse.org/t/user-card-display-performance/23747 "2015-01-07T17:09:44Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [2015 年 1 月 7 日午後 5:09 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/1 "2015-01-07T17:09:44Z")

</div>

Thanks @zogstrip for this commit:  
[https://github.com/discourse/discourse/commit/e20078a9dc34ae4b03e33277a553e41af8066942](https://github.com/discourse/discourse/commit/e20078a9dc34ae4b03e33277a553e41af8066942)

I had mentioned performance issues with this here:

> [@I would like a site setting that allows TL0 to PM staff](https://meta.discourse.org/t/i-would-like-a-site-setting-that-allows-tl0-to-pm-staff/23296/65):
>
> For me I hate having to click a users avatar to PM - mainly because it takes ~3.5 seconds to load the user card after a click as an admin.

For me the general loading in user cards has gone from ~3.5 seconds to ~2 seconds…  
… It may not seem like much - but it really helps - Thanks.

EDIT: I couldn’t think of a better category other than “bug” - feel free to re-categorise.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2015 年 1 月 7 日午後 5:27 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/2 "2015-01-07T17:27:05Z")

</div>

You’re welcome 😉

Mind sharing the slowest queries? I’m guessing it’s other `COUNT(*)` queries…

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [2015 年 1 月 7 日午後 5:42 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/3 "2015-01-07T17:42:52Z")

</div>

No problem - here you are:

##Query 1  
Executing action: show  
T+34.8 ms  
Reader  
**1284.2 ms**

```
lib/sql_builder.rb:62:in `exec'
app/models/user_action.rb:74:in `stats'
app/serializers/user_serializer.rb:175:in `stats'
app/controllers/application_controller.rb:220:in `render_json_dump'
app/controllers/users_controller.rb:38:in `block (2 levels) in show'
app/controllers/users_controller.rb:31:in `show'
lib/middleware/anonymous_cache.rb:119:in `call'
lib/middleware/unicorn_oobgc.rb:95:in `process_client'

```

###SQL

```
SELECT action_type, COUNT(*) count
FROM user_actions a
JOIN topics t ON t.id = a.target_topic_id
LEFT JOIN posts p on p.id = a.target_post_id
JOIN posts p2 on p2.topic_id = a.target_topic_id and p2.post_number = 1
LEFT JOIN categories c ON c.id = t.category_id
WHERE (a.user_id = 12) AND (t.deleted_at is null)
GROUP BY action_type

```

##Query 2  
Executing action: show  
T+1481.2 ms  
Reader  
**48.8 ms**

```
app/models/user_action.rb:88:in `private_messages_stats'
app/serializers/user_serializer.rb:250:in `private_messages_stats'
app/controllers/application_controller.rb:220:in `render_json_dump'
app/controllers/users_controller.rb:38:in `block (2 levels) in show'
app/controllers/users_controller.rb:31:in `show'
lib/middleware/anonymous_cache.rb:119:in `call'
lib/middleware/unicorn_oobgc.rb:95:in `process_client'

```

### SQL

```
SELECT COUNT(*) FROM "topics" LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = 12) WHERE ("topics"."deleted_at" IS NULL) AND (topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = 12)) AND "topics"."archetype" = 'private_message' AND (tu.last_read_post_number IS NULL OR tu.last_read_post_number < topics.highest_post_number) 

```

##Query 3  
Although this one is not that big - it’s very simular to the one above and runs just before it

Executing action: show  
T+1463.4 ms  
Reader  
**14.6 ms**

```
app/models/user_action.rb:87:in `private_messages_stats'
app/serializers/user_serializer.rb:250:in `private_messages_stats'
app/controllers/application_controller.rb:220:in `render_json_dump'
app/controllers/users_controller.rb:38:in `block (2 levels) in show'
app/controllers/users_controller.rb:31:in `show'
lib/middleware/anonymous_cache.rb:119:in `call'
lib/middleware/unicorn_oobgc.rb:95:in `process_client'

```

### SQL

```
SELECT COUNT(*) FROM "topics" LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = 12) WHERE ("topics"."deleted_at" IS NULL) AND (topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = 12)) AND "topics"."archetype" = 'private_message' AND "topics"."user_id" = 12

```

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2015 年 1 月 7 日午後 5:52 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/4 "2015-01-07T17:52:37Z")

</div>

We should definitely do something about query #1.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2015 年 1 月 7 日午後 8:44 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/5 "2015-01-07T20:44:20Z")

</div>

Perhaps the user card could be broken into 2 requests, one for the data essential to display it and another for the admin-only extra data?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2015 年 1 月 7 日午後 8:46 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/6 "2015-01-07T20:46:20Z")

</div>

Unfortunately query #1 isn’t an admin-only request… It’s part of the “standard” user serializer.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2015 年 1 月 7 日午後 9:13 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/7 "2015-01-07T21:13:02Z")

</div>

Wait, what the heck is that used for? It’s getting… user actions on the first post of all topics?

**EDIT** : That must be the ‘flagged topics’ count. That’s admin-only.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015 年 1 月 7 日午後 10:29 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/8 "2015-01-07T22:29:45Z")

</div>

yeah we need to restructure the serializer.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2015 年 1 月 8 日午前 12:24 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/9 "2015-01-08T00:24:00Z")

</div>

This is slow enough to justify the bug label in my book… it is symptomatic of deeper issues.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2015 年 2 月 2 日午前 3:07 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/10 "2015-02-02T03:07:43Z")

</div>

@DeanMarkTaylor is this OK now?

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [2015 年 2 月 3 日午前 1:00 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/13 "2015-02-03T01:00:22Z")

</div>

I’m currently awaiting a couple of [known bugs](https://meta.discourse.org/t/new-1-unread-1-for-every-incoming-post/24752/) to be fixed before updating.  
As soon as I see them resolved I will update and report back.

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [2015 年 2 月 3 日午前 2:22 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/14 "2015-02-03T02:22:56Z")

</div>

I can now report that the user card loading times…  
`Discourse 1.2.0.beta5 - https://github.com/discourse/discourse version 80d8b133d948c742275052123cad6ba753937ae0`

The loading time is clearly linked to the number of topics / posts a user has.

Numbers from profiler.

When selecting an older user with 5540 posts the time is ~2045ms (feels like 2.8 seconds to display), with the slowest query:

 ![](https://global.discourse-cdn.com/meta/original/3X/6/1/61e5c8ff70661df41da0427c13303808306e9dc8.png)

When selecting an older user with 2668 posts the time is ~1726ms (feels like 2.5 seconds to display), with the slowest query:  
 ![](https://global.discourse-cdn.com/meta/original/3X/7/6/764e7ad419383a37ca95f84e05ebc07b9eab28e2.png)

When selecting an newer user with 12 posts the time is ~373ms (feels like 1 second to display), with the slowest query:

 ![](https://global.discourse-cdn.com/meta/original/3X/9/8/986ef03a7b95c9468bc1dae7e2be932be9d727e8.png)

That same user `12` from the previous slow query had ~3803 posts - it appears I have clicked this user too many times and the cached time is ~617ms (feels like ~2 seconds).

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015 年 2 月 24 日午前 2:40 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/15 "2015-02-24T02:40:25Z")

</div>

This is now fixed via:

[https://github.com/discourse/discourse/commit/e9c5e17fbe3e4d7730d523778d254c1c1d9bc181](https://github.com/discourse/discourse/commit/e9c5e17fbe3e4d7730d523778d254c1c1d9bc181)

and

[https://github.com/discourse/discourse/commit/130dbf7358272da5c902e2e9afb8dfadcdd7497c](https://github.com/discourse/discourse/commit/130dbf7358272da5c902e2e9afb8dfadcdd7497c)

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [2015 年 2 月 24 日午前 3:36 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/16 "2015-02-24T03:36:25Z")

</div>

> [@sam](#):
>
> This is now fixed

Wooo - looks awesome and even better it feels soo much better as a user!

> [@DeanMarkTaylor](#):
>
> When selecting an older user with 5540 posts the time is ~2045ms (feels like 2.8 seconds to display)

Here are some times from clicking both old and new users with varying numbers of posts including that same user of with a 2 second+ load time:  
 ![](https://global.discourse-cdn.com/meta/original/3X/7/4/74abcea945bb48ff633c056212a92a0ae30ff52d.png)

So it looks like this has basically taken well over 1 second off of loading the User Card.

Another job well done @sam!

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015 年 2 月 24 日午前 4:09 UTC](https://meta.discourse.org/t/user-card-display-performance/23747/17 "2015-02-24T04:09:06Z")

</div>


