How to avoid ordering by in django queryset, order_by() not working

Viewed 288

I have a "big" db whith over 60M records, and I'm trying to paginate by 50. I have another db whith ~8M records and it works perfectly, but with the 60M amount it just never loads and overflows the db. I found that the problem was the order_by(id) made by django so I tried using a mysql view already ordered by id, but then django tries to order it again. To avoid this, I used order_by(), which is supposed to avoid any ordering, but it still does it.

    def get_queryset(self, request):
        qs = super(CropAdmin, self).get_queryset(request)

        qs1 = qs.only('id', 'grain__id', 'scan__id', 'scan__acquisition__id',
                      'validated', 'area', 'crop_date', 'matched_label', 'grain__grain_number', 'filename').order_by()

        if request.user.is_superuser:
            return qs1

The query made is still using order_by:

SELECT `crops_ordered`.`crop_id`,
       `crops_ordered`.`crop_date`,
       `crops_ordered`.`area`,
       `crops_ordered`.`matched_label`,
       `crops_ordered`.`validated`,
       `crops_ordered`.`scan_id`,
       `crops_ordered`.`grain_id`,
       `crops_ordered`.`filename`,
       `scans`.`scan_id`,
       `scans`.`acquisition_id`,
       `acquisitions`.`acquisition_id`,
       `grains`.`grain_id`,
       `grains`.`grain_number`
  FROM `crops_ordered`
 INNER JOIN `scans`
    ON (`crops_ordered`.`scan_id` = `scans`.`scan_id`)
 INNER JOIN `acquisitions`
    ON (`scans`.`acquisition_id` = `acquisitions`.`acquisition_id`)
 INNER JOIN `grains`
    ON (`crops_ordered`.`grain_id` = `grains`.`grain_id`)
 **ORDER BY `crops_ordered`.`crop_id` DESC**
 LIMIT 50

Any idea on how to fix this? Or a better way to work with a db of this size?

1 Answers

I don't believe order_by() will work as there will most likely be a default parameter when Django implemented this function. Having said that, I believe this thread has the answer that you want.

Edit

The link in that thread might provide too much information at once, although there aren't many details on this either. If you don't like Github, there's also an official documentation page on this method but you'll have to manually look for clear_ordering by using CTRL + f or any equivalence.

Related