Close Menu
    Trending
    • Why the Sophistication of Your Prompt Correlates Almost Perfectly with the Sophistication of the Response, as Research by Anthropic Found
    • From Transactions to Trends: Predict When a Customer Is About to Stop Buying
    • America’s coming war over AI regulation
    • “Dr. Google” had its issues. Can ChatGPT Health do better?
    • Evaluating Multi-Step LLM-Generated Content: Why Customer Journeys Require Structural Metrics
    • Why SaaS Product Management Is the Best Domain for Data-Driven Professionals in 2026
    • Stop Writing Messy Boolean Masks: 10 Elegant Ways to Filter Pandas DataFrames
    • What Other Industries Can Learn from Healthcare’s Knowledge Graphs
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » Faster Is Not Always Better: Choosing the Right PostgreSQL Insert Strategy in Python (+Benchmarks)
    Artificial Intelligence

    Faster Is Not Always Better: Choosing the Right PostgreSQL Insert Strategy in Python (+Benchmarks)

    ProfitlyAIBy ProfitlyAIJanuary 8, 2026No Comments6 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    demonstrates that it’s completely potential 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 essential query: Which abstractions really matches our workload?

    We’ll take 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 grasp 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 really issues
    • how to decide on the correct instrument with out over-engineering

    Why quick inserts matter

    Excessive-volume insert workloads present up in all places:

    • loading thousands and thousands 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 general throughput.

    That mentioned, sooner doesn’t robotically imply higher. When workloads are small sacrificing readability and security for marginal good points not often pays off.

    Understanding when efficiency issues and why is the true objective.


    Which instrument can we use to insert with?

    To speak to our Postgres database we want a database driver. In our case that is psycopg3 with SQLAlchemy layered on high. Right here’s a fast distinction:

    Psycopg3 (the motive force)

    psycopg3 is a low-level PostgreSQL driver for Python. This can be a very skinny abstraction with minimal overhead that talks to Postgres immediately.
    The trade-off is duty: 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 extremely productive and secure, however all that bookkeeping introduces overhead, particularly for bulk operations.

    Briefly:
    All three exist on a spectrum. On one aspect there’s ORM, which takes plenty of work out of your arms an supplies plenty of security at the price of overhead. On the opposite aspect there’s the Driver could be very bare-bones however supplies most throughput. Core is correct 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 honest:

    • every technique receives knowledge within the type its designed for
      (ORM objects for ORM,dictionaries for Core, tuples for the Driver)
    • solely the time spent transferring knowledge from Python into Postgres is measured
    • no technique is penalized for conversion work
    • The database exists in the identical setting 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 grasp what every technique does nicely.

    Insertion occasions per batch measurement for five completely different strategies

    1) Quicker is at all times higher?

    What is best? A Ferrari or a Jeep?

    This will depend on the downside you’re making an attempt to unravel.
    When you’re traversing a forest go along with the Jeep. If you would like 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 danger. In different instances, that achieve is completely value it.

    In some instances, the quickest technique on paper is the slowest once you account for:

    • upkeep price
    • correctness ensures
    • cognitive load

    2) What’s your Beginning Level?

    The precise insertion technique much less on row depend and extra on what your knowledge already appears like

    The ORM, Core and the motive force are usually not competing instruments. They’re optimized for various functions:

    Methodology Objective
    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 essential. 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 providers 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 max throughput; extraordinarily massive writes like writing thousands and thousands of rows for ML coaching units, bulk masses, database upkeep or migrations or low-latency ingestion providers.

    The driving force minimizes extraction and python overhead and provides you the best throughput. The draw back is that you must 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 pressure knowledge via an abstraction it’s not designed for:

    • Utilizing Core with SQLAlchemy ORM objects – >gradual as a consequence 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 really cut back efficiency.


    When to decide on which?

    Rule of thumb:

    Layer Use it when…
    ORM You might be constructing an software (correctness and productiveness)
    Core You might be transferring or reworking knowledge (steadiness between security and velocity)
    Driver You might be pushing efficiency limits (uncooked energy and full duty)

    Conclusion

    In knowledge and AI methods, efficiency is never restricted by the database. It’s restricted by how nicely our code aligns with the form of the info and the abstractions we select.

    ORM, Core and Driver-level APIs type a spectrum from high-level security to low-level energy. All are wonderful instruments when used within the context they’re designed for.

    The actual problem isn’t understanding which is fasted, it’s in choosing the correct instrument for you scenario.


    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 other articles on every kind of programming-related subjects.

    Comfortable coding!

    — Mike

    P.s: like what I’m doing? Comply with me!



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleStone Center on Inequality and Shaping the Future of Work Launches at MIT | MIT News
    Next Article How to Improve the Performance of Visual Anomaly Detection Models
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Why the Sophistication of Your Prompt Correlates Almost Perfectly with the Sophistication of the Response, as Research by Anthropic Found

    January 23, 2026
    Artificial Intelligence

    From Transactions to Trends: Predict When a Customer Is About to Stop Buying

    January 23, 2026
    Artificial Intelligence

    Evaluating Multi-Step LLM-Generated Content: Why Customer Journeys Require Structural Metrics

    January 22, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    The Machine Learning “Advent Calendar” Day 22: Embeddings in Excel

    December 22, 2025

    Learnings from a Machine Learning Engineer — Part 6: The Human Side

    April 11, 2025

    Real-Time Intelligence in Microsoft Fabric: The Ultimate Guide

    October 4, 2025

    ChatGPT’s New Image Generator, Studio Ghibli Craze and Backlash, Gemini 2.5, OpenAI Academy, 4o Updates, Vibe Marketing & xAI Acquires X

    April 11, 2025

    How to Spark AI Adoption in Your Organization with Janette Roush [MAICON 2025 Speaker Series]

    July 24, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    Boost Your LLM Output and Design Smarter Prompts: Real Tricks from an AI Engineer’s Toolbox

    June 12, 2025

    Higgsfield.ai VFX effekter som ger filmska motion control

    May 4, 2025

    Ny forskning visar att AI-modeller vet när de testas och ändrar sitt beteende

    October 3, 2025
    Our Picks

    Why the Sophistication of Your Prompt Correlates Almost Perfectly with the Sophistication of the Response, as Research by Anthropic Found

    January 23, 2026

    From Transactions to Trends: Predict When a Customer Is About to Stop Buying

    January 23, 2026

    America’s coming war over AI regulation

    January 23, 2026
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.