Skip to content

unclecatvn/agent-skills / skills/odoo-19.0

odoo-19

Verified

Odoo 19 development knowledge base with 18 specialized guides covering Actions (ir.actions.*, cron jobs, server actions), Controllers (HTTP routing, endpoints, auth types), Data files (XML/CSV records, shortcuts, noupdate), API Decorators (@api.depends, @api.constrains, @api.ondelete, @api.onchange, @api.model, @api.private), SQL Constraints (models.Constraint replacing _sql_constraints), Database Indexes (models.Index), Module development (manifest, wizards, reports), Field types (Char, Text, Monetary, relational fields), Manifest configuration (__manifest__.py, dependencies, asset bundles), Mixins (mail.thread, mail.activity.mixin, mail.alias.mixin, utm.mixin), ORM Model methods (search, CRUD, domain filters, recordsets, CamelCase model naming), Migration scripts (pre/post/end hooks, data migration), OWL frontend components (hooks, services, lifecycle), Performance optimization (N+1 prevention, batch ops, _read_group), QWeb Reports (PDF/HTML, paper formats, barcodes, t-out), Security/ACL (record rules, field permissions, privilege-based groups, @api.private), Testing (TransactionCase, HttpCase, mocking, query count assertions), Transactions (savepoints, UniqueViolation, serialization failures), Translations (i18n, PO files, translatable fields), XML Views (list/form/search, kanban card templates, xpath inheritance, QWeb templates). Use when writing, reviewing, or debugging any Odoo 19 Python or XML code, creating or modifying modules, fixing performance issues, or looking up Odoo 19 API patterns and best practices. Includes CSS/SCSS asset authoring and review for Odoo addons.

references/odoo-19-performance-guide.md

View on GitHub
# Odoo 19 Performance Guide

## Coding Conventions

Prefer batch-aware ORM operations; do not commit Odoo's ambient transaction.

Guide for optimizing Odoo 19 code: preventing N+1 queries, reducing database queries, and using profiler.

## Table of Contents

- [Profiling](#profiling)
- [Batch Operations](#batch-operations)
- [Algorithmic Complexity](#algorithmic-complexity)
- [Indexes](#indexes)
- [Performance Pitfalls](#performance-pitfalls)

---

## Profiling

Odoo provides an integrated profiling tool to record SQL queries and stack traces.

### Enable from User Interface

1. Enable developer mode
2. Toggle **Enable profiling** button
3. Choose expiry time
4. Toggle **Enable profiling** again to start session profiling

Options:

- **Record sql** - Saves all SQL queries with stack trace
- **Record traces** - Saves stack trace periodically (default: 10ms interval)

### Enable from Python Code

```python
from odoo.tools.profiler import Profiler

# Basic profiling
with Profiler():
    do_stuff()

# With custom collectors
with Profiler(collectors=['sql', PeriodicCollector(interval=0.1)]):
    do_stuff()

# In tests
with self.profile():
    do_stuff()
```

### Collectors

| Collector          | Key            | Description                                      |
| ------------------ | -------------- | ------------------------------------------------ |
| SQL collector      | `sql`          | Saves SQL queries with stack trace               |
| Periodic collector | `traces_async` | Saves stack trace periodically (separate thread) |
| QWeb collector     | `qweb`         | Saves QWeb directive execution                   |
| Sync collector     | `traces_sync`  | Saves every function call/return (high overhead) |

### Execution Context

Add context to identify calls in speedscope:

```python
for index in range(max_index):
    with ExecutionContext(current_index=index):
        do_stuff()
```

### Performance Pitfalls

- Randomness can lead to different results (garbage collector, etc.)
- Blocking calls may cause unexpected long frames
- Cache state affects results (view/assets in cache)
- Profiler overhead can impact performance (especially SQL collector)
- Large profiles may cause memory issues

---

## Batch Operations

### Avoid Loop Queries

**BAD**: Search in loop (N queries)

```python
def _compute_count(self):
    for record in self:
        domain = [('related_id', '=', record.id)]
        record.count = other_model.search_count(domain)
```

**GOOD**: Use `_read_group` (1 query)

> **Odoo 19**: `read_group()` is **deprecated**. Use `_read_group()` (internal) or `formatted_read_group()` (public API).

```python
def _compute_count(self):
    domain = [('related_id', 'in', self.ids)]
    counts_data = other_model._read_group(domain, ['related_id'], ['__count'])
    mapped_data = {
        related.id: count
        for related, count in counts_data
        if related
    }
    for record in self:
        record.count = mapped_data.get(record.id, 0)
```

### Batch Creates

**BAD**: Create in loop

```python
for name in ['foo', 'bar']:
    model.create({'name': name})
```

**GOOD**: Batch create

```python
create_values = [{'name': name} for name in ['foo', 'bar']]
records = model.create(create_values)
```

### Prefetch Records

**BAD**: Browse one at a time

```python
for record_id in record_ids:
    record = model.browse(record_id)
    record.foo  # One query per record
```

**GOOD**: Browse all together

```python
records = model.browse(record_ids)
for record in records:
    record.foo  # One query for entire recordset
```

### Disable Prefetching (when needed)

```python
for values in values_list:
    message = self.browse(values['id']).with_prefetch(self.ids)
```

---

## Algorithmic Complexity

### Reduce Nested Loops

**BAD**: O(n²) complexity

```python
for record in self:
    for result in results:
        if result['id'] == record.id:
            record.foo = result['foo']
            break
```

**GOOD**: Use dictionary (O(n))

```python
mapped_result = {result['id']: result['foo'] for result in results}
for record in self:
    record.foo = mapped_result.get(record.id)
```

### Use Set Operations

**BAD**: List-like `in` check (quadratic)

```python
invalid_ids = self.search(domain).ids
for record in self:
    if record.id in invalid_ids:  # O(n) for each record
        ...
```

**GOOD**: Use set (O(n) total)

```python
invalid_ids = set(self.search(domain).ids)
for record in self:
    if record.id in invalid_ids:
        ...
```

**ALTERNATIVE**: Recordset operations

```python
invalid_ids = self.search(domain)
for record in self - invalid_ids:
    ...
```

---

## Indexes

Database indexes speed up search operations.

```python
name = fields.Char(string="Name", index=True)
```

**Warning**: Don't index every field - indexes consume space and impact INSERT/UPDATE/DELETE performance.

### Using Indexes

```python
# Field-level index
name = fields.Char(index=True)
records = self.search([('name', '=', 'value')])  # Uses index scan
```

### Declarative Index (Odoo 19)

For composite indexes, use `models.Index` as a model attribute:

```python
class MyModel(models.Model):
    _name = 'my.model'

    name = fields.Char()
    code = fields.Char()

    _name_code_idx = models.Index('(name, code)')
```

---

## Performance Pitfalls

### N+1 Query Problem

Occurs when you:

1. Fetch a list of records
2. Loop through them
3. Execute a query for each record

**Detection**: Use `--log-sql` CLI parameter or profiler

**Solution**: Fetch related data in one query using:

- `_read_group()`
- `search_fetch()`
- `fetch()`
- `mapped()`

### Large Recordsets

Processing large recordsets can cause memory issues.

**Solution**: Process in batches

```python
def _process_large_dataset(self):
    limit = 1000
    offset = 0
    while True:
        records = self.search([], limit=limit, offset=offset)
        if not records:
            break
        records.process()
        offset += limit
```

### Computed Field Dependencies

Missing dependencies cause recomputation at wrong time.

**BAD**: Missing dotted dependency

```python
@api.depends('partner_id')
def _compute_email(self):
    for record in self:
        record.email = record.partner_id.email  # N queries!
```

**GOOD**: Include dotted path

```python
@api.depends('partner_id.email')
def _compute_email(self):
    for record in self:
        record.email = record.partner_id.email  # 1 query for all
```

### Context Pollution

Excessive context changes can cause issues.

```python
# BAD: Too many with_context calls
records.with_context(lang='fr').with_context(active_test=False).with_context(...)
```

**Solution**: Consolidate context changes

```python
# GOOD: Single with_context call
records.with_context(lang='fr', active_test=False)
```

### Unnecessary invalidate_cache

Calling `invalidate_cache()` too frequently defeats the purpose of caching.

**Solution**: Only invalidate fields that actually changed

```python
# GOOD: Invalidate only changed fields
records.invalidate_recordset(['field1', 'field2'])
```

---

## Query Count Testing

Use `assertQueryCount` in tests to establish query limits.

```python
with self.assertQueryCount(11):
    do_something()
```

Combine with profiler for analysis:

```python
with self.profile():
    with self.assertQueryCount(__system__=1211):
        do_stuff()
```

---

## Good Practices Summary

| Practice                   | Description                                   |
| -------------------------- | --------------------------------------------- |
| **Batch operations**       | Accumulate operations, execute in batch       |
| **Use \_read_group**       | Replace search/search_count in loops          |
| **Prefetch records**       | Browse all records together                   |
| **Reduce complexity**      | Use dictionaries/sets instead of nested loops |
| **Add indexes**            | On frequently searched fields                 |
| **Use fetch/search_fetch** | For targeted data loading                     |
| **Profile first**          | Use profiler before optimizing                |
| **Test query counts**      | Use assertQueryCount in tests                 |

---

## References

- Source: Odoo 19 documentation `/doc/developer/reference/backend/performance.rst`
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall