Skip to content
Snippets Groups Projects
Commit 6c21ea36 authored by Pierre Masereel's avatar Pierre Masereel Committed by Olivier Dony
Browse files

[FIX] pos_restaurant: improve performance retrieving table orders


Since synchronization of tables was added in POS restaurant,
requests are frequently made to the server to display the
number of open orders on each table.

The implementation of the counting caused O(num_tables) queries,
and those queries were slow on large databases because there
was no index for the `table_id` column.

This patch fixes both problems, and the counting now uses
3 fast queries regardless of the number of tables.

On a sample database with 2k tables and 50k POS orders,
calls to `get_tables_order_count` went down
from 1 s and 100+ SQL queries to 10 ms and 4 queries.

About 2 orders of magnitude faster, with server load drastically
reduced as well.

closes odoo/odoo#44660

Signed-off-by: default avatarOlivier Dony (odo) <odo@openerp.com>
parent d7f2beac
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,13 @@ class PosConfig(models.Model):
def get_tables_order_count(self):
""" """
self.ensure_one()
tables = self.env['restaurant.table'].search([('floor_id.pos_config_id', 'in', self.ids)])
domain = [('state', '=', 'draft'), ('table_id', 'in', tables.ids)]
order_stats = self.env['pos.order'].read_group(domain, ['table_id'], 'table_id')
orders_map = dict((s['table_id'][0], s['table_id_count']) for s in order_stats)
result = []
for table in self.floor_ids.table_ids.filtered(lambda t: t.active == True):
result.append({'id': table.id, 'orders': self.env['pos.order'].search_count([('state', '=', 'draft'), ('table_id', '=', table.id)])})
for table in tables:
result.append({'id': table.id, 'orders': orders_map.get(table.id, 0)})
return result
......@@ -16,7 +16,7 @@ class PosOrderLine(models.Model):
class PosOrder(models.Model):
_inherit = 'pos.order'
table_id = fields.Many2one('restaurant.table', string='Table', help='The table where this order was served')
table_id = fields.Many2one('restaurant.table', string='Table', help='The table where this order was served', index=True)
customer_count = fields.Integer(string='Guests', help='The amount of customers that have been served by this order.')
def _get_pack_lot_lines(self, order_lines):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment