[Python][MySQL] Performance issue with ORM
Performance issue with ORM when working with databases in Python
When working with database in Python, performance issues can be encountered using ORM (Object Relational Mapping) like SQLAlchemy.
ORM
ORM translates database tables into Python Objects, which allows developers to interact with databases using Python code, avoiding direct SQL queries. This comes at a cost while convenient.
Bookkeeping in ORM
Bookkeeping in ORM means additional internal tasks and overhead required to manage the Python object effectively. SQLAlchemy performs extensive bookkeeping to fulfill the following essential features.
- Unit of Work: Tracks object states (new, modified, deleted) to understand which database operations are needed.
- Identity Map: Ensures each database record corresponds to exactly one Python object to prevent duplication.
- Eager Loading: Pre-fetches related data upfront, rather than retrieving it later.
- Collections Management: Handles groups of related objects.
Performance issue due to Bookkeeping
In general, creating managing Python objects involves computational overhead. Each object requires memory and processing time, and the ORM adds additional tasks for tracking and state management. the more objects, the more overheads accumulated
Comments
Post a Comment