By John Julius Danker
This blog is a Comparison between of ORM Vs SQL Query on your Odoo performance.
Direct SQL Queries
What are SQL Queries?
Structured Query Language (SQL) is a programming language used for managing and manipulating databases. SQL queries allow you to interact directly with your database to retrieve or modify data.
Odoo ORM
What is Odoo ORM?
ORM acts as an intermediary layer between the application code and the database. It abstracts database interactions by representing database tables as Python classes and records as instances of those classes.
Experiment Code and Result
import cProfile
from odoo import models, fields, api
from odoo.http import request
def direct_sql_query():
query = "SELECT * FROM res_company"
request.env.cr.execute(q)
result = request.env.cr.fetchall()
def odoo_orm_operation():
partners = self.env['res.company].search([])
records = http.request.env[partners]
if __name__ == "__main__":
profiler = cProfile.Profile()
# Profiling the direct SQL query
profiler.enable()
direct_sql_query()
profiler.disable()
print("Direct SQL Query Profiling Results:")
profiler.print_stats(sort="time")
# Profiling the Odoo ORM operation
profiler.enable()
odoo_orm_operation()
profiler.disable()
print("Odoo ORM Operation Profiling Results:")
profiler.print_stats(sort="time")
Odoo SQL
414 function calls in 0.002 seconds
Odoo ORM
793884 function calls (713081 primitive calls) in 0.908 seconds
Use Cases | SQL | Use Cases | ORM |
Integrate with third party to extract large amount of data | Granular Control to join multiple tables and conditions. | Backend logic to update fields | 1 class or table at a time |
Retrieving large amount of data | High performance | Retrieving large amount of data | Performance Overhead |
Comments