[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#44782

X-original-commit: 6c21ea369dc247b3ab31b767315c48ffc8d40a3d
Signed-off-by: Olivier Dony (odo) <odo@openerp.com>
This commit is contained in:
Pierre Masereel
2020-02-06 17:26:45 +00:00
committed by Olivier Dony
parent a4d46ec55c
commit d8cde5969f
2 changed files with 9 additions and 3 deletions
+8 -2
View File
@@ -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
+1 -1
View File
@@ -17,7 +17,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.')
multiprint_resume = fields.Char()