Odoo Tricks: Speed Up Default Tree View

Indrajaya
3 min readDec 29, 2021
Photo by Oscar Sutton on Unsplash

Background

Normally, Odoo takes care of everything to make it seem easy and simple for both developers and users.

Over the time, the data getting bigger so it takes so much time to show the table after click the menu. After showing the table, user can filter to show specific data. Unfortunately, the user is already frustrated before being able to perform the filter.

Adding the index will help when user perform the filter, otherwise nothing changed.

To make everything simple, this article will focus on how to improve the performance of stock move menu in Odoo 11.

Analysis

When user click a menu, browser will call exposed service /web/dataset/search_read via json-rpc in the backend. This happen to most of the menu, except dashboard or setting menu. This behavior assume that menu has no domain to filter by default.

This url path (/web/dataset/search_read) is provided in web addons (check addons/web/controllers/main.py). The method for the exposed service then calls do_search_read.

In method do_search_read perform 2 main operations, search and count the data.

In the search operation, if the sort parameter is empty or false in the search_read or search method, Odoo will use default _order attribute. Normally, define a new model can be written as follow.

from odoo import models
class StockMove(models.Model):
_name = 'stock.move'
_order = 'date'
def method_for_operation(self):
pass

When _order attribute is not define, Odoo will get id as it’s value. When data become very large, _order attribute can introduce an issue because it force Odoo to add order by before the query send to Postgresql. To avoid this, the _order attribute can be set as false.

The count operation in do_search_read is counting amount of data. In the large data, it also cause a slow operation. Laurenz Albe explain very well about this¹. When user click a menu, they should not care about exact count. The estimation count can be provide from Postgresql query planning that execute it very fast.

By using this technique, when the user clicks on the menu, the default tree view takes place very quickly.

Solution: Develop the patch

The first need to do is clone the sample code² :

git clone https://github.com/jay009id/speedup_odoo.git

Two things need to be concern are patching the service of search_read (controllers/main.py) and sort (models/stock_move.py) process.

In file controllers/main.py (see below), the estimation count can be provide at line 27–30.

To avoid a sort process, the _order attribute can be set as false.

Install the module/addons, then user will get a “wow” moment when access a very large data, especially a stock move. This technique can be apply to other menu with a small modification.

Conclusion

The technique that explain in this article is just one part / idea of manything that need to be investigate when Odoo run very slow in large data. Enjoy the journey.

Reference

  1. https://www.cybertec-postgresql.com/en/postgresql-count-made-fast/
  2. https://github.com/jay009id/speedup_odoo

--

--