# Some queries that don't finish

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

<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:** [April 5, 2019, 6:31pm UTC](https://meta.discourse.org/t/some-queries-that-dont-finish/113560/9 "2019-04-05T18:31:27Z")

</div>

Great, that finished very fast:

```plaintext
sudo -u postgres psql discourse -c "EXPLAIN ANALYZE 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 200;";
                                                                             QUERY PLAN                                                                             
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit (cost=1.29..2470.34 rows=200 width=4) (actual time=2.318..46.698 rows=200 loops=1)
   -> Nested Loop Anti Join (cost=1.29..58896117.57 rows=4770758 width=4) (actual time=2.317..46.664 rows=200 loops=1)
         -> Nested Loop (cost=0.86..33044192.61 rows=4770860 width=4) (actual time=0.132..39.384 rows=2238 loops=1)
               -> Index Scan Backward using posts_pkey on posts (cost=0.43..12113605.04 rows=4772452 width=8) (actual time=0.075..27.719 rows=2239 loops=1)
                     Filter: (raw <> ''::text)
                     Rows Removed by Filter: 9
               -> Index Only Scan using index_topics_on_id_and_deleted_at on topics (cost=0.43..4.39 rows=1 width=4) (actual time=0.005..0.005 rows=1 loops=2239)
                     Index Cond: ((id = posts.topic_id) AND (id IS NOT NULL) AND (deleted_at IS NULL))
                     Heap Fetches: 2106
         -> Index Scan using posts_search_pkey on post_search_data pd (cost=0.43..5.42 rows=1 width=4) (actual time=0.003..0.003 rows=1 loops=2238)
               Index Cond: (post_id = posts.id)
               Filter: (((locale)::text = 'en'::text) AND (version = 2))
               Rows Removed by Filter: 0
 Planning time: 1.923 ms
 Execution time: 46.760 ms
(15 rows)

```

---

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