0% found this document useful (0 votes)
21 views

Creating Partition Table in ODOO 17

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Creating Partition Table in ODOO 17

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Creating Partition Table in Odoo 17 for Query Performance Improvement on

Large Data

Partitioning tables in Odoo 17 is a crucial step for optimizing the performance when
dealing with large volumes of data. Partitioning allows for efficient data distribution
and faster queries, especially in systems with high transaction volumes.

Here is a comprehensive explanation of partition table design, how to implement it


for the sale.order table in Odoo, and the steps to ensure that data remains
accessible from the main table (sale.order) even after partitioning.
1. Partition Table Concept

A partition table is a database technique that divides a large table into smaller parts,
called partitions, based on certain rules such as date or another column. In this
scenario, we can partition the sale.order table by year, allowing each year to have its
own table for storing data for that specific period.
Types of Partitioning:

 Range Partition: Data is partitioned based on a range of values, such as


dates (monthly or yearly).
 List Partition: Data is partitioned based on discrete values in a particular
column.
 Hash Partition: Data is partitioned based on the result of a hash function
applied to a column.
 Composite Partition: A combination of multiple partitioning methods.

For Odoo, range partitioning based on the date_order column in the sale.order
table is highly suitable, as sales transactions are often grouped by time periods
(monthly, quarterly, or yearly).
Steps to Create Partition Table in PostgreSQL for Odoo 17

Step 1: Create a New Partitioned Table

The first step is to create a new table that partitions data based on the date_order
column. We will use the PARTITION BY RANGE clause, which allows us to divide
data based on a date range.

sql

CREATE TABLE sale_order_new (LIKE sale_order) PARTITION BY RANGE


(date_order);
In this step, the sale_order_new table is created as a copy of the sale_order table
with the same schema and is partitioned based on the date_order column.
Step 2: Add Indexes
To optimize query performance, we add several indexes on frequently queried
columns, such as date_order, partner_id, and amount_total.

sql
CREATE INDEX idx_sale_order_date_order ON sale_order (date_order);

CREATE INDEX idx_sale_order_date_delivery ON sale_order (commitment_date);

CREATE INDEX idx_sale_order_customer_id ON sale_order (partner_id);

CREATE INDEX idx_sale_order_company_id ON sale_order (company_id);

CREATE INDEX idx_sale_order_fiscal_position_id ON sale_order


(fiscal_position_id);

CREATE INDEX idx_sale_order_team_id ON sale_order (team_id);

CREATE INDEX idx_sale_order_journal_id ON sale_order (journal_id);

CREATE INDEX idx_sale_order_client_order_ref ON sale_order (client_order_ref);

CREATE INDEX idx_sale_order_amount_total ON sale_order (amount_total);


These indexes will speed up data access and searches on columns frequently used
in Odoo’s business processes.
Step 3: Create Partitions by Year

After creating the partitioned table, we create partitions based on the date range,
with one partition for 2023 and another for 2024.
sql

CREATE TABLE sale_order_2023 PARTITION OF sale_order

FOR VALUES FROM ('2023-01-01') TO ('2023-12-31');

CREATE TABLE sale_order_2024 PARTITION OF sale_order

FOR VALUES FROM ('2024-01-01') TO ('2024-12-31');

These partitions store sales transaction data by year, with each partition holding data
for its respective year.
Step 4: Migrate Data from the Old Table

To move data from the old sale_order table to the new partitioned table, we use the
INSERT INTO command.

sql
INSERT INTO sale_order_new SELECT * FROM sale_order;
This step migrates all data from the old sale_order table to the newly partitioned
sale_order_new table.
Step 5: Drop the Old Table

Once the data is migrated, we can drop the old sale_order table to replace it with the
new partitioned table.

sql

DROP TABLE sale_order CASCADE;


Step 6: Rename the New Table to sale_order

To maintain compatibility with Odoo, we rename the new table sale_order_new to


sale_order.

sql

ALTER TABLE sale_order_new RENAME TO sale_order;


Step 7: Add Foreign Keys

We add a foreign key constraint to the partner_id column to maintain referential


integrity between the sale_order and res_partner tables.

sql

ALTER TABLE sale_order

ADD CONSTRAINT fk_partner_id


FOREIGN KEY (partner_id)

REFERENCES res_partner(id)

ON DELETE CASCADE;
Step 8: Add the id Column with Serial

We add the id column as a primary key and set it to auto-increment using the
SERIAL data type.

sql

ALTER TABLE sale_order

ADD COLUMN id SERIAL PRIMARY KEY;


Step 9: Add Constraints and Unique Index

To ensure unique data, we add a primary key constraint and a unique index to the
partitions.
sql
ALTER TABLE sale_order_2024

ADD PRIMARY KEY (id, date_order);

CREATE UNIQUE INDEX sale_order_unique_id ON sale_order_2024 (id,


date_order);
Step 10: Link Sequence to Partitions

To ensure each partition uses the same sequence for the id column, we create a new
sequence and link it to the partitioned tables.

sql

CREATE SEQUENCE sale_order_id_seq;

ALTER TABLE sale_order ALTER COLUMN id SET DEFAULT


nextval('sale_order_id_seq');

ALTER TABLE sale_order_2023 ALTER COLUMN id SET DEFAULT


nextval('sale_order_id_seq');

ALTER TABLE sale_order_2024 ALTER COLUMN id SET DEFAULT


nextval('sale_order_id_seq');
Step 11: Testing and Verifying

After setting up the partitions and constraints, test the setup by inserting new data.

sql

INSERT INTO sale_order (name, date_order, partner_id, amount_total)


VALUES ('SO001', '2024-09-30 08:27:07', 1, 100.00);
Step 12: Verify Table Structure and Constraints

Finally, check if all constraints and columns are properly defined.

sql

SELECT conname, conrelid::regclass AS table_name

FROM pg_constraint

WHERE conrelid = 'sale_order'::regclass;

SELECT column_name, is_nullable

FROM information_schema.columns
WHERE table_name = 'sale_order';
3. Benefits of Partitioning the sale.order Table

 Faster Query Performance: With partitioning, queries only need to scan the
relevant partitions (e.g., for January), instead of scanning the entire large
table.
 Easier Data Management: You can easily delete or archive data from
specific periods by dropping the relevant partition, without running large delete
queries.
 Improved Scalability: With millions of transaction records, partitioning makes
the database more manageable as it splits the data into smaller tables.
4. Ensuring Data Access from the Main sale.order Table

Although data is split into partitions, users and Odoo modules can still access it
through the main sale.order table. PostgreSQL automatically routes queries to the
relevant partitions. For example, the following query will still work:

sql

SELECT * FROM sale_order WHERE date_order BETWEEN '2024-01-01' AND


'2024-01-31';

PostgreSQL will automatically direct this query to the relevant partition, like
sale_order_2024, without scanning other partitions.
5. Key Takeaways

Partitioning the sale.order table in Odoo is an effective solution for managing large
datasets. Using yearly range partitioning on the date_order column optimizes query
performance without affecting day-to-day operations. With the support of
PostgreSQL and minor adjustments in Odoo, partitioned tables can be seamlessly
integrated into your system for long-term scalability.

Result of research
I created a record in the sale_order for PT Beringin, and when I checked the query
for the main table, sale_order, the record was successfully created. I also checked
the partitioned table, and the record was successfully inserted there as well.

You might also like