title: Django ORM join on single table
body: <p>I have SQL/ORM related question, there is one model NewsArticle with ~500k objects - querying on it causes some performance issues.
  Model more or less looks like this:</p>
  
  <pre><code>Class NewsArticle(models.Model):
      date_published = models.DateTimeField()
      duplicates_group_id = models.UUIDField(blank=False, null=True)
      duplicates = models.ManyToManyField('self', blank=True)
      topic = models.ForeignKey('NewsTopic', on_delete=models.SET_NULL, null=True)
      online = models.BooleanField(default=True)
      ...
  </code></pre>
  
  <p>What I need is to group by duplicates_group_id and iterate only on articles with lowest id from every matching duplcates_group.
  In SQL following query is quite fast(uses proper db indexes):</p>
  
  <pre><code>SELECT 
      l.`*` # fields dont matter now
  FROM `summarizer_newsarticle` l
      INNER JOIN
          (SELECT MIN(id) AS first_article_id
           FROM `summarizer_newsarticle`
           WHERE date_published &gt;= '2019-12-02 00:00:00'
           GROUP BY duplicates_group_id) r 
      ON l.id = r.first_article_id
  WHERE
      l.`online` = 1 AND l.`topic_id` = 5
  </code></pre>
  
  <p>In View for displaying those articles in get_queryset method I struggle to prepare proper query.
  Also raw SQL didnt solve the issue because <strong>get_queryset</strong> expects returning queryset object. 
  Without sufficient SQL queries performance is really bad
  Do you maybe have an idea if its even possible to reproduce such query or to do it different way?
  Thank you</p>