# 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 نوفمبر 2017، 1: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:** 1

<div class="post-metadata">

**Author:** ![TomekB](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tomekb/32/66593_2.png) [@TomekB](https://meta.discourse.org/u/TomekB)\
**Post date:** [17 نوفمبر 2017، 1:24م UTC](https://meta.discourse.org/t/slow-sql-query-causes-homepage-to-load-in-2-4-sec/74261/1 "2017-11-17T13:24:17Z")

</div>

I have some performance issues with loading the homepage of my forum. The first request takes a long time (2-4 sec) to complete. I see one query in particular that usually takes almost 2 sec. What can I do to improve? I have my DB on a dedicated server with `db_shared_buffers: "8GB"` and `db_work_mem: "100MB"`

```
lib/freedom_patches/active_record_base.rb:7:in `exec_sql'
lib/sql_builder.rb:67:in `exec'
lib/sql_builder.rb:103:in `map_exec'
app/models/topic_tracking_state.rb:178:in `report'
app/controllers/application_controller.rb:446:in `preload_current_user_data'
app/controllers/application_controller.rb:253:in `preload_json'
lib/middleware/anonymous_cache.rb:149:in `call'
config/initializers/100-quiet_logger.rb:16:in `call'
config/initializers/100-silence_logger.rb:29:in `call'
lib/middleware/request_tracker.rb:110:in `call'
lib/scheduler/defer.rb:87:in `process_client'
    SELECT 
        u.id AS user_id,
        topics.id AS topic_id,
        topics.created_at,
        highest_staff_post_number highest_post_number,
        last_read_post_number,
        c.id AS category_id,
        tu.notification_level
    FROM topics
    JOIN users u on u.id = 29622
    JOIN user_stats AS us ON us.user_id = u.id
    JOIN user_options AS uo ON uo.user_id = u.id
    JOIN categories c ON c.id = topics.category_id
    LEFT JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = u.id
    WHERE u.id = 29622 AND
        topics.archetype <> 'private_message' AND
        ((1=0) OR ("topics"."deleted_at" IS NULL AND topics.created_at >= GREATEST(CASE
                WHEN COALESCE(uo.new_topic_duration_minutes, 2880) = -1 THEN u.created_at
                WHEN COALESCE(uo.new_topic_duration_minutes, 2880) = -2 THEN COALESCE(u.previous_visit_at,u.created_at)
                ELSE ('2017-11-17 13:16:32.122898'::timestamp - INTERVAL '1 MINUTE' * COALESCE(uo.new_topic_duration_minutes, 2880))
            END, us.new_since, '2017-11-11 22:34:58') AND tu.last_read_post_number IS NULL AND COALESCE(tu.notification_level, 2) >= 2)) AND
        (topics.visible OR u.admin OR u.moderator) AND
        topics.deleted_at IS NULL AND
        ( NOT c.read_restricted OR u.admin OR category_id IN (
            SELECT c2.id FROM categories c2
            JOIN category_groups cg ON cg.category_id = c2.id
            JOIN group_users gu ON gu.user_id = 29622 AND cg.group_id = gu.group_id
            WHERE c2.read_restricted )
        )
        AND NOT EXISTS( SELECT 1 FROM category_users cu
                        WHERE last_read_post_number IS NULL AND
                            cu.user_id = 29622 AND
                            cu.category_id = topics.category_id AND
                            cu.notification_level = 0)

UNION ALL

    SELECT 
        u.id AS user_id,
        topics.id AS topic_id,
        topics.created_at,
        highest_staff_post_number highest_post_number,
        last_read_post_number,
        c.id AS category_id,
        tu.notification_level
    FROM topics
    JOIN users u on u.id = 29622
    JOIN user_stats AS us ON us.user_id = u.id
    JOIN user_options AS uo ON uo.user_id = u.id
    JOIN categories c ON c.id = topics.category_id
    LEFT JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = u.id
    WHERE u.id = 29622 AND
        topics.archetype <> 'private_message' AND
        (("topics"."deleted_at" IS NULL AND tu.last_read_post_number < topics.highest_staff_post_number AND tu.notification_level >= 2) OR (1=0)) AND
        (topics.visible OR u.admin OR u.moderator) AND
        topics.deleted_at IS NULL AND
        ( NOT c.read_restricted OR u.admin OR category_id IN (
            SELECT c2.id FROM categories c2
            JOIN category_groups cg ON cg.category_id = c2.id
            JOIN group_users gu ON gu.user_id = 29622 AND cg.group_id = gu.group_id
            WHERE c2.read_restricted )
        )
        AND NOT EXISTS( SELECT 1 FROM category_users cu
                        WHERE last_read_post_number IS NULL AND
                            cu.user_id = 29622 AND
                            cu.category_id = topics.category_id AND
                            cu.notification_level = 0)

;

```

---

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