# Einige Abfragen, die nicht abgeschlossen werden

**URL:** <https://meta.discourse.org/t/some-queries-that-dont-finish/113560>\
**Category:** Support\
**Created:** [5. April 2019 um 15:19 UTC](https://meta.discourse.org/t/some-queries-that-dont-finish/113560 "2019-04-05T15:19:00Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)\
**Post date:** [5. April 2019 um 17:23 UTC](https://meta.discourse.org/t/some-queries-that-dont-finish/113560/2 "2019-04-05T17:23:03Z")

</div>

The problem is slowly getting worse - the original queries are still running and almost exactly 2 hours later, 3 more started. They’re now occupying 2/12 of my cores. If this continues overnight it _will_ turn into an actual problem.

**Update** : this query was introduced by [this recent commit](https://github.com/discourse/discourse/commit/aa2311a7b00c8f0d298387f321a8c2005d169b34#diff-2a4f5dac0a4f596882e0a68bca94dc43). Perhaps it’s missing an index on some tables? I have 4.1M records in the posts and post\_search\_data tables and 1.5M in the topics table, so the double LEFT JOIN could be a problem if something is not indexed correctly.

Not sure if this helps, but here’s an EXPLAIN of the query on my DB:

```plaintext
sudo -u postgres psql discourse -c "EXPLAIN SELECT posts.id FROM posts LEFT JOIN post_search_data pd ON pd.locale='en' AND pd.version=2 AND pd.post_id=posts.id LEFT JOIN topics ON topics.id=posts.topic_id WHERE pd.post_id IS NULL AND topics.id IS NOT NULL AND topics.deleted_at IS NULL AND posts.raw!='' ORDER BY posts.id DESC LIMIT 20000;";
                                                          QUERY PLAN                                                           
-------------------------------------------------------------------------------------------------------------------------------
 Limit (cost=2000.88..124179.27 rows=20000 width=4)
   -> Nested Loop Anti Join (cost=2000.88..29146177.32 rows=4770758 width=4)
         Join Filter: (pd.post_id = posts.id)
         -> Gather Merge (cost=1000.88..21351547.22 rows=4770860 width=4)
               Workers Planned: 2
               -> Nested Loop (cost=0.86..20799871.57 rows=1987858 width=4)
                     -> Parallel Index Scan Backward using posts_pkey on posts (cost=0.43..12078793.28 rows=1988522 width=8)
                           Filter: (raw <> ''::text)
                     -> Index Only Scan using index_topics_on_id_and_deleted_at on topics (cost=0.43..4.39 rows=1 width=4)
                           Index Cond: ((id = posts.topic_id) AND (id IS NOT NULL) AND (deleted_at IS NULL))
         -> Materialize (cost=1000.00..495214.55 rows=102 width=4)
               -> Gather (cost=1000.00..495214.04 rows=102 width=4)
                     Workers Planned: 2
                     -> Parallel Seq Scan on post_search_data pd (cost=0.00..494203.84 rows=42 width=4)
                           Filter: (((locale)::text = 'en'::text) AND (version = 2))

```

---

_[View the full topic](https://meta.discourse.org/t/some-queries-that-dont-finish/113560)._
