TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Member-only story

Don’t Crash Your App: Load Records from the Database in Batches for Better Performance

Mike Huls
7 min readApr 18, 2024

--

Python transporting small batches of data (image generated by ChatGPT)

This article is about optimizing the communication between your Python app and a database so that your app runs smoothly and your database server doesn’t melt. This article addresses a common inefficiency: the habit of loading all data from a query at once.

When faced with queries that returns a lot of records it’s often impractical or even impossible to load all returned records. Instead of loading all results in memory and then processing row-by-row, in this article we’ll find out how to load many small chunks. Instead of loading 1 million records and processing we’ll load a 400 batches of 2.500 records each! This way your app doesn’t have to load all results in memory which has clear benefits:

  • Enhanced memory usage
  • Better perceived response times
  • Reduces database stress

We will also take a look under the hood and dive into the technical details of this technique, showing you exactly how it works behind the scenes. Let’s code!

Why use fetchmany

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Mike Huls
Mike Huls

Written by Mike Huls

I write about interesting programming-related things: techniques, system architecture, software design and how to apply them in the best way. — mikehuls.com

No responses yet