How to improve the speed?

Software Performance directly affects User Experience

Introduction

This blog is regarding improving the speed of software. Good speed is essential for software because it directly affects the user experience. when software performs quickly and efficiently, it leads to a more positive user experience, increased productivity, and higher user satisfaction. In addition to the user experience, good speed can have a significant impact on the overall success of software.


These are some points that should be kept in mind to improve speed while developing odoo modules.


Use compute instead of onchange


The use of the compute attribute is generally considered better than using onchange methods in Odoo for several reasons:

- Automatic Triggering: The compute attribute automatically triggers the computation of the field whenever its dependent fields change. This eliminates the need for explicit triggering or handling, reducing the chances of missing updates or inconsistencies.

- Consistency: With compute, the field calculation logic is centralized within the model, ensuring consistent computation across all operations and contexts. On the other hand, onchange methods need to be explicitly called or triggered, which can lead to inconsistencies if not properly handled.


Example
total_amount = fields.Float(string='Total Amount', compute='_compute_total_amount')
@api.depends('order_lines.price_subtotal')
 def _compute_total_amount(self):
    for order in self:
        order.total_amount = sum(order.order_lines.mapped('price_subtotal'))


Use search_count instead of search


search_count is preferred over search when you only need the count of matching records without retrieving the record details. It offers improved performance, resource optimization, simplified code, and better scalability in such scenarios.


Example

get number of sale orders records having total_amount > 1000 using search_count
for obj in self:
    domain = [('total_amount', '>', 1000)]
    obj.order_count = self.env['sale.order'].search_count(domain)


Use read_group instead of search


The "read_group" method can offer better performance than the "search" method for certain types of aggregation operations in Odoo. This is because "read_group" is optimized for performing grouped queries and aggregations directly at the database level, while "search" has a broader range of functionalities and may involve additional processing steps.

 Here are a few reasons why "read_group" can be faster than "search" in Odoo:

- Database-level optimizations: The "read_group" method leverages the underlying database engine's capabilities to optimize grouped queries. Databases are often highly optimized for efficient data retrieval and aggregation, which can result in faster execution times compared to complex search operations.

- Reduced data transfer: When using "read_group," you can specify only the fields needed for the aggregation, reducing the amount of data that needs to be transferred from the database to the application. This can significantly improve performance, especially when dealing with large datasets.

for more info please refer: https://www.odoo.com/documentation/16.0/developer/reference/backend/orm.html#odoo.models.Model.read_group


Use SQL queries


In some cases, it may be more efficient to use SQL queries directly in Odoo rather than relying solely on ORM methods such as search, write, and delete, especially when dealing with large databases. SQL queries can allow for more fine-grained control over data retrieval and modification, which can be beneficial for complex or high-volume operations.

Here are some situations where using SQL queries may be appropriate:

- Complex queries: If the query you need to perform is too complex or involves multiple tables, using SQL queries may be more efficient than using ORM methods. SQL offers a more powerful and flexible syntax for querying and manipulating data, allowing you to perform more complex operations.

- High-volume operations: If you need to perform a large number of operations on a large dataset, using SQL queries can be more efficient than using ORM methods. SQL can allow you to process large amounts of data quickly and efficiently, minimizing the time needed to complete the operation.

- Performance optimization: If you need to optimize the performance of your queries, using SQL queries can be a good option. SQL can allow you to fine-tune your queries and take advantage of database-specific optimizations, resulting in faster query execution times.

- Bulk data import/export: If you need to import or export large amounts of data, using SQL queries can be more efficient than using ORM methods. SQL can allow you to perform bulk data operations, minimizing the time and resources needed to transfer large amounts of data.


How to improve the speed?
BizzAppDev Expert May 19, 2023
Share this post
Sign in to leave a comment
Unlock New Opportunities at Odoo Community Days India 2024
Join Us for Two Days of Innovation and Networking!