Enable Dark Mode!
By: Ayana KP

What is Prefetch Patterns in Odoo 16

Technical Odoo 16

Odoo has been a game-changer in enterprise resource planning (ERP) and business management software. With each new release, Odoo continues to evolve and provide better solutions for businesses of all sizes. In Odoo 16, one of the most exciting features is the introduction of Prefetch Patterns. These patterns can significantly enhance the performance of your Odoo system. In this blog, we will dive into what Prefetch Patterns are. 

Understanding Prefetch Patterns

Prefetch Patterns in Odoo 16 is a mechanism that allows developers to define and optimize data loading in Odoo models. This feature helps reduce the number of database queries and minimizes the loading of unnecessary data. By specifying which fields to prefetch and when to load them, Odoo can minimize the overhead on the database and make your application more responsive.

Why Prefetch Patterns Matter

Prefetch Patterns can significantly enhance the performance of your code by fetching related data in advance. This proactive approach reduces the need for repetitive database queries, which can be resource-intensive and lead to system slowdowns. The result? Faster response times and smoother interactions within the system, ultimately delivering a superior user experience. As your business expands, your Odoo system must efficiently handle growing volumes of data. Prefetch Patterns play a crucial role in ensuring your system maintains optimal performance even when dealing with extensive datasets. This not only minimizes unnecessary database queries but also conserves valuable server resources, potentially resulting in cost savings and more efficient resource utilization.

Best Practices for Using Prefetch Patterns

1. Prioritize Fields: Only prefetch fields that are essential for the current operation. Prefetching too many fields can negate the benefits and lead to unnecessary overhead.

Example : 
class SaleOrder(models.Model):
    _inherit = 'sale.order'
    def compute_total_amount(self):
        # Fetch the name and order lines using Prefetch Patterns
        self.with_prefetch(['partner_id.name', 'order_line.product_id.name']).compute_amount()

2. Use Patterns Sparingly: While Prefetch Patterns are powerful, they should be used strategically. Overusing them can lead to increased memory consumption.
Prefetching works perfectly when you specifically focus on the recordset.

# Correct prefetching
 def compute_method(self):
     for rec in self:

3. Test Thoroughly: Before deploying Prefetch Patterns in a production environment, thoroughly test their impact on performance and resource usage.
Example : 
Rather than giving one ID at a time to the Browse method, you can collect all the IDs in one place and use them all at once. This way, you can work with the recordset and still keep the benefit of prefetching. Each piece of information is gathered through separate SQL queries.

def some_action(self):
   record_ids = []
   self.env.cr.execute("some query to fetch record id")
   record_ids = [rec[0] for rec in self.env.cr.fetchall()]
   recordset = self.env['purchase.order'].browse(record_ids)
   for record in recordset:

Understand Query Optimization: Gain a deep understanding of how Prefetch Patterns affect the underlying database queries. When you break a recordset into smaller pieces, the ORM makes a new recordset with fresh prefetch settings. If you want to get details for each piece after breaking them apart, you can do that by giving the prefetch record ID to the 'with_prefetch()' method. In the related model, it divides the recordset into two parts. We provided regular prefetch settings for both sets. So when you access information from one part, the ORM also gets information from the other part and stores it temporarily.

self.env.cr.execute("select id from sale_order limit 10")
record_ids = [rec[0] for rec in self.env.cr.fetchall()]
recordset = self.env['sale.order'].browse(record_ids)
recordset1 = recordset[:10]
for rec in recordset1:
   # Prefetch name of all 10 records in the first loop
   # Prefetch attention of all 10 records in the first loop
recordset2 = recordset[10:].with_prefetch(recordset._ids)
for rec in recordset1:
   # Prefetch name of all 10 records in the first loop
   # Prefetch attention of all 10 records in the first loop  

In the example, recordset1 only gets details for five records, while recordset2 gets details for all records. Prefetch settings aren't just about splitting recordsets. You can also apply the 'with_prefetch()' method to keep the same prefetch settings for multiple recordsets. This way, when you access data from one record, the same data is fetched for all the other recordsets.
Prefetch Patterns in Odoo 16 offer a powerful way to boost performance and responsiveness. By prefetching data in advance and minimizing unnecessary database queries, you can provide a better user experience, improve scalability, and make more efficient use of your server resources. When used wisely and sparingly, Prefetch Patterns can be a game-changer for your Odoo 16 instance, ensuring that it runs smoothly and efficiently, even as your business grows.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message