Repository navigation
Replies: 1 comment
|
20s vs 2s for the same queries is a big gap, and it's almost certainly not "CockroachDB is just slow" — it's one of three measurable things. Here's how to tell which one, rather than guessing: 1. Scale-to-zero wake-up (most likely, given the free plan). CockroachDB Basic (the free/serverless tier) scales compute to zero when idle. The first request after a period of inactivity pays the cost of waking the cluster back up; later requests are fast. Test it: hit the same slow endpoint twice in a row. If the first call takes ~20s and the second takes ~2s, that's your answer. Mitigation: keep the cluster warm with a periodic trivial query (e.g. 2. Round trips, not query cost. CockroachDB is distributed: every statement crosses the network to the KV layer, and an endpoint that issues many sequential queries (the classic N+1 pattern) pays a network round trip per query. Local Postgres has no network hop at all, which alone explains a large multiplier. Test it: turn on SQLAlchemy echo / log the statements per request and count them, and run 3. Connection churn. Your code already does the right thing by creating the engine once at module level (the pool persists across requests), but two details matter: Most likely it's (1), possibly combined with (2). Run the "same endpoint twice" test first — it takes thirty seconds and settles the biggest suspect. If every request is ~20s even back-to-back, look at (2): count the statements per request and check the query plans. |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I am using FastAPI and connecting to cockroachdb (which I am using the free plan) and my request from the API anytime I query the cockroachdb is extremely slow. My cluster is running on
us-west-2and I am located in California. I don't have the FastAPI deployed anywhere and just running it locally on my laptop which is framework laptop running ubuntu 24.04.I also believe I have a lot of data for my queries to be this slow that some of my endpoints take about ~20s to get a response.
When I connect to a local postgreql on my machine, my requests take ~2s.
I think this is a huge difference and want to know if I am doing something wrong in how I am connecting to cockroachdb, or is this due to it being the free plan?
This is how I connecting to the database in my code
Any help or guidance is greatly appreciated here.
All reactions