Moien U Deen Let's talk

How I made a slow Odoo report 52 times faster

A branch sales report at Zero Healthcare took 261 seconds for one month and timed out for a quarter. After a rewrite it runs in about 5 seconds with the same numbers. Here is the method, step by step.

1. Measure before you change anything

Turn on developer mode and run the report with the --log-level=debug_sql option on a copy of the database, or use Odoo's profiler. Count the queries. In this case one month of data produced around 44,000 database queries. Slow Odoo reports are rarely slow because of one heavy query; they are slow because of thousands of small ones.

2. Find the N+1 pattern

The usual cause is a loop that reads related data one record at a time:

for order in orders:
    for line in order.lines:
        total += line.price_subtotal      # fine, prefetched
        payments = env['pos.payment'].search([('pos_order_id', '=', order.id)])  # one query per order

Any search, search_count or browse inside a loop multiplies queries by the number of records.

3. Replace loops with grouped and bulk reads

Ask the database for totals instead of computing them in Python. read_group (or _read_group in recent versions) returns sums grouped by branch, day or payment method in one query:

rows = env['pos.payment']._read_group(
    [('payment_date', '>=', date_from), ('payment_date', '<', date_to)],
    groupby=['session_id', 'payment_method_id'],
    aggregates=['amount:sum'],
)

Where you need records, fetch them all at once with one search and build a dictionary keyed by id, then look values up in the loop.

4. Add the right index

If the report filters a large table by date or state, check that the field is indexed. In a custom module, index=True on the field definition creates it. Only index fields that the slow query actually filters on.

5. Prove the output is identical

Speed is worthless if the numbers change. Run the old and new report for the same periods, export both, and compare totals line by line. Only release when every figure matches. For Zero Healthcare the rewritten report was compared over multiple months and branches before it replaced the original.

6. Release safely

Install the new version on a full copy of live, keep the old report available for a short overlap period, and release with a rollback plan.

Have a slow Odoo report? Send me the details on WhatsApp.

Need help with this in your Odoo?

Tell me how your business runs today. I start with requirements before suggesting any setup.

Message me on WhatsApp