1

I'm currently using the Barcode module in Odoo 14 to scan the barcode of the products. However, every time I click on an order, the module loads extremely slowly.

I figured out that the get_all_products_by_barcode method caused all of this. My database has 64000 products, and that method alone took almost 50 seconds to query and return ALL products back to the client every single time.

Does anyone have a solution for this? Thank you.

P/s: This is the default method to get all product barcodes from Odoo

    @api.model
    def get_all_products_by_barcode(self):
        # This search_read method is really slow
        products = self.env['product.product'].search_read(
            [('barcode', '!=', None), ('type', '!=', 'service')],
            ['barcode', 'display_name', 'uom_id', 'tracking']
        )
        packagings = self.env['product.packaging'].search_read(
            [('barcode', '!=', None), ('product_id', '!=', None)],
            ['barcode', 'product_id', 'qty']
        )

        # Some other codes below here
Nam Pham
  • 11
  • 3
  • You should share your code in your question so people can help you out. – Rolv Apneseth Jun 22 '21 at 15:13
  • Add an index to the barcode column in the products table (or set `index=True` on the model field). – snakecharmerb Jun 22 '21 at 17:29
  • I just shared the code that took the majority of the running time – Nam Pham Jun 23 '21 at 04:02
  • @snakecharmerb an index will not help a lot in this case i think. Even if we removed the where clause this would be a slow request. To make it faster you would need, in addition to the index, to load the product after scanning from the database and not all of the products when opening the order. But in this case you are making the scanning itself a bit slower. – Kerrim Jun 25 '21 at 07:39

1 Answers1

0
    products_qry = """SELECT pp.id, pp.barcode, pt.name, pt.uom_id, pt.tracking
                    FROM product_product pp
                    JOIN product_template pt ON pt.id = pp.product_tmpl_id
                    WHERE pp.barcode <> '' and pt.type <> 'service'"""
    self.env.cr.execute(products_qry)
    products = self.env.cr.dictfetchall()

    """ 
    products = self.env['product.product'].search_read(
        [('barcode', '!=', None), ('type', '!=', 'service')],
        ['barcode', 'display_name', 'uom_id', 'tracking']
    )
    """
    packagings_qry = """SELECT pp.barcode, pp.product_id, pp.qty
                    FROM product_packaging as pp
                    WHERE pp.barcode <> '' and pp.product_id <> NULL
                    """
    self.env.cr.execute(packagings_qry)
    packagings = self.env.cr.dictfetchall()