The prefetching pattern for recordsets
When you access data from a recordset, it makes a query in the database. If you have a recordset with multiple records, fetching records on it can make a system slow because of the multiple SQL queries. In this recipe, we will explore how you can use the prefetching pattern to solve this issue. By following the prefetching pattern, you can reduce the number of queries needed, which will improve performance and make your system faster.
How to do it…
Take a look at the following code; it is a normal compute
method. In this method, self
is a recordset of multiple records. When you iterate directly on the recordset, prefetching works perfectly:
# Correct prefetching def compute_method(self): for rec in self: print(rec.name)
However, in some cases, prefetching becomes more complex, such as when fetching data with the browse
method. In the following example, we...