A month ago, I migrated all of my sites from Render to Vercel. Then I realized that all of my sites were taking too long to load.
At first, I thought the problem was with the database, so I started optimizing my queries. I didn’t know how to optimize queries properly at first, so I took help from Claude and ChatGPT.
I managed to reduce the number of queries from 86 → 64 → 39 → 22 → 16. But the sites were still slow. Eventually, I couldn’t find anything else to optimize.
Then my exams started, so I put this issue aside and focused on my studies.
After my end-semester exams, I started working on it again. I used AI tools and, honestly, blindly copy-pasted some of the suggested code without fully understanding it. Eventually, I managed to reduce the number of queries to just 8.
But it was still slow.
Then I noticed something interesting: on my local server, a query usually took around 10–50 ms, while the same queries were taking around 200 ms in production.
That’s when I realized that database query optimization wasn’t the real problem.
The actual problem was the deployment region.
My Supabase database was located in South Asia (Mumbai), while my Vercel deployment was running in Washington, DC. The distance between the application server and the database was adding significant network latency to every database request.
I changed the Vercel deployment region to a location closer to my database, and suddenly, everything became fast.
After spending almost two months optimizing queries, I finally discovered that the queries weren't the problem—the distance between my server and database was.