Skip to content
Snippets Groups Projects
Commit 5a188992 authored by Aurelien van Delft (avd)'s avatar Aurelien van Delft (avd)
Browse files

[FIX] stock: enable index/bitmap scan in report_stock_quantity


The report_stock_quantity model defines a view in its init method
to compute quantity information related to stock. This view
is made of two CTEs and a three-part query separated by UNION ALL.
The first CTE, existing_sm, retrieves the stock_move data
from the database that are later used by the remaining of the query.

One of the where conditions of existing_sm is (m.state != 'done' or
m.date >= ((now() at time zone 'utc')::date - interval '3month')).
m.state != 'done' is translated to m.state <> 'done' by the query planner.
This type of operator has the side-effect of turning off index scan.
Therefore, the scanning of the existing_sm CTE performs a Seq Scan
and applies the where conditions in a Filter node. This can be quite
ineffecient if the stock_move table is big, and if the selectivity
of the m.state != 'done' condition is high enough to theoretically
justify an IndexScan.

To fix that, we take the inverse of m.state != 'done', i.e. an IN cond.
This allows postgres to use Bitmap Scan, which
is usually better under these specific conditions.

E.g. speedup: db with 7M stock_moves, select from report_stock_quantity
with conds for state, date, product_id, warehouse_id, company_id
4.5s -> 2.5s

opw-3288364

closes odoo/odoo#122911

X-original-commit: aad458e5
Signed-off-by: default avatarWilliam Henrotin (whe) <whe@odoo.com>
Signed-off-by: default avatarVan Delft Aurélien (avd) <avd@odoo.com>
parent 9ab317e8
Branches
Tags
No related merge requests found
......@@ -61,7 +61,7 @@ WITH
(whs.id IS NULL OR whd.id IS NULL OR whs.id != whd.id) AND
m.product_qty != 0 AND
m.state NOT IN ('draft', 'cancel') AND
(m.state != 'done' or m.date >= ((now() at time zone 'utc')::date - interval '3month'))
(m.state IN ('draft', 'waiting', 'confirmed', 'partially_available', 'assigned') or m.date >= ((now() at time zone 'utc')::date - interval '3month'))
),
all_sm (id, product_id, tmpl_id, product_qty, date, state, company_id, whs_id, whd_id) AS (
SELECT sm.id, sm.product_id, sm.tmpl_id,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment