Creating Partition Table in ODOO 17
Creating Partition Table in ODOO 17
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.
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:
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
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
sql
CREATE INDEX idx_sale_order_date_order ON sale_order (date_order);
After creating the partitioned table, we create partitions based on the date range,
with one partition for 2023 and another for 2024.
sql
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
sql
sql
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
To ensure unique data, we add a primary key constraint and a unique index to the
partitions.
sql
ALTER TABLE sale_order_2024
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
After setting up the partitions and constraints, test the setup by inserting new data.
sql
sql
FROM pg_constraint
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
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.