"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > (SQL Query) Caching vs Indexing in Express.js

(SQL Query) Caching vs Indexing in Express.js

Published on 2024-11-08
Browse:103

(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. ?

Release Statement This article is reproduced at: https://dev.to/w3vish/sql-query-caching-vs-indexing-in-expressjs-40n6?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3