Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • Robots-Blog | Humanoide Robotik aus Deutschland: igus bringt neuen Serviceroboter auf den Markt
    • GM reimagines Hummer off-roader with California ideas unit
    • London’s DEScycle secures over €10 million in grant funding to scale critical metals recovery platform
    • How to Edit, Merge, and Split PDFs With Free Online Tools
    • Florida crackdown targets illegal machines in Sarasota
    • Audiophile-Oriented Noble Audio Debuts More Affordable Osprey Earbuds
    • New radio bursts detected from binary stars
    • Remarkable, Catalysr and Indigenous pre-accelerators score NSW government support for diverse founders
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Tuesday, June 2
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»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)

    Editor Times FeaturedBy Editor Times FeaturedJanuary 8, 2026No Comments6 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    demonstrates that it’s completely attainable to insert 2M data 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 really matches our workload?

    We’ll take a look at 5 methods to insert knowledge into Postgres utilizing Python. The purpose is to not look simply at insert speeds and crown a winner however to know the trade-offs between abstraction, security, comfort and efficiency.

    Ultimately you’ll perceive:

    • the strengths and weaknesses of ORM, Core and driver-level inserts
    • when efficiency really issues
    • how to decide on the fitting software with out over-engineering

    Why quick inserts matter

    Excessive-volume insert workloads present up in all places:

    • loading hundreds of thousands of data
    • 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 scale back system load, liberate staff and enhance total throughput.

    That stated, sooner doesn’t robotically imply higher. When workloads are small sacrificing readability and security for marginal features hardly ever pays off.

    Understanding when efficiency issues and why is the true purpose.


    Which software will 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 prime. Right here’s a fast distinction:

    Psycopg3 (the driving force)

    psycopg3 is a low-level PostgreSQL driver for Python. This can be a very skinny abstraction with minimal overhead that talks to Postgres straight.
    The trade-off is accountability: you write SQL your self, handle bathing and deal with correctness explicitly.

    SQLAlchemy

    SQLAlchemy sits on prime 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 implies 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 prime of Core and abstracts much more. It maps Python courses 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.

    In brief:
    All three exist on a spectrum. On one facet there’s ORM, which takes numerous work out of your palms an supplies numerous 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 correct within the center and offers you a pleasant stability of security, efficiency and management.

    Merely stated:

    • ORM helps you employ the Core extra simply
    • Core helps you employ 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 transferring 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 pace e.g.

    The purpose is to not “discover the quickest insert” however to know what every methodology does nicely.

    Insertion occasions per batch measurement for five completely different strategies

    1) Sooner is all the time higher?

    What is best? A Ferrari or a Jeep?

    This will depend on the downside you’re attempting to resolve.
    In the event 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 various.

    The identical applies with inserting. Shaving 300 milliseconds off a 10-second insert might not justify further complexity and danger. In different circumstances, that achieve is totally value it.

    In some circumstances, the quickest methodology on paper is the slowest if you account for:

    • upkeep value
    • 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 to be like like

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

    Technique Goal
    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 purposes the place readability and security are most necessary. Consider web sites and API’s. Efficiency is normally “adequate” and readability issues extra.

    Core shines in conditions the place you need management with out writing uncooked SQL. Assume knowledge ingestion, batch jobs, analytics pipelines and performance-sensitive companies like ETL jobs.
    You recognize precisely what SQL you need however you don’t wish to handle connections or dialect variations your self.

    The Driver is optimized for max throughput; extraordinarily massive writes like writing hundreds of thousands 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 offers you the very 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 via an abstraction it’s not designed for:

    • Utilizing Core with SQLAlchemy ORM objects – >gradual attributable to conversion overhead
    • Utilizing ORM with tuples – >awkward and brittle
    • ORM bulk in ETL course of – >wasted overhead

    Typically dropping to a decrease stage can really scale 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 (stability between security and pace)
    Driver You might be pushing efficiency limits (uncooked energy and full accountability)

    Conclusion

    In knowledge and AI programs, efficiency isn’t restricted by the database. It’s restricted by how nicely 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 wonderful instruments when used within the context they’re designed for.

    The actual problem isn’t understanding which is fasted, it’s in deciding on the fitting software for you state of affairs.


    I hope this text was as clear as I supposed 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, try my other articles on all types of programming-related matters.

    Joyful coding!

    — Mike

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



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    Escaping the Valley of Choice in BI

    June 2, 2026

    Ensuring Data Integrity with Cryptographic Hashing and the Ethereum Blockchain

    June 1, 2026

    RAG Is Not Machine Learning, and the ML Toolkit Solves the Wrong Problem

    June 1, 2026

    How to Combine Claude Code and Codex for Maximum Coding Power

    June 1, 2026

    It’s the Lessons We Learned Along the Way. Or, Is It?

    June 1, 2026

    Proxy-Pointer RAG: Eliminating Wasteful Entity & Relations Extraction in Knowledge Graphs

    May 31, 2026

    Comments are closed.

    Editors Picks

    Robots-Blog | Humanoide Robotik aus Deutschland: igus bringt neuen Serviceroboter auf den Markt

    June 2, 2026

    GM reimagines Hummer off-roader with California ideas unit

    June 2, 2026

    London’s DEScycle secures over €10 million in grant funding to scale critical metals recovery platform

    June 2, 2026

    How to Edit, Merge, and Split PDFs With Free Online Tools

    June 2, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    Police in Philippines raid illegal online gambling group

    September 13, 2025

    Hughesnet Satellites Are Bringing Faster Internet to Rural America

    May 26, 2025

    Bandcamp bans purely AI-generated music from its platform

    January 18, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

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