宜兴通达竭诚为您服务。

通达科技 - 宜兴电脑维护及IT服务外包服务商

Prefetch Patterns
  • 上传者: Administrator
  • 上传时间:2024年02月24日 07时33分15秒
摘要:
Prefetch Patterns** When you access information from a record, the record is queried. If you have a data set with various data sets, specifying differ ......
正文 相关文章 请喝咖啡

    Prefetch Patterns

    When you access information from a record, the record is queried. If you have a data set with various data sets, specifying different SQL queries can slow down the framework when fetching the data sets. In this recipe, we'll look at how to solve this problem using a prefetching example. You can reduce the number of queries required by following a prefetch design. This makes the execution more streamlined and the framework faster.

    Examine the accompanying code. It is a representative processing technology. In this strategy, the self is a record with many records. Prefetching works perfectly at the point of highlighting the recordset directly.

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

    However, prefetching can be more complex, for example, when retrieving information using a browse strategy. In the accompanying model, the for loop searches through the records one by one. This doesn't use prefetching effectively and runs more queries than expected.

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

    Instead of passing individual IDs to the Browse method, you can get the IDs into a variable and pass them right away. Then you can perform operations on that recordset. With that in mind, you don't lose the prefetch element, and the information is captured in individual SQL queries.

    # Correct prefetching
    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:
       	print(record.name)

    Prefetching reduces the amount of SQL queries when working with different data sets. This is done by fetching each piece of information without delay. Prefetching generally works consistently in Odoo, but under certain conditions, this component is lost, for example, when splitting the dataset as shown in the attached model.

    records = [rec for rec in record_ids if rec.id not in [101, 102, 103, 104]]

    The above code does not take advantage of prefetching as it splits the recordset into parts.

    Accurate use of prefetching can contribute to the overall representation of object-relational mapping (ORM). You should look into how prefetching works in your engine. When you highlight a record in a for loop and access field values in the first iteration, prefetching retrieves information for the entire record instead of bringing information from the current record into the iteration. This is because once you reach a field within the for circle, you are likely to carry that information with you to the next record iteration. The main emphasis of the for loop, prefetching, fetches, and caches information for all records. The next iteration of the for loop will serve information from this cache instead of executing another SQL query. This reduces the number of queries from O(n) to O(1).

    We should assume that the recordset contains ten records. Information for each of the ten records is retrieved when the name field of the record is accessed in the first loop. This is not only true for name fields. It also shows each field of these ten records. The next iteration provides information from memory. This reduces the number of questions from 10 to 1.

    self.env.cr.execute("select id from purchase_order limit 10")
    record = self.env['purchase.order'].browse(record_ids)
    for rec in record:
       print(rec.name) # Prefetch name of all 10 records in the first loop
       print(rec.attention) # Prefetch attention of all 10 records in the first   loop

    Note that prefetching preserves the values of all fields (except *2many fields) regardless of whether those fields are used in this for loop idea. This is because, unlike the additional questions in each segment, the additional sections have a minor impact on performance.

    In some cases, prefetched fields can interfere with execution. In such cases, prefetching can be disabled by passing False to the prefetch_fields setting, such as recordset.with_context(prefetch_fields=False). The prefetch component uses the environment cache to save and restore record values. This means that once a record has been retrieved from the information base, all subsequent calls to fields from the environment cache will be processed. You can access the environment cache using the env.cache property. To invalidate the cache, you can use the invalidate_cache() strategy in your environment.

    When splitting a recordset, the ORM creates a new recordset with new prefetch settings. Executing a procedure on such a recordset prefetches information for individual records only. Assuming you need to prefetch each record after prefetching, you can do this by passing the prefetch record id to the with_prefetch() technique. In the companion model, it divides the recordset into two sections. Here, we passed typical prefetch settings for both datasets. So when you get information from one, the ORM will get information from the other and cache the information for some time.

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

    In the code above, Recordset1 prefetches only five records in the recordset, but Recordset2 prefetches all the records in the recordset. Prefetch settings are not limited to recordset detachment. You can also use the with_prefetch() strategy to have typical prefetch settings across different recordsets. Which means, when information is fetched from one record, information is also fetched for all remaining recordsets.

    本文章从网上收集,如有侵权请联系tderp@tderp.com删除
  • 微信扫一扫,一分也是爱:
  • 微信

服务原则及地区范围

宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。

我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!

通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。

宜兴地区提供上门服务:

  • 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
  • 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。
  • 联系电话:189-21-343434
  • 在线沟通: