Skip to main content

Command Palette

Search for a command to run...

The Midnight API Timeout: Why Your SQL Results Are Killing Your Performance

Learn how to optimize database performance and improve user experience using DISTINCT, ORDER BY, LIMIT, and LIKE for professional-grade data retrieval

Updated
6 min read
The Midnight API Timeout: Why Your SQL Results Are Killing Your Performance
R

I am a full-stack developer (currently working with cognizant) who is passionate about web development and creating digital products with innovative solutions.

I hold 6+ years of experience in development, understanding clients' unique needs, and delivering quality code. I've honed my coding skills over these years through both my professional exposure and self-study. Have worked with teams that have used agile development methodology.

I am someone who likes to keep acquiring new skills and capabilities. And when I am not a web developer, I enjoy reading, writing and running.

My interests include:

  • Exploring new ideas
  • Learning about future tech & products
  • Finding user-friendly solutions to tech-related problems
  • Sharing my knowledge with close acquaintances

I was staring at a New Relic dashboard that looked like a heart monitor during a marathon. Our "Simple Bookstore" app—which was supposed to be a lightweight internal tool—was dragging its feet. A single API call to fetch the book list was taking 14 seconds.

Fourteen seconds. In tech time, that’s long enough to rethink your career choices and go start a goat farm.

I pulled the logs and found the culprit. A junior dev (it might have been me, let’s be honest) had written a query that essentially said, "Give me everything you’ve got." The database, being a loyal soldier, was trying to ship 500,000 rows of unindexed, unsorted, and highly redundant data over the wire to a frontend that only had room for ten.

That night, I realized that writing SQL isn't about asking for data; it's about sculpting it. If your database is a block of marble, your retrieval clauses are the chisels.

In this guide, we are going to explore four key conceptsDISTINCT, ORDER BY, LIMIT, and LIKE—to help you master the art of surgical data retrieval and stop your APIs from choking on their own ambition.


1. DISTINCT: The "No Plus-Ones" Rule

We’ve all been there: you query the authors table and get "Stephen King" printed forty-two times because he’s written forty-two books.

The Conceptual Foundation

DISTINCT is your filter for redundancy. It tells the SQL engine: "I don't care how many times this value appears; I just want to know it exists."

The Analogy: Imagine you're organizing a family reunion. You don't need to know every single cousin's name to order the "Smith Family" banner; you just need to know which unique families are showing up. DISTINCT is that family headcount.

Real-World Scenario: The Duplicate Notification Disaster

Suppose you're building a newsletter system. If you query SELECT email FROM signups, and a user accidentally clicked "Submit" five times, they’re getting five emails. That’s a one-way ticket to the spam folder.

  • The Fix: SELECT DISTINCT email FROM signups;

Senior Dev Perspective

"Never use DISTINCT to mask a broken data model. If you find yourself needing DISTINCT on every single query just to get 'clean' data, your joins are probably creating a Cartesian product, or your schema design is leaking. Use it for reporting, not as a band-aid for messy architecture."


2. ORDER BY: The Art of Intentional Sequences

Data without order is just noise. By default, SQL returns data in "whatever order the engine found it," which is usually the order of insertion. In a production environment, that’s a gamble you don't want to take.

The Conceptual Foundation

ORDER BY allows you to define the narrative of your data. Whether it's the "Newest First" or "Price: Low to High," this clause dictates the user experience.

The Analogy: Think of a library. A library where books are shelved in the order they were purchased is a nightmare. ORDER BY is the librarian who sorts them by author name or genre so you can actually find what you're looking for.

Scenario-Based Debugging: The Leaderboard Flip

Imagine a gaming app where you want to show the "Top 5 Players." You write:

SELECT username, score FROM players ORDER BY score LIMIT 5;

Suddenly, the community is in an uproar. Why? Because you forgot DESC. You just highlighted the five players with the lowest scores.

  • The Fix: ORDER BY score DESC is the difference between a celebration and an insult.

Pro-Tip: Multi-Column Sorting

Don't forget you can sort by layers. ORDER BY last_name ASC, first_name ASC ensures that all the "Smiths" are grouped together and then sorted by their first names. It’s the hallmark of a polished UI.


3. LIMIT & OFFSET: The API’s Best Friends

This is where the 14-second API disaster usually lives. If you are fetching 100,000 rows to show a "Top 10" list on a mobile screen, you are wasting memory, bandwidth, and CPU cycles.

The Conceptual Foundation

LIMIT sets the ceiling. It tells the database, "Stop looking once you've found X items." OFFSET (often used with LIMIT) tells it where to start.

The Analogy: An "All You Can Eat" buffet is only profitable because people have a physical LIMIT. If everyone could take the entire kitchen home (the database), the restaurant would crash. Your API needs that same boundary.

Scenario-Based Debugging: The "Offset Death Spiral"

You’re building pagination. Page 1 uses LIMIT 10 OFFSET 0. Page 1,000 uses LIMIT 10 OFFSET 10000.

What happens if this fails? As the offset grows, the database still has to scan through those first 10,000 rows just to discard them. On massive datasets, "Page 5,000" can become a performance killer.

  • Senior Dev Perspective: "For high-scale pagination, consider 'Keyset Pagination' (using WHERE id > last_seen_id) instead of large offsets. It keeps your queries O(1) instead of O(N)."

Users rarely know the exact ID or full title of what they’re looking for. They remember fragments.

The Conceptual Foundation

LIKE uses wildcards (% for "anything" and _ for "one thing") to perform pattern matching.

The Analogy: It’s like trying to find a specific shirt in a dark room. You don't know the exact serial number, but you know it has a "logo that looks like a bird" (LIKE '%bird%').

Scenario-Based Debugging: The Missing Percent

A user searches for "SQL," but your query is WHERE title LIKE 'SQL'. It returns zero results. Why? Because the book is titled "Mastering SQL."

  • The Fix: You need the bookends: LIKE '%SQL%'.

Performance Warning

LIKE '%term%' is the "Leading Wildcard" problem. It prevents the database from using B-tree indexes, forcing a full table scan.

  • Senior Dev Perspective: "If your LIKE queries are slowing down, it’s time to graduate to Full-Text Search (FTS) or external tools like ElasticSearch. Don't make SQL do a job it wasn't built for."

Conclusion: The Contract with Your API

Once I implemented a strict LIMIT, added an ORDER BY on an indexed timestamp, and used DISTINCT to clean up some lazy join logic, that 14-second response time dropped to 85 milliseconds.

The database wasn't the problem. My communication with the database was.

As senior developers, our job isn't just to make things work; it's to make them sustainable. Every query you write is a contract between your back-end and your database. When you use these clauses effectively, you’re respecting the hardware, the network, and—most importantly—the user’s time.

We’ve all written that one "Infinite Query of Doom." The trick is to only do it once.

What’s the most "expensive" query you’ve ever accidentally pushed to production? Let’s swap horror stories in the comments.

More from this blog

Rajkumar Thangavel

19 posts

Tech enthusiast with nearly a decade of experience as a software developer across banking, telecom, healthcare and e-commerce. I share real-world learning, perspectives, and practical insights.

Optimizing SQL Performance to Avoid Timeouts