On large recordsets I got some severe performance issues while browsing through an inherits relation.
For example I have a batch process exporting a csv file with products variants:

I have to export 50.000 products.

If I do :
`variant.categ_id` It takes a very long time to get the result.

If I do:
`variant.product_tmpl_id.categ_id` The result is immediate

The problem is that the global perfomance of the application is impacted by this problem.

For example my POS takes more than 15 minutes to load. Because I have 26.000 products and the compute price method uses a lot browsing through the inherits such like `product.cated_id`, `product.uom_id` and they all takes a huge time to compute.

Same issue in this case.

If I do :
`variant.price` It takes a very long time to get the result.

If I do:
`variant.product_tmpl_id.price` The result is immediate

Could you help me ?


Simple way to reproduce:

1/ Create a new database and install sale module
2/ Launch following script through odoo shell :

```python
# -*- coding: utf-8 -*-
import logging

_logger = logging.getLogger(__name__)
self = self  # noqa

nb_product = 50000

i = 0
while i < nb_product:
    prod = self.env['product.template'].create({'name': 'product_%s' % i})
    i += 1
    if i % 1000 == 0:
        self.env.cr.commit()
        _logger.info("%s Products Created" % i)
```

2/ Launch following script through odoo shell :
```python
# -*- coding: utf-8 -*-
import logging

_logger = logging.getLogger(__name__)
self = self  # noqa


products = self.env['product.product'].search([])

for product in products:
    # Immediate response
    _logger.info('Category Template %s' % product.product_tmpl_id.categ_id)
    # Lagging response
    _logger.info('Category Variant %s' % product.categ_id) 
    break
```
"""
        Add possibly missing JOIN to ``query`` and generate the ORDER BY clause for m2o fields,
        either native m2o fields or function/related fields that are stored, including
        intermediate JOINs for inheritance if required.
        :return: the qualified field name to use in an ORDER BY clause to sort by ``order_field``
        """
        field = self._fields[order_field]
        if field.inherited:
            
            qualified_field = self._inherits_join_calc(alias, order_field, query)
            alias, order_field = qualified_field.replace('"', '').split('.', 1)
            field = field.base_field
        assert field.type == 'many2one', 'Invalid field passed to _generate_m2o_order_by()'
        if not field.store:
            _logger.debug("Many2one function/related fields must be stored "
                          "to be used as ordering fields! Ignoring sorting for %s.%s",
                          self._name, order_field)
            return []
        
        dest_model = self.env[field.comodel_name]
        m2o_order = dest_model._order
        if not regex_order.match(m2o_order):
            
            m2o_order = dest_model._rec_name
        
        
        join = (alias, dest_model._table, order_field, 'id', order_field)
        dest_alias, _ = query.add_join(join, implicit=False, outer=True)
        return dest_model._generate_order_by_inner(dest_alias, m2o_order, query,
                                                   reverse_direction, seen)