DEV Community

Vish
Vish

Posted on

1 1 1 1 1

(SQL Query) Caching vs Indexing in Express.js

Hello Devs, This is my first post on this platform! πŸ‘‹

I wanted to share a surprising experience I had with Express.js and SQL. I’m a beginner developer, and while working on the API for my project, I was handling over 200k+ API requests per day. Initially, I set up an SQLite database (~400 MB) with an Express.js API, and used Node.js caching to speed things up.

At first, the response time for a query was around 200-300 ms, since it involved several JOINs and searches. So, I decided to run some experiments to see if I could improve performance.

Here’s what I did:

  • I removed Node caching.
  • I created indexes on 20+ columns across 5+ tables (my DB has a total of 103 columns in 5 tables).

The result was surprising! After indexing, some of my more complex queries, involving multiple JOINs, were being executed in just 3-5 ms.

Previously, with Node caching:

  • The first request used to take around 300-400 ms.
  • Subsequent requests would take 2-5 ms (due to caching).

After, with only SQLite indexes:
every requests only takes 7-10 ms without any caching.

Now the queries are consistently fast, and this also reduced the server load! Initially, I was running my app on a DigitalOcean droplet with 4 GB of RAM and 4 vCPUs. But after optimizing the SQL queries, I can comfortably handle the same workload with just 2 GB of RAM and 2 vCPUs.

I know this may sound simple, but proper SQL indexing made such a big difference in query performance that I no longer even need to rely on caching. πŸš€

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series πŸ“Ί

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series πŸ‘€

Watch the Youtube series

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay