# Slow SQL query causes homepage to load in 2-4 sec

**URL:** <https://meta.discourse.org/t/slow-sql-query-causes-homepage-to-load-in-2-4-sec/74261>\
**Category:** Product Support\
**Created:** [17. November 2017 um 13:24 UTC](https://meta.discourse.org/t/slow-sql-query-causes-homepage-to-load-in-2-4-sec/74261 "2017-11-17T13:24:17Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)\
**Post date:** [1. Februar 2018 um 17:19 UTC](https://meta.discourse.org/t/slow-sql-query-causes-homepage-to-load-in-2-4-sec/74261/9 "2018-02-01T17:19:06Z")

</div>

In both those queries, this step takes about 1.6 seconds:

```plaintext
-> Seq Scan on topics (cost=0.00..206318.55 rows=715171 width=21) (actual time=1.650..1675.564 rows=532482 loops=1)    
    Filter: ((deleted_at IS NULL) AND (deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text))
    Rows Removed by Filter: 23895

```

Filtering out deleted topics and private messages still leaves 532k topics in the result. After all other filters have completed, only 85 rows are returned.

Maybe we can simplify this filter, which is removing 531k topics:

```plaintext
Join Filter: (((NOT c.read_restricted) OR u.admin OR (hashed SubPlan 1)) AND (topics.created_at >= GREATEST(CASE WHEN (COALESCE(uo.new_topic_duration_minutes, 2880) = '-1'::integer) THEN u.created_at WHEN (COALESCE(uo.new_topic_duration_minutes, 2880) = '-2'::integer) THEN COALESCE(u.previous_visit_at, u.created_at) ELSE ('2018-02-01 08:14:09.813495'::timestamp without time zone - ('00:01:00'::interval * (COALESCE(uo.new_topic_duration_minutes, 2880))::double precision)) END, us.new_since, '2018-01-26 19:25:08'::timestamp without time zone)))
                Rows Removed by Join Filter: 531113

```

It includes `topics.created_at >= ` some date, but i guess the `CASE` is burying it. /shrug

@Falco Any ideas?

---

_[View the full topic](https://meta.discourse.org/t/slow-sql-query-causes-homepage-to-load-in-2-4-sec/74261)._
