demonstrates that it’s completely attainable to insert 2M information per second into Postgres. As a substitute of chasing micro-benchmarks, on this article we’ll step again to ask a extra necessary query: Which abstractions truly suits our workload?
We’ll have a look at 5 methods to insert knowledge into Postgres utilizing Python. The objective is to not look simply at insert speeds and crown a winner however to know the trade-offs between abstraction, security, comfort and efficiency.
In the long run you’ll perceive:
- the strengths and weaknesses of ORM, Core and driver-level inserts
- when efficiency truly issues
- how to decide on the fitting device with out over-engineering
Why quick inserts matter
Excessive-volume insert workloads present up all over the place:
- loading tens of millions of information
- syncing knowledge from exterior APIs
- backfilling analytics tables
- ingesting occasions or logs into warehouses
Small inefficiencies compound rapidly. Turning a 3-minute insert job right into a 10-second one can cut back system load, unlock staff and enhance total throughput.
That mentioned, quicker doesn’t routinely imply higher. When workloads are small sacrificing readability and security for marginal beneficial properties not often pays off.
Understanding when efficiency issues and why is the actual objective.
Which device will we use to insert with?
To speak to our Postgres database we’d like a database driver. In our case that is psycopg3 with SQLAlchemy layered on high. Right here’s a fast distinction:
Psycopg3 (the driving force)
psycopg3 is a low-level PostgreSQL driver for Python. It is a very skinny abstraction with minimal overhead that talks to Postgres instantly.
The trade-off is accountability: you write SQL your self, handle bathing and deal with correctness explicitly.
SQLAlchemy
SQLAlchemy sits on high of database drivers like psycopg3 and supplies two layers:
1) SQLAlchemy Core
That is the SQL abstraction and execution layer. It’s database-agnostic which signifies that you write Python expressions and Core will translate them into SQL within the right database-dialect (PostgreSQL / SQL Server / SQLite) and safely binds parameters.
2) SQLAlchemy ORM
ORM is constructed on high of Core and abstracts much more. It maps Python lessons to tables, tracks object state and handles relationships. The ORM is very productive and protected, however all that bookkeeping introduces overhead, particularly for bulk operations.
In brief:
All three exist on a spectrum. On one facet there’s ORM, which takes plenty of work out of your fingers an supplies plenty of security at the price of overhead. On the opposite facet there’s the Driver may be very bare-bones however supplies most throughput. Core is true within the center and provides you a pleasant steadiness of security, efficiency and management.
Merely mentioned:
- ORM helps you utilize the Core extra simply
- Core helps you utilize the Driver extra safely and database-agnostic
The benchmark
To maintain the benchmark truthful:
- every methodology receives knowledge within the type its designed for
(ORM objects for ORM,dictionaries for Core, tuples for the Driver) - solely the time spent shifting knowledge from Python into Postgres is measured
- no methodology is penalized for conversion work
- The database exists in the identical surroundings as our Python script; this prevents out benchmark from start bottle-necked by add velocity e.g.
The objective is to not “discover the quickest insert” however to know what every methodology does properly.

1) Sooner is at all times higher?
What is best? A Ferrari or a Jeep?
This will depend on the drawback you’re making an attempt to unravel.
In case you’re traversing a forest go along with the Jeep. If you need be the primary throughout the end line, the Ferrari is a greater different.
The identical applies with inserting. Shaving 300 milliseconds off a 10-second insert could not justify further complexity and threat. In different circumstances, that achieve is totally price it.
In some circumstances, the quickest methodology on paper is the slowest once you account for:
- upkeep value
- correctness ensures
- cognitive load
2) What’s your Beginning Level?
The precise insertion technique much less on row rely and extra on what your knowledge already appears to be like like
The ORM, Core and the driving force aren’t competing instruments. They’re optimized for various functions:
| Technique | Function |
ORM (add_all) |
Enterprise logic, correctness, small batches |
ORM(bulk_save_object) |
ORM objects at scale |
Core (execute) |
Structured knowledge, mild abstraction |
Driver (executemany) |
Uncooked rows, excessive throughput |
Driver (COPY) |
Bulk ingestion, ETL, firehose workloads |
An ORM excels in CRUD-heavy functions the place readability and security are most necessary. Consider web sites and API’s. Efficiency is often “ok” and readability issues extra.
Core shines in conditions the place you need management with out writing uncooked SQL. Suppose knowledge ingestion, batch jobs, analytics pipelines and performance-sensitive companies like ETL jobs.
You understand precisely what SQL you need however you don’t need to handle connections or dialect variations your self.
The Driver is optimized for optimum throughput; extraordinarily giant writes like writing tens of millions of rows for ML coaching units, bulk hundreds, database upkeep or migrations or low-latency ingestion companies.
The driving force minimizes extraction and python overhead and provides you the best throughput. The draw back is that it’s important to manually write SQL, making it straightforward to make errors.
3) Don’t mismatch abstractions
The ORM isn’t gradual. COPY isn’t magic
Efficiency issues seem once we power knowledge by an abstraction it’s not designed for:
- Utilizing Core with SQLAlchemy ORM objects – >gradual as a result of conversion overhead
- Utilizing ORM with tuples – >awkward and brittle
- ORM bulk in ETL course of – >wasted overhead
Typically dropping to a decrease degree can truly cut back efficiency.
When to decide on which?
Rule of thumb:
| Layer | Use it when… |
| ORM | You’re constructing an software (correctness and productiveness) |
| Core | You’re shifting or remodeling knowledge (steadiness between security and velocity) |
| Driver | You’re pushing efficiency limits (uncooked energy and full accountability) |
Conclusion
In knowledge and AI programs, efficiency is never restricted by the database. It’s restricted by how properly our code aligns with the form of the information and the abstractions we select.
ORM, Core and Driver-level APIs type a spectrum from high-level security to low-level energy. All are glorious instruments when used within the context they’re designed for.
The true problem isn’t figuring out which is fasted, it’s in deciding on the fitting device for you state of affairs.
I hope this text was as clear as I meant it to be but when this isn’t the case please let me know what I can do to make clear additional. Within the meantime, take a look at my different articles on every kind of programming-related matters.
Blissful coding!
— Mike
P.s: like what I’m doing? Observe me!


