Back to Blog
Cost Optimization
November 15, 2024
9 min read

How a Single Full Table Scan Can Secretly Double Your Supabase Bill

Discover how one poorly optimized query can dramatically increase your cloud database costs and learn how to prevent it.

#cost-optimization#table-scans

You just opened your monthly cloud bill. You stare at the screen, confused. The database costs have doubled, but your user growth hasn't. You're now paying for a larger compute instance and your I/O operations are through the roof. What happened?

The culprit is likely a silent killer hiding in plain sight: the full table scan.

This guide is for the developer, tech lead, or founder who is responsible for both building features and managing the cloud budget. We'll show you how this seemingly innocent technical issue directly translates into a financial problem and how to fix it in under an hour.

TL;DR:

  1. A full table scan is a database operation that reads an entire table to find data, causing massive spikes in CPU and I/O.
  2. This inefficiency directly drives up your cloud bill by forcing you onto more expensive compute instances and increasing your billed I/O operations.
  3. You can find these scans using EXPLAIN ANALYZE in the Supabase SQL Editor. A "Seq Scan" with a high "cost" is your red flag.
  4. The fix is usually a simple CREATE INDEX, which can reduce the query's cost by over 99% and allow you to downgrade your instance, saving thousands.

The Anatomy of Your Database Bill

Your Supabase project runs on powerful cloud infrastructure, and its costs are driven by factors that are directly impacted by query performance:

  • Compute Instance Size (CPU & Memory): The raw horsepower of your database server. High CPU usage from inefficient queries is the #1 reason teams are forced to upgrade to more expensive instances.
  • I/O Operations (IOPS): A measure of the read/write operations on your disk. Cloud providers bill you for this, and inefficient queries can cause it to skyrocket.

A full table scan attacks both of these cost centers simultaneously.


How to Find the Secret Scans with EXPLAIN ANALYZE

You can find these budget-killers with a simple command in the Supabase SQL Editor. Run this in front of any query you suspect is slow.

The Expensive Plan:

EXPLAIN ANALYZE SELECT * FROM public.orders WHERE user_id = 'some-user-id';
-- Seq Scan on orders (cost=0.00..4500.50 rows=10 width=120) (actual time=0.01..550.2ms rows=10 loops=1)
-- Planning Time: 0.1 ms
-- Execution Time: 550.5 ms

Look for two things:

  • Seq Scan: This is short for Sequential Scan—our full table scan. This is the smoking gun.
  • cost=0.00..4500.50: This isn't dollars, but an arbitrary unit of work. A higher number means more resources (I/O and CPU) are consumed. Think of this as a direct proxy for your cloud bill.

The Fix: A Financial Decision, Not Just a Technical One

The fix is almost always laughably simple: add a database index.

CREATE INDEX idx_orders_user_id ON orders (user_id);

Now, let's run EXPLAIN ANALYZE again:

-- Index Scan using idx_orders_user_id on orders (cost=0.42..8.44 rows=10 width=120) (actual time=0.02..0.04ms rows=10 loops=1)
-- Planning Time: 0.2 ms
-- Execution Time: 0.06 ms

The plan switched to an Index Scan, and the cost plummeted from 4500.50 down to 8.44. That's a 99.8% reduction in the work the database has to do.

This isn't just a performance tweak; it's a massive ROI. The 10 minutes it takes a developer to find and fix a missing index can directly lead to downgrading your database instance, saving your company thousands of dollars a year.


Conclusion: Database Performance is Financial Performance

For any Tech Lead, Engineering Manager, or FinOps professional, the lesson is clear: your database's performance is not just an engineering metric; it's a core financial lever.

Empower your developers to use tools like EXPLAIN ANALYZE and bridge the gap between engineering and finance. Proactively hunting for full table scans isn't just about making the app faster—it's about building a more efficient and profitable business.

Want to optimize your database performance?

Get AI-powered recommendations for your specific database setup.